J. Doe
J. Doe

Reputation: 21

Batch script to move files that contain certain string

Hi guys just got the following code script running to search in a directory for files that contain the string "ABC" and move them to the directory at the end.

for /f "eol=: delims=" %%F in ('dir /b^|find "ABC"') do move /Y "%%F"  "C:\DESTINATION_DIRECTORY"

Was wondering how to modify this to not have to be run from the input directory, i.e to add a SOURCE_DIRECTORY variable so I can run this script from elsewhere but have it parse thru the SOURCE_DIRECTORY.

Thanks for any help.

Upvotes: 1

Views: 3905

Answers (1)

npocmaka
npocmaka

Reputation: 57252

@echo off

set "root=c:\st"
pushd %root% && (
    for  %%# in ("*ABC*") do echo move /Y "%%~f#"  "C:\DESTINATION_DIRECTORY"
)
popd

or

for /f "tokens=* delims=" %%# in ('dir /b "%root%\*ABC*"') do echo move /Y "%root%\%%~nx#"  "C:\DESTINATION_DIRECTORY"

for recursive search:

for /f "tokens=* delims=" %%# in ('dir /b /s "%root%\*ABC*"') do echo move /Y "%%~f#"  "C:\DESTINATION_DIRECTORY"

Upvotes: 2

Related Questions