1

我似乎无法理解批处理文件如何将 yyyy/mo/dd/hh/mm/ss 添加到文件名的开头。(使用 Windows 7)精确到秒很重要。

它实际上不必是批处理文件,它只需要是一个小程序,只要我将文件添加到文件夹,目录监视器就可以执行它:http: //brutaldev.com/page/Directory-Monitor.aspx 我只认为批处理文件将是最简单和最有效的方法,但欢迎任何其他建议。

我使用许多具有重叠文件名的按顺序编号的文件,每当我将它们添加到文件夹时,我需要一种快速重命名它们的方法,这样就永远不会有任何同名的文件,但它们仍将保持按顺序排列。这就是我想到将当前日期和时间添加到文件名开头的方式以及为什么秒很重要,因为我可以在一分钟内轻松地将多个集合添加到一个文件夹中,但肯定不会在一秒内。如果批处理文件可以忽略文件扩展名并简单地将当前日期/时间添加到添加到文件夹中的任何文件的开头,那将是理想的。

4

3 回答 3

1

此代码的前四行将为您提供 XP Pro 及更高版本中可靠的 YY DD MM YYYY HH Min Sec 变量。

@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') 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%"
echo datestamp: "%datestamp%"
echo timestamp: "%timestamp%"
echo fullstamp: "%fullstamp%"

:: this line will rename the files in the current folder which haven't already
:: been renamed by checking for the fullstamp format at the start of the line
:: but it will skip this batch file

for /f "delims=" %%a in ('dir /b /a-d ^|findstr /v "^[0-9]*-[0-9]*-[0-9]*_[0-9]*-[0-9]*-[0-9]*" ') do if /i not "%%a"=="%~nx0" ren "%%a" "%fullstamp% - %%a"

pause
于 2014-07-06T08:54:18.960 回答
0
   @ECHO off
   SETLOCAL
   IF [%1] NEQ [] goto s_start

   :: Author - Simon Sheppard, July 2003
   :: Tested for Windows NT, 2K, XP 

   ECHO STAMPME.cmd
   ECHO REName a file with the DATE/Time
   ECHO.
   ECHO SYNTAX
   ECHO       STAMPME TestFile.txt
   ECHO.
   ECHO       STAMPME "Test File.txt"
   ECHO.
   ECHO       STAMPME "c:\docs\Test File.txt"
   ECHO.
   ECHO       In a batch file use CALL STAMPME ...

   :: To change the filename format just change around the last line below

   GOTO :eof

   :s_start

    SET _file=%~n1%
    SET _pathname=%~f1%
    SET _ext=%~x1%

    ::Get the date
   ::  note ISO 8601 date format would require 4 digit YYYY Year)

   FOR /f "tokens=6-8 delims=/ " %%G IN ('NET TIME \\%computername%') DO (
         SET _mm=%%G
         SET _dd=%%H
         SET _yy=%%I
     )

   :: Get the time
   FOR /f "tokens=2-4 delims=:." %%G IN ('cmd /c "time<nul"') DO (
         SET _hr=%%G
         SET _min=%%H
     SET _sec=%%I
     GOTO :done
   )
:done
   ECHO Today is Year: [%_yy%] Month: [%_mm%] Day: [%_dd%]
   ECHO The time is:   [%_hr%]:[%_min%]:[%_sec%]

   REN "%_pathname%" "%_hr%-%_min%-%_sec%@%_file%%_ext%"

这似乎对我有用

于 2012-10-05T10:14:19.813 回答
0

我更喜欢不依赖于本地设置的解决方案(wmic 总是提供相同的格式):

@echo off
setlocal EnableDelayedExpansion
for %%a in (a.*) do (
  for /f %%i in ( 'wmic os get localdatetime /value ^|find "Local"' ) do set %%i
    set ldt=!LocalDateTime:~0,4!-!LocalDateTime:~4,2!-!LocalDateTime:~6,2!-!LocalDateTime:~8,2!-!LocalDateTime:~10,2!-!LocalDateTime:~12,2!-!LocalDateTime:~15,3!

    echo seconds        ### ren %%a !LocalDateTime:~0,14!%%a
    echo milliseconds   ### ren %%a !LocalDateTime:~0,18!%%a
    echo with separator ### ren %%a !ldt!-%%a
)
于 2014-01-01T17:44:39.900 回答