4

I am a windows command line beginner, so my apologies for this basic question. That's a follow up to the question and answer. https://superuser.com/a/999966/914314

FOR /R "C:\Source Folder" %i IN (*.png) DO MOVE "%i" "C:\Staging Folder" was given as solution, but this finds all files with an extension. I would like to move files that have a specific string in their names. I am sure one must change the (*png) bit, but I could not work out how to search for a string here :(

Taking the original post's example a step further, looking to move all files with the string colour:

|parent
|    |a
|    |    123-colour.png
|    |    123abc.png
|    |b
|    |    456-colour.png
|    |    123abc.png
|    |c
|    |    789-colour.png
|    |    123abc.png

should become

|parent
|    123-colour.png
|    456-colour.png
|    789-colour.png
|    |a
|    |    123abc.png
|    |b
|    |    123abc.png
|    |c
|    |    123abc.png

The original folders can and should remain. To make this clear, I left them in the example.

tjebo
  • 175

3 Answers3

3

You can also try where /r, which returns the full file path in "%i"

for /f tokens^=* %i in ('where /r "C:\Source Folder" *colour*.png')do move "%~i" "C:\Staging Folder"
  • Or...
cd /d "C:\Source Folder" & for /f tokens^=* %i in ('where /r . *colour*.png')do move "%~i" "C:\Staging Folder"

rem :: or using pushd and popd..

pushd "C:\Source Folder" & (for /f tokens^=* %i in ('where /r . colour.png')do move "%~i" "C:\Staging Folder") & popd

Io-oI
  • 9,237
1

Found one solution:

FOR /R "C:\Source Folder" %i IN (*string*) DO MOVE "%i" "C:\Staging Folder"

I am not sure if this is a correct way of using REGEX in command line though. But it works.

tjebo
  • 175
1

Try

FOR /R "C:\Source Folder" %i IN (*colour*) DO MOVE "%i" "C:\Staging Folder"
Sysadmin
  • 661