Copy a list (txt) of files - batch-file

I've seen some scripts examples over SO, but none of them seems to provide examples of how to read filenames from a .txt list.
This example is good, so as to copy all files from A to B folder
xcopy c:\olddir\*.java c:\newdir /D /E /Q /Y
But I need something like the next, where I can fill actually the source and destination folder:
#echo off
set src_folder = c:\whatever\*.*
set dst_folder = c:\foo
xcopy /S/E/U %src_folder% %dst_folder%
And instead of src_folder = c:\whatever\*.*, those *.* need to be list of files read from a txt file.
File-list.txt (example)
file1.pds
filex.pbd
blah1.xls
Could someone suggest me how to do it?

Given your list of file names in a file called File-list.txt, the following lines should do what you want:
#echo off
set src_folder=c:\whatever
set dst_folder=c:\target
for /f "tokens=*" %%i in (File-list.txt) DO (
xcopy /S/E "%src_folder%\%%i" "%dst_folder%"
)

I just tried to use Frank Bollack and sparrowt's answer, but without success because it included a /U switch for xcopy. It's my understanding that /U means that the files will only be copied if they already exist in the destination which wasn't the case for me and doesn't appear to be the case for the original questioner. It may have meant to have been a /V for verify, which would make more sense.
Removing the /U switch fixed the problem.
#echo off
set src_folder=c:\whatever
set dst_folder=c:\target
for /f "tokens=*" %%i in (File-list.txt) DO (
xcopy /S/E "%src_folder%\%%i" "%dst_folder%"
)

This will do it:
#echo off
set src_folder=c:\batch
set dst_folder=c:\batch\destination
set file_list=c:\batch\file_list.txt
if not exist "%dst_folder%" mkdir "%dst_folder%"
for /f "delims=" %%f in (%file_list%) do (
xcopy "%src_folder%\%%f" "%dst_folder%\"
)

The following will copy files from a list and preserve the directory structure. Useful when you need to compress files which have been changed in a range of Git/SVN commits¹, for example. It will also deal with spaces in the directory/file names, and works with both relative and absolute paths:
(based on this question: How to expand two local variables inside a for loop in a batch file)
#echo off
setlocal enabledelayedexpansion
set "source=input dir"
set "target=output dir"
for /f "tokens=* usebackq" %%A in ("file_list.txt") do (
set "FILE=%%A"
set "dest_file_full=%target%\!FILE:%source%=!"
set "dest_file_filename=%%~nxA"
call set "dest_file_dir=%%dest_file_full:!dest_file_filename!=%%"
if not exist "!dest_file_dir!" (
md "!dest_file_dir!"
)
set "source_file_full=%source%\!FILE:%source%=!"
copy "!source_file_full!" "!dest_file_dir!"
)
pause
Note that if your file list has absolute paths, you must set source as an absolute path as well.
[¹] if using Git, see: Export only modified and added files with folder structure in Git

This will also keep the files original file directory:
#echo off
set src_folder=c:\whatever
set dst_folder=c:\target
set file_list=C:\file_list.txt
for /f "tokens=*" %%i in (%file_list%) DO (
echo f | xcopy /E /C /R /Y "%src_folder%\%%i" "%dst_folder%\%%i"
)

Also can use robocopy and Not use for loop with xcopy - can parse list of files in argument.
robocopy Source_folder Destination_folder [files_to_copy] [options]
Files to copy it's string with Space delimiter.
For example:
robocopy . "d:\my folder" *.txt "my file one.cpp" file2.cpp
robocopy "d:\F 15" "d:\backup\F 15" /E

Related

Copy a file from one directory and replace the file in multiple directory

I need to copy a jar file from directory(source) and replace the file in the destination. But the problem is my destination directories are different as explained below:
Source=D:\temp\R56A
Target=D:\path\AP\Different_folders\lib\i2
Target folder example:-
D:\path\AP\ABC1\lib\i2
D:\path\AP\XY_C\lib\i2
D:\path\AP\GHS3\lib\i2
I AM NOT ABLE TO FETCH THROUGH DIFFERENT FOLDER NAMES and the script not taking it.
This is for a windows box. Can we copy the folder name in a text file and call that text file as variable in a for loop? Is it possible?
#ECHO OFF
REM SETLOCAL ENABLEDELAYEDEXPANSION
set Source=D:\temp\R56A
set Target=D:\path\AP\<Different_Directory_names>\lib\i2
set file=i2-bam.jar
for /f "delims=" %%f in ('dir /a-d /b /s "%Source%\%file%"') do (
copy /V "%%f" "%Target%\" 2>nul
)
PART 2
#ECHO OFF
for /d "D:\temp\R56A\" %%f in (i2-bam.jar) do copy %%f "D:\path\AP\<Different_Directory_names>\lib\i2"
Is this what you're trying to do?
#Echo Off
Set "Source=D:\temp\R56A"
Set "File=i2-bam.jar"
Set "Target=D:\path\AP"
Set "Sub=lib\i2"
If Not Exist "%Source%\%File%" Exit /B
If Not Exist "%Target%\" Exit /B
For /D %%A In ("%Target%\*")Do If Exist "%%A\%Sub%\" Copy /Y "%Source%\%File%" "%%A\%Sub%">Nul

xcopy batch issue

I want to copy a set of subfolders where name contains items on a list. The list has a set of codes (e.g. ABC1, ABC2) but the folders are named ABC1_revised_2018, etc. My batch file I put together is below. What I am getting a '"Usebackq tokens=^" was unexpected' error.
#ECHO ON
SET FileList=C:\filelist.txt
SET Source=C:\Files
SET Destination=C:\Files-Parsed
FOR /D "USEBACKQ TOKENS=^" %%D IN ("%FileList%") DO XCOPY /E /F /D "%Source%\%%~D" "%Destination%\"
GOTO :EOF
I am attempting to use ^ to denote match beginning of string but that clearly isn't working. Any ideas? I have tried with a batch file and also line by line in cmd.
append
Folder
-ABC1-text-date (this is a subfolder)
-ABC2-text-date
filelist.txt only has values like ABC1, ABC2, etc. not exact matches does this help?
Well, if you want to recurse through directories and copy sub directories as per partial matches inside the file:
#echo off
set "FileList=C:\filelist.txt"
set "Source=C:\Files"
set "Destination=C:\Files-Parsed"
for /f "delims=" %%a in (%filelist%) do (
pushd %source%
for /f "delims=" %%i in ('dir /s /b /ad "%%a*"') do xcopy /E /F /D "%%~fi" "%Destination%"
popd
)
after getting the entry in the file, for /d will do a directory listing of the directory* in the source directory and physically copy the dir as C:\source\*\ABC2018 etc.

Copying using a file list

I have a list of filenames without their extension and a folder and sub-folders of those files with their extensions. I am trying to use the list to copy those files to a different location. I tried to use a script I found here and modified it:
#echo off
FOR /R "P:\Case\MyCase\Productions" %%G in (.) do (
for /F "delims=" %%i in (UniqueFileList.txt) do (
if exist %%G\%%i.* xcopy %%G\%%i.* "C:\Temp\CopiedFiles" /D /Y
)
)
My file names are TIN00001.msg, TIN00002.txt, TIN00003.jpg, etc...
and the names in my file list is TIN00001, TIN00002, TIN00003, etc...
How can I use a script to copy the files ?
Any help is appreciated.
Thanks !!
Mustu
I'm not sure if I fully understand your intention but my best guess is that something like this would suit you.
#Echo Off
Set "rootDir=P:\Case\MyCase\Productions"
Set "destDir=C:\Temp\CopiedFiles"
Set "listTxt=%~dp0UniqueFileList.txt"
For /F "Delims=" %%A In ('Where/R "%rootDir%" *.*^|FindStr/LIG:"%listTxt%"'
) Do FindStr/LIX "%%~nA" "%listTxt%">Nul 2>&1 && XCopy "%%A" "%dstDir%" /D /Y
If %listTxt% isn't in the same location as the script then change it's location to a full path too.

Batch file to compare the same files in two folders

I want to create a batch file to compare two folders with the same set of files and copy any files that are different in size from one folder to the other. I tried to adapt the answer from another question as follows as a starting point, but even this doesn't work.
#echo off
Set folder1="C:\folder1"
Set folder2="C:\folder2"
Cd /D "%folder1%"
For %%a in (*.*) do (
For %%b in ("%folder2%\%%a") do (
If "%%~Za" neq "%%~Zb" echo Different file size in %%a
)
)
Update1:
I figured out what was wrong in the example above; it was the quotes around the directories in the set folder commands. The following works as it should:
#echo off
Set folder1=C:\folder1
Set folder2=C:\folder2
Cd /D "%folder1%"
For %%a in (*.*) do (
For %%b in ("%folder2%\%%a") do (
If %%~za neq %%~zb echo Different file size %%a
)
)
Update2:
So this finally does what I want (compares two folders with the same set of files and copies any files that are different in size from one folder to the other):
#echo off
Set folder1=C:\folder1
Set folder2=C:\folder2
Cd /D "%folder1%"
For %%a in (*.*) do (
For %%b in ("%folder2%\%%a") do (
If %%~za neq %%~zb xcopy "%folder1%\%%a" "%folder2%" /y
)
)
The classic way to do this is
xcopy /L /y /d "directory1\*" "directory2"
The /L LISTS the files that would be copied. /d actually says "if the source file in the first parameter is later than the file in the second" which may not be what you want. The /y prevents a prompt for ovewrite (with the /L switch, just a listing is created, but still...)
Files existing in the source but not in the destination will be copied to the destination (or listed if the /L switch is used)
Remove the /L switch to actually perform the copy.
Add /s to repeat for all subdirectories of the source.

How do I copy file and folder structure using xcopy from a text file?

I have a text file containing a list of files and folders. What I want to do is use xcopy to replicate what is written in the text file. My text file looks like this:
"C:\FOLDER"
"C:\FOLDER\FILE1.TXT"
"C:\FOLDER\FILE2.TXT"
"C:\FOLDER\FOLDER2"
"C:\FOLDER\FOLDER2\FILE3.TXT"
For a given output directory "C:\OUTPUT" I would like to replicate the entire structure, so:
"C:\OUTPUT\FOLDER"
"C:\OUTPUT\FOLDER\FILE1.TXT"
"C:\OUTPUT\FOLDER\FILE2.TXT"
"C:\OUTPUT\FOLDER\FOLDER2"
"C:\OUTPUT\FOLDER\FOLDER2\FILE3.TXT"
How can I accomplish this? So far I have written a for loop that reads in each line of the file, but it copies all files if the line is a folder. What I want to do is only copy and create the files and folders that are mentioned in the text file.
#echo off
for /f "delims=] tokens=1*" %%a in (textfile.txt) do (
XCOPY /S /E %%a "C:\OUTPUT"
)
Am I on the right track?
Thank you and best regards,
Andrew
Yes, you are close. Just need to use the existing path as the appended destination path.
Update
#echo off
for /f "delims=" %%A in (textfile.txt) do if exist "%%~fA\*" (
md "C:\Output\%%~pA"
copy /y "%%~fA" "C:\Output\%%~pnxA"
)
Original
If %%A = "C:\Folder\Folder2\File3.txt", then %%~pA = Folder\Folder2
#echo off
for /f "delims=" %%A in (textfile.txt) do (
md "C:\Output\%%~pA"
if not exist "%%~fA\*" echo f | xcopy "%%~fA" "C:\Output\%%~pnxA" /y
)
The if not exist "%%~fA\*" makes sure to only copy the entry if it is not a directory. See Reference for more Techniques and Comments
Type in for /? at the command line to view a list of the variable modifiers. %%~A will remove the surrounding quotations (if any) from the variable.
Post about xcopy prompting issue. and fix #2.
Alternate Setup, since you most likely will not need the xcopy abilities.
#echo off
for /f "delims=" %%A in (textfile.txt) do (
md "C:\Output\%%~pA"
if not exist "%%~fA\*" copy /y "%%~fA" "C:\Output\%%~pnxA"
)

Resources