JSI Tip 9294. How can I copy files that match a mask?

Jerold Schulman

April 25, 2005

1 Min Read
ITPro Today logo in a gray background | ITPro Today


I have scripted CopyBackup.bat to copy files that match a mask to a folder.

The syntax for using CopyBackup.bat is:

CopyBackup BkpFolder Mask Drive1 [Drive2 Drive3 ... DriveN]Where:

BkpFolder is the full path to the destination folder.Mask      is the file mask, like JSI*.txt to copy all text files that start with JSI,                              or *string*.* to copy all files that have string in their name,                              or ??ABC*.txt to copy all text files that have ABC in the 3-5 position of their name.DriveX    is the source drive or folder or share to fully search.

NOTE: If a file named filename.ext was copied from D:Folder1Folder2, it will be named D_Folder1_Folder2_filename.ext. If you don't want this full identification, remove the !src!_ from the copy statement.

CopyBackup.bat contains:

@echo offif {%3}
<h1><a name="_echo_Syntax_CopyBackup_BkpFolder_Mask_Drive1_Drive2_Drive3_DriveN_amp_goto_EOF_setlocal_ENABLEDELAYEDEXPANSION_set_bkp_1#_set_bkp_bkp_set_bkp_bkp_#_set_bkp_bkp_#_set_mask_2_LOOP_if_2_" id="_echo_Syntax_CopyBackup_BkpFolder_Mask_Drive1_Drive2_Drive3_DriveN_amp_goto_EOF_setlocal_ENABLEDELAYEDEXPANSION_set_bkp_1#_set_bkp_bkp_set_bkp_bkp_#_set_bkp_bkp_#_set_mask_2_LOOP_if_2_">{} @echo Syntax: CopyBackup BkpFolder Mask Drive1 [Drive2 Drive3 ... DriveN]&amp;goto :EOFsetlocal ENABLEDELAYEDEXPANSIONset bkp=%1#set bkp=%bkp:"=%set bkp=%bkp:/#=%set bkp=%bkp:#=%set mask=%2:LOOPif {%2}</a></h1>
{} endlocal&goto :EOFset drv=%2shiftFOR /R %drv% %%f in (%mask%) do ( for /f "Tokens=*" %%x in ('@echo %%f^|FIND /I /V "%bkp%"') do (  set src=%%~DPf  set src=!src::=!  set src=!src:=_!  copy /Y "%%f" "%bkp%!src!_%%~NXf">nul 2>&1 ))goto LOOP



Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like