简体   繁体   English

使用批处理文件将文件名附加到上次修改的日期和时间

[英]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分别设置变量line1line2 ,然后对其进行一些格式更改。

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.

相关问题 复制文件并在批处理脚本中追加修改日期 - Copy file and append modified date in batch script 在Windows批处理文件中将日期追加到文件名 - Append Date to Filename in Windows Batch File 通过将文件修改日期/时间添加到文件名的开头来重命名父文件夹和子文件夹中的所有文件的批处理文件 - Batch file to rename all files in a parent folder and sub folders by adding file modified date/time to beginning of the filename 将当前时间与 Windows 批处理文件中上次修改的文件日期时间进行比较 - Compare current time to file date time last modified in Windows Batch File 批处理-将创建日期/时间或随机添加到文件名末尾 - batch - append either date/time of creation or random to end of filename 根据批处理文件中的最后修改日期复制文件 - Copying a file based on last modified date in batch file 附加日期,批处理日志文件的时间 - Append date, time to batch log file 批处理脚本获取远程文件的上次修改日期 - Batch Script Get Last Modified Date of Remote File 如何在批处理文件中追加到文件名 - How to append to filename in batch file 如何检查文件的大小是否从上次在批处理或Java中修改? - How to check if size of a file was modified from the last time in batch or java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM