I have a requirement that I need to copy a file to multiple PCs frequently. It has to first check if the file exists, if yes then rename it with DATE and if no, then copy.
I prepared a txt file with all IP's (complist.txt).
I am looking to create a parameter file where user can enter all file/folder locations. The batch code should not be touched by user.
I did the following test:
New file to be copied.
FOR /F "TOKENS=*" %%A IN (C:\Atiq\test_batch_copy\Complist.txt) DO XCOPY /F /Y "C:\Atiq\test_batch_copy\file.txt" "\\%%~A\applsys\Test\"
To rename file if existing
FOR /F "TOKENS=*" %%A IN (C:\Atiq\test_batch_copy\Complist.txt) DO COPY /Y "C:\Atiq\test_batch_copy\file.txt" "\\%%~A\applsys\Test\file_%date:~10,4%%date:~4,2%%date:~7,2%.txt"
Both are working as expected. I would like to maintain one batch for both "Copy new file" and "Rename file if existing".
Please help.
Please let me know if this is the right way to code. Its working. I just want to seperate the parameters in seperate file and call the code in other file. Is it possible?
@echo off;
set source=C:\Atiq\test_batch_copy\file.txt
set target=\\%%~s\applsys\Test\file.txt
set IPList=C:\Atiq\test_batch_copy\Complist.txt
set today=%date:~10,4%%date:~4,2%%date:~7,2%
for /f "tokens=*" %%s in (%IPList%) do (
if exist %target% (
COPY /Y "C:\Atiq\test_batch_copy\file.txt" "\\%%~s\applsys\Test\file_%today%.txt"
XCOPY /F /Y "%source%" "\\%%~s\applsys\Test\"
)
if not exist %target% (
XCOPY /F /Y "%source%" "\\%%~s\applsys\Test\"
)
)
today
a separate variable and setting it in a separate statement is a good step. And thank you for breaking the 180-character long command into shorter lines, and indenting. But please indent consistently: theCOPY
and the firstXCOPY
(underif exist %target%
) should be indented. And indent deeper: four spaces is typical. And try to be clearer. Every time I read “copy the file if it doesn’t exist”, my head explodes a little. … (Cont’d)IF
-EXIST
-THEN
. Where are you stuck? What do you need help with? … … … … … … … … … … … … … … … … Please do not respond in comments; edit your question to make it clearer and more complete.