9

This is my current code

xcopy "C:\Users\Asus\Desktop\Test\Test.MDB" "C:\Users\Asus\Google Drive\" /Y /H /E /F /I
exit

I need the code to do something like:

xcopy "C:\Users\Asus\Desktop\Test\Test.MDB" "C:\Users\Asus\Google Drive\Test (4-21-18).MDB" /Y /H /E /F /I
exit

I need to back up the files every 2 weeks in the task scheduler and I need the script to automatically add the date of the back-up. Also, I have looked at the list of commands (e.g. /Y /H /E) and I cannot find one that describes non-overwriting in the destination folder. I need the back-ups to pile up and not get deleted every time the code runs.

Pherdindy
  • 1,168
  • 7
  • 23
  • 52
  • You can try xcopy "C:\Users\Asus\Desktop\Test\Test.MDB" %sourceFolder% "C:\Users\Asus\Google Drive\copy-%date%" – Akshay Nandwana Apr 21 '18 at 03:17
  • `?` pops out after trying this out: `xcopy "C:\Users\Asus\Test.MDB" %sourceFolder% "D:\Test\copy-%date&" /Y /H /F /I` even with `xcopy "C:\Users\Asus\Test.MDB" %sourceFolder% "D:\Test\copy-%date&"` – Pherdindy Apr 21 '18 at 03:23
  • reference: https://stackoverflow.com/questions/16994879/how-to-append-date-to-directory-path-in-xcopy the last answer to this reference would work, as no answer in this reference marked correct it's difficult to say which is correct. Let us know here which answer would work for you and we make it marked here so it can help others too – Akshay Nandwana Apr 21 '18 at 03:32
  • Thanks although I do not understand how to use `%sourceFolder%` and `%date%`. Are these supposed to be replaced with anything? – Pherdindy Apr 21 '18 at 04:45
  • `xcopy C:\Users\Asus\Test.MDB D:\Test\copy-%date%` %sourceFolder% is your folder from where the file present. Use the above command directly. %date% is too be use as it is. – Akshay Nandwana Apr 21 '18 at 05:45
  • Do I need to add quotation marks to that? `xcopy "C:\Users\Asus\Test.MDB" "D:\Test\copy-%date%"`? Confused as to why %date% portion and other code segments were not documented properly by microsoft when explaining xcopy usage. – Pherdindy Apr 21 '18 at 05:54
  • @Aks Post an answer using %date%. Add the code the OP needs in the post, not a sample. Include in your answer where info is available about `%date%` and other environment variables. – edixon May 02 '18 at 19:39
  • Isn't this a question for [su]? – Lasse V. Karlsen May 09 '18 at 14:03

5 Answers5

2

You can add %date%

If you want to create folders with the date and put the file in it, use like this to join the date to a foldername (D:\myFolder15-04-2020):

xcopy /y /q /s "c:\myFolder\*" "D:\myFolder"%date%"\"

or a folder name with just the date: (D:\15-05-2020)

xcopy /y /q /s "c:\myFolder\*" "D:\"%date%"\"

If you want to put the files in the same folder and change the file name use:

xcopy /y /q /s "c:\myFolder\*" "D:\myFolder\"%date%".MDB*"

The trick is:

"\" at the end of the command means a folder name
"*" at the end of the command means a file name

1

You can create a bat file, get the current date in a variable and have this variable as part of the file name.

This bat file works:

for /f "skip=1" %%x in ('wmic os get localdatetime') do if not defined MyDate
set MyDate=%%x
set today=%MyDate:~4,2%-%MyDate:~6,2%-%MyDate:~2,2%

mkdir "C:\Users\Asus\Google Drive\Test (%today%).MDB"

xcopy "C:\Users\Asus\Desktop\Test\Test.MDB" "C:\Users\Asus\Google Drive\Test (%today%).MDB" /Y /H /E /F /I
exit

This code first saves the current date in "MyDate" variable. Then the desired date format is saved in "today" variable. Finally the content of the "today" variable is used as part of the file name that is passed in "xcopy" as an argument.

Mkdir makes sure that the directory is first created before xcopy is used. This prevents the xcopy question <F = file, D= directory>? that pops out. If a path refers to a file or directory that does not exist, xcopy considers it reasonable to first ask you what it is. Alternatively you could add a '\' in the end of the directory path to indicate that it is a directory.

Spyros K
  • 2,480
  • 1
  • 20
  • 37
1

You can do this. Maybe exist better solutions but it will be working and Additionally, this is an approach for more than one file.

XCOPY /Y /H /E /F /I C:\Users\Asus\Desktop\Test\*.MDB

rem get date, make if file name friendly
FOR /F "tokens=1-4 delims=/ " %%i in ('date/t') do set d=%%i-%%j-%%k-%%l

set MDB=*.%d%.MDB
ren *.MDB %mdb%
move C:\Users\Asus\Desktop\Test\*.MDB C:\Users\Asus\Google Drive\Test\

Hope this help.

QMaster
  • 3,743
  • 3
  • 43
  • 56
0

It works! "echo F|" to auto confirm that you copy a file in the cmd prompt.

call set currentDate=%date:/=-%
call set currentDate=%currentDate:~-10%

echo F|xcopy "C:\Users\Asus\Desktop\Test\Test.MDB" "C:\Users\Asus\Google Drive\Test (%currentDate%).MDB" /Y /H /E /F
exit
Ashokan Sivapragasam
  • 2,033
  • 2
  • 18
  • 39
0

I did this a bit differently:

set today=%date%
set today_processed=%today:/=-%
xcopy /s /y "C:\Documents and Settings\Username\FolderToCopy\" "C:\Users\Username\Documents\BackupFolder\"%today_processed%"\"

Another way to replace the slashes by dashes in the date...

Valentin
  • 76
  • 4