windows 如何使用批处理文件重命名文件以包含日期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18895422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use a Batch-File to Rename a File to Include the Date?
提问by GCSTider
I've got a text file that displays an alarm printer. I would like to set up a batch file, under Windows XP, to change the default name of the alarm printer to include the date, which would make searching for errors much easier. The alarm printer is captured to a text file.
我有一个显示警报打印机的文本文件。我想在 Windows XP 下设置一个批处理文件来更改警报打印机的默认名称以包含日期,这将使查找错误变得更加容易。报警打印机被捕获到一个文本文件中。
I've been able to change the name, but every time I try to set the name to the date either nothing happens or the name is changed to the code instead.
我已经能够更改名称,但是每次我尝试将名称设置为日期时,要么没有任何反应,要么将名称更改为代码。
So far I've tried
到目前为止我已经尝试过
for /f "tokens=1-5 delims=/ "%d in (%date%) do rename "C:\TPM 4 Alarm Printer\test.txt" %%e-%%f-%%g.txt
and
和
for /F "tokens=2,3,4 delims=/ " %%i in ('date/t') do set y=%%k
for /F "tokens=2,3,4 delims=/ " %%i in ('date/t') do set d=%%k%%i%%j
for /F "tokens=5-8 delims=:. " %%i in ('echo.^| time ^| find "current" ') do set t=%%i%%j
set t=%t%_ if "%t:~3,1%"=="_"
set t=0%t% set t=%t:~0,4%
set "C:\Users\e727896\Desktop\test.txt=%d%%t%"
echo %C:\Users\e727896\Desktop\test.txt% –
回答by SachaDee
Something like this should work on XP :
这样的事情应该适用于 XP:
ren "C:\TPM 4 Alarm Printer\test.txt" "%date:~4,2%-%date:~7,2%-%date:~10,4%.txt"
Just adapt it for your problem.
只需根据您的问题调整它。
回答by foxidrive
This will provide a more reliable timestamp if you are using XP Pro.
如果您使用 XP Pro,这将提供更可靠的时间戳。
@echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set "dt=%%a"
set "YY=%dt:~2,2%"
set "YYYY=%dt:~0,4%"
set "MM=%dt:~4,2%"
set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%"
set "Min=%dt:~10,2%"
set "Sec=%dt:~12,2%"
set datestamp=%YYYY%%MM%%DD%
set timestamp=%HH%%Min%%Sec%
set fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%
ren "C:\TPM 4 Alarm Printer\test.txt" "Alarm - %fullstamp%.txt"