开发者

Writing a batch file for date-based file copying

I need to make a batch file that can copy files from one path to another based on parameters.

For example, typing "datecopy -m 8 c:/copyfrom/*.* d:/c开发者_运维百科opyto/*.*", would find all files in c:/copyfrom dated less than 8 months old, and copy them to d:/copyto -folder. Alternately, instead of -m for month, I could use -h for hour or -y for year.

That's not the full program of course, but should get me started. Thanks for any potential tips. :)


I know this might not look like it's answering your question, but save yourself more pain and heartache than you can imagine by doing this in jscript or VbScript

Lately I've been looking at Windows Powershell, basically Windows Scripting on speed.
However you can be assured that Windows Script Host (jscript & VBScript) is already on Windows from XP onwards (possibly from W2k onwards).

My advice is to NOT use windows batch commands.


Supposing you can assemble a set of good-old-unix tools, do take a look at the find utility. It has the options you ask for, and more.

find c:/copy/from -atime 240 | xargs cp "{}" c:/copyto


Here is a script that will copy files newer than 8 months.

# Script TimedCopy.txt
var str from, to, timediff, list, file
lf -n "*" $from ($ftype=="f") AND ($fmtime > addtime(diff(("-"+$timediff)))) > $list
while ($list <> "")
do
    lex "1" $list > $file
    system copy ("\""+$file+"\"") ("\""+$to+"\"")
done

The lf (list files) command is pretty flexible. Its help page is at http://www.biterscripting.com/helppages/lf.html .

To run the script, copy and paste the script into file C:/Scripts/TimedCopy.txt, start biterscripting and run this command.

script "C:/Scripts/TimedCopy.txt" from("c:/copyfrom") to("d:/copyto") timediff("240000000")

Explanation of the timediff argument

"240000000" means 240 days, 00 hours, 00 mins, 00 secs

"120000" means 12 hours, 00 mins, 00 secs

"3000" means 30 mins, 00 secs

"30" means 30 seconds

etc.

(By dated, I assume you mean modified. If you meant created, use $fctime instead of $fmtime in the script.)

Hope this helps.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜