开发者

create a batch file which copies text files of specified date

开发者 https://www.devze.com 2023-03-16 23:21 出处:网络
I want to create a batch file which copies text file by taking user input. As an input user give from date and to date. The name of the text files are like: error_log_04_06_2011

I want to create a batch file which copies text file by taking user input. As an input user give from date and to date. The name of the text files are like: error_log_04_06_2011

Now for example I want data from 04/06/2011 to 07/06/2011, so i can do that by using xcopy command like this

@ECHO OFF

SET /P f=Please Enter from date(m-d-yyyy):

SET /P t=Please Enter to date(mm-dd-yyyy):

mkdir d:\bkp

xcopy /S /D:%f% /EXCLUDE:%t% C:\Emulator\Log_Data d:\bkp

This will copy all the file from the date mentioned. Now I want to give "to" date also.so i can copy files of a certain period.

One approch is that I use delete command and delete extra files that are copied. But Delete command doesnt 开发者_运维知识库have any switch regarding date. So plz help.....


Unfortunately, there isn't much that can help in this area built-in to batch/cmd. dir can take a flag to order its output by modified time, but I'm guessing you want it to depend on just the filenames and it still wouldn't give you the range you want.

The filename you have shown isn't sortable. If they were named in YYYY_MM_DD format, you could find the files in the date range with sort and a for loop.

Without that, you will end up needing to do date math to generate each individual date in your range, convert each date to your filename format, and pass that to copy.

Rob Van Der Woude has a good list of date functions in batch, including the harder date math. It would be a good place to start. http://www.robvanderwoude.com/battech.php#Time


I haven't tried it, but you could use the xcopy /d and /l switches to generate lists of files that are dated after a certain date to avoid doing the maths.

After the xcopy from the first date you could drive a delete in a for /l loop from a list of file dated after the second date.

0

精彩评论

暂无评论...
验证码 换一张
取 消