Writing a batch file for date-based file copying

会有一股神秘感。 提交于 2019-12-24 10:57:48

问题


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:/copyto/*.*", 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. :)


回答1:


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.




回答2:


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



回答3:


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.



来源:https://stackoverflow.com/questions/2205746/writing-a-batch-file-for-date-based-file-copying

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!