简体   繁体   English

Windows批处理脚本中的增量文件名

[英]Incremental filename in windows batch script

I was looking to save ping results to a text file and have got a crude script batch file running which is working. 我当时想将ping结果保存到文本文件中,并且正在运行一个运行中的粗略脚本批处理文件。

What i want to achieve is that when ever the batch file is run(in this case when the system boots, the user will manually run it) the ping logfile is incremented as in it becomes Day 01 d:\\pingresults.txt Day 02 d:\\pingresults+xx.txt 我要实现的是,无论何时运行批处理文件(在这种情况下,当系统启动时,用户都将手动运行它),ping日志文件会随着它变为Day 01 d:\\ pingresults.txt Day 02 d而增加。 :\\ pingresults + xx.txt

Or if the date can be appended at the end The current batch script 或者是否可以在日期末尾附加日期当前批处理脚本

    :start
@echo ****DO NOT CLOSE****
@echo off
DATE /T
TIME/T
echo %date% >> d:\pinghostname.txt
echo %time% >> d:\pinghostname.txt
ping 8.8.8.8 >> d:\pinghostname.txt
timeout /t 30
goto start

Any ideas? 有任何想法吗?

If I understood it correctly you want to add a timestamps to your files. 如果我正确理解,则想在文件中添加时间戳。

Something like this: 像这样:

SET "job_start_date=%date:~6,10%%date:~3,2%%date:~0,2%"

which will produce (each part between %...% is taking part of the date): 将会产生( %...%之间的每个部分都参与了日期):

20181109

So your script would look like this: 因此,您的脚本应如下所示:

:start
@echo ****DO NOT CLOSE****
@echo off
DATE /T
TIME/T
SET "job_start_date=%date:~6,10%%date:~3,2%%date:~0,2%"
echo %date% >> d:\pinghostname_%job_start_date%.txt
echo %time% >> d:\pinghostname_%job_start_date%.txt
ping 8.8.8.8 >> d:\pinghostname_%job_start_date%.txt
timeout /t 30
goto start

then you will get a filename: pinghostname_20181109.txt 那么您将获得一个文件名: pinghostname_20181109.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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