DOS脚本根据日期动态获取文件名和路径
我必须在 Windows 中编写一些批处理/dos 脚本,将文件放入 UNIX 框中。 但是 Windows 中的路径和文件名每年和每月都会发生变化。
假设 Windows 中的目录位于路径 C:/2009MICS,该目录将保存全年的文件(12 个文件)。 我的批次将每月运行一次,并且应该仅选择相应月份的文件。 例如,如果我的批次在 2009 年 2 月运行。 它应该从 2009MICS 文件夹中选择并传输二月份的文件。
I have to write some batch/dos script in windows which will put the files in UNIX box.
But the path and filenames are getting changed every year and month respectively in windows.
Suppose a directory in windows at path C:/2009MICS which will hold the files for whole year( 12 files).
My batch will run monthly and should pick the files for respective months only.
e.g if my batch is running in Feb,09. It should pick and transfer the file for feb month from 2009MICS folder.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
会将 2009.txt 复制到目标路径。 要包含月份,请使用
免责声明 - 我在德语版 Win Vista 上测试了这一点,希望它也适用于国际版本。
would copy 2009.txt to the destination path. To include the month, use
Disclaimer - I tested that on a German edition of Win Vista, hope it works with the International editions as well.
一种方法是使用 GetDate.cmd(最后)将今天的日期检索到环境变量中。 由此,您可以将
%mm%
变量(当前月份)与文件日期戳的月份进行比较,如下所示:结果:
您可以使用类似的标记结构来解析文件名中的日期,如果日期戳不可靠/不使用。
进一步阅读:
http://ss64.com/nt/syntax-getdate.html - 将日期返回到独立于区域日期设置的环境变量中
http://ss64.com/nt/syntax-datemath.html - 添加或从任意日期减去天数
http://ss64.com/nt/syntax-delolder.html - 删除文件单个文件夹中超过 N 天的内容
One approach would be to retrieve todays date into environment vars with GetDate.cmd (at end). From that you could compare the
%mm%
variable (current month) with the month of the file's datestamp with something like this:Result:
You could use similar token structure to parse the date out of the filename if the datestamp is not reliable/used.
Further reading:
http://ss64.com/nt/syntax-getdate.html - return date into environment vars independent of regional date settings
http://ss64.com/nt/syntax-datemath.html - Add or subtract days from any date
http://ss64.com/nt/syntax-delolder.html - Delete files older than N days from a single folder
我看到两种不同的方式来解释你所问的问题。
日期是由运行脚本的日期驱动的,因此实际上使用系统日期,还是由要复制的文件的文件日期驱动?
Phil 有一个很好的观点,但是如果您未能在给定日期激活 sctipt 并尝试稍后执行(例如 3 月 1 日),则将约会过程锁定到当前日期会给您带来麻烦。
对于文件部分,我会做类似的事情:
我希望我的问题是正确的,否则这就是一个开始:)
I see two different ways to interpret what you are asking..
Is the date driven by the day you run the script, so actually using the system date, or driven by the file date of the files to be copied?
Phil has a good point, but locking the dating process to the current date will jinx your sort if you fail to activate the sctipt on a given date and try to do it later, like march 1st..
for the file part, i'd do something like:
I hope I got the question right, else that's a start right here :)