Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use xcopy to add the date in the destination file?

Tags:

xcopy

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.

like image 870
Pherdindy Avatar asked Apr 21 '18 03:04

Pherdindy


People also ask

Does xcopy create destination folder?

Copying Files to a New Folder If the destination folder does not exist, Xcopy creates the destination folder using the /I option.

How do I use xcopy files?

Press F if you want the file or files to be copied to a file. Press D if you want the file or files to be copied to a directory. You can suppress this message by using the /i command-line option, which causes xcopy to assume that the destination is a directory if the source is more than one file or a directory.

Will xcopy overwrite files?

As noted in this SS64 page on XCOPY , the /Y option suppresses the prompt to confirm overwriting a file. Your welcome. Glad to hear it was a success. =)


3 Answers

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.

like image 57
QMaster Avatar answered Sep 26 '22 22:09

QMaster


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.

like image 32
Spyros K Avatar answered Sep 26 '22 22:09

Spyros K


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

like image 23
Carlos Ferreira Avatar answered Sep 27 '22 22:09

Carlos Ferreira