[英]Append to a filename last modified date and time with a batch file
I am trying to copy all *.csv
files from a folder to another folder, but while copying I need to add modified date and time of file to filename. 我正在尝试将所有
*.csv
文件从一个文件夹复制到另一个文件夹,但是在复制时,我需要将文件的修改日期和时间添加到文件名中。 For example, if filename is Test.csv
and it was modified on 11/21/2018
15:01:10
output should be Test11-21-201815_01-10.csv
. 例如,如果文件名是
Test.csv
并且已在11/21/2018
15:01:10
上进行了修改, 15:01:10
输出应为Test11-21-201815_01-10.csv
。 I found a script to add current timestamp to it, but I need to add modified date of file. 我找到了向其添加当前时间戳的脚本,但是我需要添加文件的修改日期。 You can see it below:
您可以在下面看到它:
@echo off
set Source=C:\csvtest
set Target=C:\csvtest\csvtest\Archive
FOR /f "tokens=1-8 delims=/.:- " %%A in ("%date%%time%") DO (
SET Month=%%B
SET Day=%%C
SET Year=%%D
SET Hours=%%E
SET Minutes=%%F
SET Seconds=%%G
SET All=%%B-%%C-%%D_%%E-%%F-%%G
)
FOR %%i IN ("%Source%\*.csv") DO (
COPY "%%i" "%Target%\%%~Ni %All%.csv")
Thanks in advance for your help. 在此先感谢您的帮助。
There are some examples here on SO for appending a date, 在SO上有一些附加日期的示例,
but I'd use PowerShell as a tool to accomplish this (wrapped in batch). 但是我将使用PowerShell作为完成此任务的工具(分批包装)。
:: Q:\Test\2018\11\23\SO_53450598.cmd
@echo off
set "Source=C:\test"
set "Target=C:\test\Archive"
PowerShell -Nop -C "Get-ChildItem '%Source%\*.csv'|Copy-Item -Destination {'%Target%\{0} {1:MM-dd-yyyy_HH-mm-ss}.csv' -f $_.BaseName,$_.LastWriteTime} -WhatIf"
If the output looks OK, remove the -WhatIf
at the end. 如果输出看起来正常,请最后删除
-WhatIf
。
@LotPings way lasts a lot of time if PowerShell isn't loaded. 如果未加载PowerShell,@ LotPings方式将持续大量时间。
So, I will provide a much faster and more understandable way in batch-file which needs delayedexpansion
: 因此,我将在需要
delayedexpansion
批处理文件中提供一种更快,更易理解的方法:
@echo off
setlocal enabledelayedexpansion
for /f "tokens=1-2" %%a IN ('forfiles /M file.ext /C "cmd /C echo @fdate @ftime"') do (
set line1=%%a
set line2=%%b
set line1r=!line1:/=-!
set line2r=!line2::=-!
rename "file.ext" "file!line1r!!line2r!.ext"
)
rem NOTE: Date format is DD-MM-YYYY. Time format is HH-MM-SS.
Let's break it down: 让我们分解一下:
for /f ...
is well known to you I believe. for /f ...
对于我来说,众所周知。
Command forfiles /M file.ext /C "cmd /C echo @fdate @ftime"
means to run command ( /C
) for file specified in /M
option ( file.ext
). 命令
forfiles /M file.ext /C "cmd /C echo @fdate @ftime"
表示对/M
选项( file.ext
)中指定的文件运行命令( /C
)。 "cmd /C echo @fdate @ftime"
means to open a new cmd and carry out the command specified by string and then terminate. "cmd /C echo @fdate @ftime"
表示打开新的cmd并执行字符串指定的命令,然后终止。 @fdate
is the last modified date of the file and @ftime
is the last modified time of the file. @fdate
是文件的最后修改日期,而@ftime
是文件的最后修改时间。
We set variables line1
and line2
for each of @fdate
and @ftime
and then we make some format changes to them. 我们为
@fdate
和@ftime
分别设置变量line1
和line2
,然后对其进行一些格式更改。
Finally, we rename file.ext
to fileDD-MM-YYYYHH-MM-SS.ext
. 最后,我们将
file.ext
重命名为fileDD-MM-YYYYHH-MM-SS.ext
。
For more information type for /? & forfiles /?
有关更多信息,请输入
for /? & forfiles /?
for /? & forfiles /?
in a fresh cmd. 在一个新鲜的cmd中。
Hope this helps you, as it is faster. 希望这对您有所帮助,因为它更快。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.