简体   繁体   中英

How to use batch file to copy and rename file using task scheduler?

This is the script I have right now in a .bat file.

copy S:\Stuff\Stuff\"Database1.accdb" S:\Stuff\Stuff\AccessBackUp\AccessMainDB

This works fine, but in the new backup folder, I get a file named "Database1.accdb"

I don't want each backup to be overwritten. How can I add the date to it? I also tried something like this.

copy S:\Stuff\Stuff\"Database1.accdb" S:\Stuff\Stuff\AccessBackUp\AccessMainDB\"Database1.accdb"%time::=% 

But nothing happens with this.

Is it possible to do several files using this? If so, how?

set "affix=%date%%time%"
set "affix=%affix::=%"
set "affix=%affix:/=%"
copy S:\Stuff\Stuff\"Database1.accdb" "S:\Stuff\Stuff\AccessBackUp\AccessMainDB\Database1.accdb_%affix%"

This sets affix to the concatenation of the date and time, then removes colons and slashes which are not allowed in filenames. Then append the affix to the destination filename.

The code is likely to need tweaking to remove daynames and other unwanted elements. Without knowing exactly what your date and time formats are, it's not possible to advise. For instance, date may be represented by the three elements in dd/mm/yy or mm/dd/yy or yy/mm/dd order, may have leading zeroes suppressed, may have 2 or 4-digit years numbers, may or may not have a dayname which may be abbreviated or not and may not be in English. There's another set of variables for the time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM