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

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:\Folder1\Folder2, 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 off
if \{%3\}

\{\} @echo Syntax: CopyBackup BkpFolder Mask Drive1 \[Drive2 Drive3 ... DriveN\]&goto :EOF setlocal ENABLEDELAYEDEXPANSION set bkp=%1# set bkp=%bkp:"=% set bkp=%bkp:/#=% set bkp=%bkp:#=% set mask=%2 :LOOP if \{%2\}

\{\} endlocal&goto :EOF set drv=%2 shift FOR /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

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.