0

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:

  1. 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\"
    
  2. 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\" 
 )
)
2
  • Making 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: the COPY and the first XCOPY (under if 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) Commented Mar 10, 2022 at 19:03
  • (Cont’d) …  And your question wanders all over the place.  Are you totally lost and up the creek?  No, you obviously know how to loop over the contents of a file and do 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. Commented Mar 10, 2022 at 19:03

0

You must log in to answer this question.

Browse other questions tagged .