2

My filenames contain somewhat standardized prefixes. Generally, the format is yyyy-99999-xx.

Filename examples:

2015-12345-NY-0 Coney Island
2015-12345-NY-1 Coney Island
2015-54321-NY Coney Island

As you can see, there can be multiple files containing identical characters 1 thru n. I would like to copy files from a folder by searching for prefixes contained in a .txt file list.

Basically, I would like to "refine/expand" the process shown in Batch: Copy a list (txt) of files so that file-list.txt - I've changed to Prefix-List.txt - contains only the filename "prefixes" of the files to copy. I had to change the xcopy switch to /K in order to copy any files.

Additionally, I would like to redirect errors to Errors.txt

"Nice to have" would be including subfolders in the search.

Using my filename examples above:

2015 would copy 3 files.
2015-12345-NY would copy 2 files.
2015-12345-NY-1 would copy 1 file.

Here's my .bat file

set src_folder=d:\JAR\source
set dst_folder=d:\JAR\destination
for /f "tokens=*" %%i in (File-list.txt) DO (
    xcopy /K "%src_folder%\%%i" "%dst_folder%"
)

Mofi's solution is exactly what I asked for. Now I'd like to expand a little by changing the destination directory name & adding a date-time prefix.

I have 2 questions
1. How to get Mon=08 (instead of Aug)?
2. What is syntax for MKDIR using a variable?

Here's the coding I'm using (modified from Windows batch: formatted date into variable ).

 @echo off
 setlocal 
 for /f "skip=8 tokens=2,3,4,5,6,7,8 delims=: " %%D in ('robocopy /l * \ \  /ns /nc /ndl /nfl /np /njh /XF * /XD *') do (
 set "dow=%%D"
 set "month=%%E"
 set "day=%%F"
 set "HH=%%G"
 set "MM=%%H"
 set "SS=%%I"
 set "year=%%J"
 SET "DESTINATION=%%J%%E%%F%%G%%H%%I-EXTRACTION"
)

 echo Day of the week: %dow%
 echo Day of the month : %day%
 echo Month : %month%
 echo hour : %HH%
 echo minutes : %MM%
 echo seconds : %SS%
 echo year : %year%
 echo DESTINATION : %DESTINATION%

 endlocal

 MKDIR {"%DESTINATION%"}
Community
  • 1
  • 1
jarhtmd
  • 21
  • 1
  • 5
  • With only a cursory look, if these are prefixes, then a wildcard needs to be specified. `xcopy /K "%src_folder%\%%i*" "%dst_folder%")` – lit Aug 08 '15 at 20:22

2 Answers2

0

This doesn't have a lot of testing, but it might point in the right direction.

set src_folder=d:\JAR\source
set dst_folder=d:\JAR\destination
for /f "tokens=*" %%i in (File-list.txt) DO (
    for /f "usebackq tokens=*" %%k in (`dir /s /b "%src_folder%\%%i*") DO
        xcopy /K "%%k" "%dst_folder%"
    )
)
lit
  • 14,456
  • 10
  • 65
  • 119
0

Here is my suggestion for this little batch file with comments:

@echo off
rem Define source and destination directory.
set "src_folder=d:\JAR\source"
set "dst_folder=d:\JAR\destination"

rem Delete error log file from a previous run.
del Errors.txt 2>nul

rem Copy all files in all subdirectories matching
rem any prefix string in prefix list text file.
for /F "delims=" %%i in (Prefix-List.txt) do (
    xcopy "%src_folder%\%%i*" "%dst_folder%" /C /H /I /K /Q /R /S /Y >nul 2>>Errors.txt
)

rem Delete Errors.txt if it is empty because of no error.
for %%F in (Errors.txt) do if %%~zF == 0 del Errors.txt

But I doubt you are really happy with error information as xcopy error messages are really not useful without printing copied files to console and redirecting them all into the text file, too.

For details on the used command, open a command prompt window, run there the following commands and read all output help pages.

  • del /?
  • for /?
  • if /?
  • set /?
  • xcopy /?

Extensions:

  1. Make the source folder a variable passed via first parameter to batch file on execution.
    Display an error message if batch file is executed without a parameter.
    Display an error message if specified source folder does not exist.

  2. Prefix yyyy-mm-dd_ for destination directory independent on local date format.

This modified version of above batch file includes the two extensions:

@echo off
rem Check first parameter of batch file.
if "%~1" == "" goto NoSourceFolder
if not exist "%~1" goto SourceNotExist
if not exist Prefix-List.txt goto NoPrefixList

setlocal
rem Define source directory based on parameter.
set "src_folder=%~1"

rem Get local date and time in a language independent format.
for /F "tokens=2 delims==." %%T in ('%SystemRoot%\System32\wbem\wmic.exe OS GET LocalDateTime /VALUE') do set "LocalDate=%%T"

rem Define local date in format yyyy-mm-dd.
set "LocalDate=%LocalDate:~0,4%-%LocalDate:~4,2%-%LocalDate:~6,2%"

rem Define destination directory with date prefix.
set "dst_folder=d:\JAR\%LocalDate%_destination"

rem Delete error log file from a previous run.
del Errors.txt 2>nul

rem Copy all files in all subdirectories matching
rem any prefix string in prefix list text file.
for /F "delims=" %%i in (Prefix-List.txt) do (
    xcopy "%src_folder%\%%i*" "%dst_folder%" /C /H /I /K /Q /R /S /Y >nul 2>>Errors.txt
)

rem Delete Errors.txt if it is empty because of no error.
for %%F in (Errors.txt) do if %%~zF == 0 del Errors.txt
endlocal
goto :EOF

:NoSourceFolder
echo %~nx0: Missing source folder name as first parameter.
goto HaltOnError

:SourceNotExist
echo %~nx0: There is no folder "%~1".
goto HaltOnError

:NoPrefixList
echo %~nx0: There is no file "%CD%\Prefix-List.txt".

:HaltOnError
echo.
pause

Thanks goes to Jay for answer on How to get current datetime on Windows command line, in a suitable format for using in a filename?

For details on %~1 and %~nx0 run in a command prompt window call /? and read all the output help pages, and read also What does %~dp0 mean, and how does it work?.

Community
  • 1
  • 1
Mofi
  • 46,139
  • 17
  • 80
  • 143
  • How can I modify to 1. Make the Source directory a variable; to be supplied at run time? 2. Add a yyyy-mm-dd prefix to the Destination folder name? – jarhtmd Aug 12 '15 at 09:28