Hello Salmon ... so i tried what you made and found an issue in which the directory structure if not already created at C:\test2 then it wont copy to the destination. If I manually go in and make the directory structure to match to the source it copies, but if the directory structure isnt created first, then it fails to copy to the destination.
I tested this out on my wifes computer in which I copied some junk data to c:\test1 ( which acts like the last backup location ).
Then copied the same data to c:\test3 ( which acts as the working directory for all data as if its a NAS )
Then deleted 6 files from c:\test1 ( this way c:\test3 contains data that is not present at the last backup )
I then run the first batch and it sends the output of xcopy /L to files2copy.txt
This creates this list:
C:\test3\CrystalDiskInfo7_1_1\DiskInfo.ini
C:\test3\CrystalDiskInfo7_1_1\DiskInfo32.exe
C:\test3\CrystalDiskInfo7_1_1\DiskInfo64.exe
C:\test3\CrystalDiskInfo7_1_1\License\CAlphaButton.txt
C:\test3\CrystalDiskInfo7_1_1\License\COPYRIGHT-ja.txt
C:\test3\CrystalDiskInfo7_1_1\License\COPYRIGHT.txt
6 File(s)
I then run the second batch which removes the last line of this to remove the 6 File(s) portion and this is written to xfiles2copyx.txt with this list:
C:\test3\CrystalDiskInfo7_1_1\DiskInfo.ini
C:\test3\CrystalDiskInfo7_1_1\DiskInfo32.exe
C:\test3\CrystalDiskInfo7_1_1\DiskInfo64.exe
C:\test3\CrystalDiskInfo7_1_1\License\CAlphaButton.txt
C:\test3\CrystalDiskInfo7_1_1\License\COPYRIGHT-ja.txt
C:\test3\CrystalDiskInfo7_1_1\License\COPYRIGHT.txt
Now this third batch as is with c:\test2 empty of directory/folder structure with echo enabled shows this:
copy "C:\test3\CrystalDiskInfo7_1_1\DiskInfo.ini" "c:\test2\CrystalDiskInfo7_1_1
\"
copy "C:\test3\CrystalDiskInfo7_1_1\DiskInfo32.exe" "c:\test2\CrystalDiskInfo7_1
_1\"
copy "C:\test3\CrystalDiskInfo7_1_1\DiskInfo64.exe" "c:\test2\CrystalDiskInfo7_1
_1\"
copy "C:\test3\CrystalDiskInfo7_1_1\License\CAlphaButton.txt" "c:\test2\CrystalD
iskInfo7_1_1\License\"
copy "C:\test3\CrystalDiskInfo7_1_1\License\COPYRIGHT-ja.txt" "c:\test2\CrystalD
iskInfo7_1_1\License\"
copy "C:\test3\CrystalDiskInfo7_1_1\License\COPYRIGHT.txt" "c:\test2\CrystalDisk
Info7_1_1\License\"
Press any key to continue . . .
And with echo removed to actually copy I get this:
The system cannot find the path specified.
0 file(s) copied.
The system cannot find the path specified.
0 file(s) copied.
The system cannot find the path specified.
0 file(s) copied.
The system cannot find the path specified.
0 file(s) copied.
The system cannot find the path specified.
0 file(s) copied.
The system cannot find the path specified.
0 file(s) copied.
Press any key to continue . . .
I then thought that what if I create the directory structure at c:\test2 to see if that will fix this and I then got this:
1 file(s) copied.
1 file(s) copied.
1 file(s) copied.
1 file(s) copied.
1 file(s) copied.
1 file(s) copied.
Press any key to continue . . .
Checked c:\test2 and now c:\test2 has the data and there are 3 files now at c:\test2\CrystalDiskInfo7_1_1\ as well as 3 others in c:\test2\CrystalDiskInfo7_1_1\license
So it looks like the fix to this will be to create the folder structure before the files are copied. Scratching my head I had an idea to add
MD "!destdir!" before the copy instruction and that fixed the issue, but not sure if its the best method or not. It works now but it attempts to create the structure for each line read in and when it already exists it ignores rewriting the folder structure but complains as seen below:
1 file(s) copied.
A subdirectory or file c:\test2\CrystalDiskInfo7_1_1\ already exists.
1 file(s) copied.
A subdirectory or file c:\test2\CrystalDiskInfo7_1_1\ already exists.
1 file(s) copied.
1 file(s) copied.
A subdirectory or file c:\test2\CrystalDiskInfo7_1_1\License\ already exists.
1 file(s) copied.
A subdirectory or file c:\test2\CrystalDiskInfo7_1_1\License\ already exists.
1 file(s) copied.
Press any key to continue . . .
Here is what I did so you can see what I did from what you helped me with:
@echo off
setlocal enabledelayedexpansion
set source=c:\test3
set destin=c:\test2
For /f "delims=" %%F in (xfiles2copyx.txt) do (
set sfile=%%F
set "dfile=!sfile:%source%=%destin%!"
for /f "delims=" %%A in ("!dfile!") do set destdir=%%~dpA
MD "!destdir!"
copy "!sfile!" "!destdir!"
)
pause
I can continue to run with this as the solution since it works, but if there is a better way to only create the structure once and move on vs this hitting it with a hammer method that I got to work, I'd be interested in learning how to do it once for making each directory branch to get rid of the
A subdirectory or file c:\test2\CrystalDiskInfo7_1_1\License\ already exists message.
Maybe its something like
REM place this where it will be updated with latest path
set currentpath=!destdir!
REM place this at the tail end of the loop
set lastpath=!destdir!
Then if the next iteration ( IF statement tests to see if currentpath and lastpath are equal and if equal skip the MD "!destdir!" process until there is an inequality to which it runs the MD "!destdir!" process.
I put this together below in an attempt to get rid of the folder already existing message, but unsuccessful ...
@echo off
setlocal enabledelayedexpansion
set source=c:\test3
set destin=c:\test2
For /f "delims=" %%F in (xfiles2copyx.txt) do (
set sfile=%%F
set "dfile=!sfile:%source%=%destin%!"
for /f "delims=" %%A in ("!dfile!") do set destdir=%%~dpA
REM updated with latest path
set currentpath="!destdir!"
if lastpath==currentpath (echo "skip over making directory") else (MD "!destdir!")
copy "!sfile!" "!destdir!"
REM keeps track of last path for comparison
set lastpath="!destdir!"
)
pause
The output when run is:
1 file(s) copied.
A subdirectory or file c:\test2\CrystalDiskInfo7_1_1\ already exists.
1 file(s) copied.
A subdirectory or file c:\test2\CrystalDiskInfo7_1_1\ already exists.
1 file(s) copied.
1 file(s) copied.
A subdirectory or file c:\test2\CrystalDiskInfo7_1_1\License\ already exists.
1 file(s) copied.
A subdirectory or file c:\test2\CrystalDiskInfo7_1_1\License\ already exists.
1 file(s) copied.
Press any key to continue . . .