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
  • 6
  • If this is not an automation task then I suggest to use TotalCommander or a similar tool: it's easy to search for the files you need, feed them to listbox, then move where you want... See also https://superuser.com/questions/1558135/how-to-delete-all-the-open-files-in-notepad/1560436#1560436 – user2380383 Jun 15 '20 at 14:32

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
  • 7,588
  • 3
  • 12
  • 41
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
  • 6
  • 1
    The `*` character can replace one or more characters in the Windows command line. This syntax is perfectly acceptable. Note that you still may want to be more precise in some cases e.g. `*string*.ext`, assuming you do not wish to move every file with `string` anywhere in the file name. – Anaksunaman Jun 15 '20 at 14:49
1

Try

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