0

I have a batch file that copies files form one folder to another at a given time and date (set on Windows 7.0 Task Scheduler).

My problem is I need files with same name not to be overwritten, I have tried piping a echo command to respond to overwright prompt in CMD but files are still overwritten.

Code:

    REM Schedule background backup of Sebia Files, COnfig files, and patinet files

    echo off

    REM May fail if PC off or Flash drive unavailble to add additonal flags to try later
    REM /D No to overwrite existing files. 

    REM echo "No" | copy/-Y c:\source c:\Dest\
    REM echo "No" | xcopy/-Y /s/z  "C:\Users\haematology\Desktop\FolderA" "C:\Users\haematology\Desktop\FolderB"


    rem S will copy all subfolders and files within them Y prevents any overwrite prompts D essentially copies anything newer than what is already in the destination
    xcopy /s/z/Y/D  "C:\Users\haematology\Desktop\FolderA" "C:\Users\haematology\Desktop\FolderB"

   rem tIMESTAMP
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"

set "datestamp=%YYYY%%MM%%DD%" & set "timestamp=%HH%%Min%%Sec%"
set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"
rem echo datestamp: "%datestamp%"
rem echo timestamp: "%timestamp%"
rem echo fullstamp: "%fullstamp%"




msg * "Back up succesful!" %fullstamp% 

Any input appreciated

EDIT:

echo "No" | xcopy/-Y /s/z  "C:\Users\haematology\Desktop\FolderA" "C:\Users\haematology\Desktop\FolderB

or changing Y flag to -Y in

xcopy /s/z/Y/D  "C:\Users\haematology\Desktop\FolderA" "C:\Users\haematology\Desktop\FolderB

does not seem to work, files with the same name will be overwriiten.

dancingbush
  • 2,131
  • 5
  • 30
  • 66
  • 1
    You've commented out the lines where you pass "no" to the copy lines. Also, the `Y` in the `xcopy` command that isn't commented out should be a `-Y` that you pipe "no" to. – SomethingDark Nov 27 '15 at 20:22
  • @SoemthingDark see edit... – dancingbush Nov 27 '15 at 20:37
  • 2
    But `Overwrite ? (Yes/No/All): ` prompt accepts the only character (`y` or `n` or `a`, case insensitive). Therefore, you should use e.g. `echo N|copy /-Y …`. However, I'd switch to [`robocopy`](http://ss64.com/nt/robocopy.html). – JosefZ Nov 27 '15 at 21:07
  • Nope still does not work-fiels with same name will be overwritten, whta is the robocopy sommand? – dancingbush Nov 27 '15 at 21:46
  • Type `robocopy /?` and read help; depending on Windows version it might not be available, but you can download it from Microsoft in case; `robocopy` is an advanced copy/backup tool with numerous option what should be copied/moved/overwritten/skipped... – aschipfl Nov 27 '15 at 21:53
  • 1
    If you do want to use `xcopy`, use switch `/-Y` and use a pipe as @JosefZ suggested: `echo N|xcopy /-Y ...`; this pipe technique can also be used when `xcopy` prompts you whether the destination is a file or a dir. like shown in [this answer](http://stackoverflow.com/a/33770152/5047996)... – aschipfl Nov 27 '15 at 22:10
  • Thanks got it, syntax error on my part. Is it possible to do a check if the file already exisits in the destination folder, and if so save it as a file with different name ie file2.0, file3.0 etc – dancingbush Nov 27 '15 at 22:16
  • Thanks for the feedback...have posted another question here re: renaming ....http://stackoverflow.com/questions/33965599/copy-and-rename-multple-file-types-using-batch-file – dancingbush Nov 27 '15 at 22:38

0 Answers0