如何在文本文件名中附加当前日期?

时间:2022-05-03 13:57:26

I have a text file that is created at run time by batch script. how can i append the current date in the text file name like instancename_4/12/2013 ?

我有一个文本文件,在运行时由批处理脚本创建。如何将当前日期附加到文本文件名中,如instancename_4 / 12/2013?

Thanks in advance

提前致谢

3 个解决方案

#1


1  

try this:

echo off
set v = instancename_%date%.log
echo some log >> %v%

#2


2  

Use following

set filea=MyBigFile%date:~10%%date:~4,2%%date:~7,2%%time:~0,2%%time:~3,2%.txt 

#3


1  

set filename=instancename_%date:/=_%.txt

should set the filename close to your specification.

应将文件名设置为接近您的规范。

You can't have / in a filename. The %date:/=_% converts / to _.

你不能拥有/在文件名中。 %date:/ = _%将/转换为_。

Since your date format appears to have a suppressed leading zero, attempting to use substringing won't work properly.

由于您的日期格式似乎具有抑制的前导零,因此尝试使用子字符串将无法正常工作。

We'd really need to know precisely how the variable %date% appears on your machine. It may have the dayname and some people have the monthname spelled out rather than numeric. Some people also have suppressed-leading-zero n the month as well - and I believe it's also possible to have the day or month with a leading space for (day, month)<10 And the year may have 2 or 4 digits, too.

我们确实需要准确了解变量%date%如何出现在您的机器上。它可能有日期名称,有些人的月份名称拼写而不是数字。有些人也在月内也处于领先零位 - 而且我相信也可能有一天或一个月的领先空间(日,月)<10而且这一年也可能有2或4位数。

It may also be possible to use

它也可以使用

for /f "tokens=1,2,3delims=/" %%a in (%date%) do set filename=instancename_%%a_%%b_%%c.txt

again, _ being substituted for /.

再次,_代替/。

The power of this format is that you could also reverse the sequence of %%a..%%c to give you the date in YY_MM_DD format which is better for sorting. YMMV.

这种格式的强大之处在于您还可以反转%% a .. %% c的序列,以YY_MM_DD格式提供日期,这样可以更好地进行排序。因人而异。

#1


1  

try this:

echo off
set v = instancename_%date%.log
echo some log >> %v%

#2


2  

Use following

set filea=MyBigFile%date:~10%%date:~4,2%%date:~7,2%%time:~0,2%%time:~3,2%.txt 

#3


1  

set filename=instancename_%date:/=_%.txt

should set the filename close to your specification.

应将文件名设置为接近您的规范。

You can't have / in a filename. The %date:/=_% converts / to _.

你不能拥有/在文件名中。 %date:/ = _%将/转换为_。

Since your date format appears to have a suppressed leading zero, attempting to use substringing won't work properly.

由于您的日期格式似乎具有抑制的前导零,因此尝试使用子字符串将无法正常工作。

We'd really need to know precisely how the variable %date% appears on your machine. It may have the dayname and some people have the monthname spelled out rather than numeric. Some people also have suppressed-leading-zero n the month as well - and I believe it's also possible to have the day or month with a leading space for (day, month)<10 And the year may have 2 or 4 digits, too.

我们确实需要准确了解变量%date%如何出现在您的机器上。它可能有日期名称,有些人的月份名称拼写而不是数字。有些人也在月内也处于领先零位 - 而且我相信也可能有一天或一个月的领先空间(日,月)<10而且这一年也可能有2或4位数。

It may also be possible to use

它也可以使用

for /f "tokens=1,2,3delims=/" %%a in (%date%) do set filename=instancename_%%a_%%b_%%c.txt

again, _ being substituted for /.

再次,_代替/。

The power of this format is that you could also reverse the sequence of %%a..%%c to give you the date in YY_MM_DD format which is better for sorting. YMMV.

这种格式的强大之处在于您还可以反转%% a .. %% c的序列,以YY_MM_DD格式提供日期,这样可以更好地进行排序。因人而异。