Search for files based on a list of partial names and copy them to a destination folder using windows shell -


i absolute newbie batch programming. have posted question after amount of searching. kindly guide me. have folder containing thousand images:

000001_x_abc1.jpg 000001_x_efg1.jpg 000001_x_hij1.jpg 000002_x_abc1.jpg 000002_x_efg1.jpg 000002_x_hij1.jpg . . . . 234562_x_abc2.jpg 234562_x_efg2.jpg 234562_x_hij2.jpg 

of theses files have generated 'list of files' need pull out based on partial names i.e numeric id - first 6 numeric values in file name e.g 234562*.jpg , copy them destination folder.

note: every numeric id based search should give me 3 files , need copy three. appreciated.

i have tried following code based on search:

@echo off  setlocal enabledelayedexpansion  set "dest_dir=my_desination" set "search_dir=my_source"  /f "tokens=*" %%a in (%~dp0my_list.txt%) (     /r "%search_dir%" %%f in (*%%a*) (         set "src=%%~dpf"         set dest=!src:%search_dir%=%dest_dir%!         xcopy /s /i "%%~f" "!dest!"     ) ) 

and list file below:

002631_*.jpg 054741_*.jpg 054992_*.jpg 055053_*.jpg 055054_*.jpg 055118_*.jpg 055267_*.jpg 055294_*.jpg 055382_*.jpg 055415_*.jpg 055466_*.jpg 055546_*.jpg 

supposing list file contains full file patterns 1 per line, following should work you:

@echo off setlocal enableextensions disabledelayedexpansion  rem define constants here: set "sourcedir=d:\data" set "targetdir=d:\backup" set "listfile=d:\files.lst"  cd /d "%sourcedir%" || exit /b 1 /f "usebackq delims= eol=|" %%l in ("%listfile%") (     /f "delims= eol=|" %%f in ('dir /b "%%l"') (        copy "%%~f" "%targetdir%\%%~nxf"     ) )  endlocal exit /b 

Comments