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%"}