Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 以秒为单位将文件批量重命名为创建时间_Batch File - Fatal编程技术网

Batch file 以秒为单位将文件批量重命名为创建时间

Batch file 以秒为单位将文件批量重命名为创建时间,batch-file,Batch File,我重命名了一堆文件,并在文件名前加上文件创建时间(包括秒),同时保留原始文件的末尾。最好是24小时制,但上午/下午也可以 理想示例:Video001.mp4变成20160421-153242-Video001.mp4,Video002.mp4变成20160421-153248-Video002.mp4。格式可能略有不同 下面是我编写的代码,虽然很接近,但不包括秒数: @echo off FOR %%V IN (%1) DO ( FOR /F "tokens=1-6 delims=/: " %%

我重命名了一堆文件,并在文件名前加上文件创建时间(包括秒),同时保留原始文件的末尾。最好是24小时制,但上午/下午也可以

理想示例:
Video001.mp4
变成
20160421-153242-Video001.mp4
Video002.mp4
变成
20160421-153248-Video002.mp4
。格式可能略有不同

下面是我编写的代码,虽然很接近,但不包括秒数:

@echo off

FOR %%V IN (%1) DO (
FOR /F "tokens=1-6 delims=/: " %%J IN ("%%~tV") DO (

IF EXIST %%L%%J%%K_%%O%%M%%N-%%~nV%%~xV (ECHO Cannot rename %%V)

ELSE (Rename "%%V" %%L%%J%%K_%%O%%M%%N-%%~nV%%~xV)
我不知道如何获得文件创建时间的秒数部分。我到处找了找,但还没有找到正确的答案


应用程序允许按创建日期(在多个摄像头源之间)对视频文件进行简单排序。

您可以使用以下注释的批处理代码:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
if "%~1" == "" goto :EOF
for /F delims^=?*^ eol^= %%I in ("%~1") do if "%~1" == "%%I" goto NoPattern
for %%I in (%1) do set "FilePath=%%~dpI" & goto ProcessFiles
echo ERROR: There is no file matched by: "%~1"
goto :EOF
:NoPattern
rem Get full name of directory or file.
for %%I in (%1) do set "FilePath=%%~fI"
if not "%FilePath:~-1%" == "\" set "FilePath=%FilePath%\"
rem References the passed argument string an existing directory?
if exist "%FilePath%" goto ProcessFiles
rem References the passed argument string an existing file?
if exist %1 set "FilePath=%~dp1" & goto ProcessFiles
echo ERROR: Could not find: "%~1"
goto :EOF

:ProcessFiles
rem Escape each backslash in file path with one more backslash.
set "EscapedFilePath=%FilePath:\=\\%"

for /F "eol=| delims=" %%I in ('dir %1 /A-D-H /B 2^>nul') do (

    rem Get name of file without path.
    set "FileName=%%I"
    setlocal EnableDelayedExpansion
    set "EscapedName=!FileName:'=\'!"
    set "FileDateTime="

    rem Get creation date and time of file in format YYYYMMDDHHMMSS.
    for /F "usebackq tokens=2 delims==." %%J in (`%SystemRoot%\System32\wbem\wmic.exe DATAFILE where "name='!EscapedFilePath!!EscapedName!'" GET CreationDate /VALUE 2^>nul`) do set "FileDateTime=%%J"

    if defined FileDateTime (
        rem The first eight characters are the file creation date.
        set "FileDate=!FileDateTime:~0,8!"
        rem The next six characters are the file creation time.
        set "FileTime=!FileDateTime:~8,6!"

        rem Get the first sixteen characters of the current file name.
        set "FileNameStart=!FileName:~0,16!"

        rem Rename the current file only if its name does not already start with the
        rem creation date and time to avoid renaming the same files again and again.
        if not "!FileNameStart!" == "!FileDate!-!FileTime!-" (
            if not exist "!FilePath!!FileDate!-!FileTime!-!FileName!" (
                ren "%FilePath%!FileName!" "!FileDate!-!FileTime!-!FileName!" 2>nul
                if errorlevel 1 echo ERROR: Failed to rename file: "%FilePath%!FileName!"
            ) else (
                echo ERROR: Cannot rename file: "%FilePath%!FileName!"
                echo        There is already a file with name: "!FileDate!-!FileTime!-!FileName!"
            )
        )
    ) else (
        echo ERROR: Failed to get file time for: "%FilePath%!FileName!"
        echo        It is necessary to change manually the file name.
    )
    endlocal
)
endlocal
注意:此批处理文件无法重命名文件名中包含一个或多个逗号的文件

%%~tV
以区域和语言相关格式扩展到最后一次修改文件时间,无需秒数。但需要的是创建文件的秒数时间

-Windows Management Instrumentation命令行实用程序-可用于以与区域和语言无关的日期/时间格式以秒获取创建文件的时间。使用WMIC的缺点是速度。此命令非常慢。有关WMIC文件时间使用的更多详细信息,请阅读上的“我的回答”

如果希望上次修改文件时间而不是创建文件时间,请使用
LastModified
而不是
CreationDate

创建日期和上次修改日期有什么区别?

上次修改日期是上次修改文件内容的日期

创建日期是文件第一次在当前存储的目录中创建的日期

例如,如果文件是昨天新建的,则其创建日期和上次修改日期相同,因为文件是在目录中创建的,而内容是在同一时间最后修改的。如果文件今天复制到其他目录,则上次修改日期仍然是同一日期(昨天),但副本的创建日期是今天

大多数人只对上次修改日期感兴趣,而对文件创建日期不感兴趣,因为重要的是上次修改文件内容的时间,而不是在当前目录中创建文件的时间

要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读为每个命令显示的所有帮助页面

  • 呼叫/?
    …了解
    %1
    %1
  • dir/?
  • echo/?
  • endlocal/?
  • 获取/?
  • goto/?
  • 如果/?
  • rem/?
  • ren/?
  • 设置/?
  • setlocal/?
  • wmic/?
  • wmic数据文件/?
  • wmic数据文件获取/?

您无法从纯批处理语法中获得秒数。为此,您需要使用其他工具。以下PowerShell脚本满足您的要求:

MyRename.ps1

注意:此代码仅显示要重命名的内容。要实际执行重命名,请从ren命令中删除
-WhatIf
参数

如果需要修改的时间而不是创建时间,请将脚本中的
$\uu.CreationTime
更改为
$\uu.LastWriteTime

要从批处理文件调用此PowerShell脚本,请运行
PowerShell.exe

powershell -f .\MyRename.ps1 *.mp4
如果在禁用运行脚本时出现错误,请将
-ExecutionPolicy bypass
添加到命令行

powershell -ExecutionPolicy bypass -f .\MyRename.ps1 *.mp4

根据@Mofi对OP的回答,以下是我允许通过拖放重命名多个文件的版本:

@echo off
if "%~1" == "" goto :eof
SETLOCAL EnableDelayedExpansion

REM outer loop
for %%V in (%*) do (
    set "FileName=%%~fV"
    set "FileName=!FileName:\=\\!"
    set "FileName=!FileName:'=\'!"

    call :sub "!FileName!"

    set "FileDate=!FileDateTime:~0,8!"
    set "FileTime=!FileDateTime:~8,6!"

    if exist "!FileDate!_!FileTime!-%%~nV%%~xV" (
        echo Cannot rename file: %%~fV
    ) else (
        ren "%%~fV" "!FileDate!-!FileTime!-%%~nV%%~xV"
    )
)
REM ENDLOCAL
goto :eof

REM inner loop
:sub
for /F "usebackq skip=1 tokens=1 delims=." %%T in (`%SystemRoot%\System32\wbem\wmic.exe DATAFILE where "name='%~1'" get CreationDate`) do set "FileDateTime=%%T" & goto :next
:next
goto :eof
编辑:对进行小的更改,以便在路径或文件名中使用空格;)


EDIT2:将单引号
'
替换为
\'
以路径或文件名转义它们的小改动

您应该首先搜索它,这很容易找到@这4个链接都没有帮助,因为它们都是关于通过添加当前本地时间重命名文件的。但当被问及如何将
文件的创建时间(包括秒)添加到需要其他方法的文件名时,请参见我答案中的代码。
@echo off
if "%~1" == "" goto :eof
SETLOCAL EnableDelayedExpansion

REM outer loop
for %%V in (%*) do (
    set "FileName=%%~fV"
    set "FileName=!FileName:\=\\!"
    set "FileName=!FileName:'=\'!"

    call :sub "!FileName!"

    set "FileDate=!FileDateTime:~0,8!"
    set "FileTime=!FileDateTime:~8,6!"

    if exist "!FileDate!_!FileTime!-%%~nV%%~xV" (
        echo Cannot rename file: %%~fV
    ) else (
        ren "%%~fV" "!FileDate!-!FileTime!-%%~nV%%~xV"
    )
)
REM ENDLOCAL
goto :eof

REM inner loop
:sub
for /F "usebackq skip=1 tokens=1 delims=." %%T in (`%SystemRoot%\System32\wbem\wmic.exe DATAFILE where "name='%~1'" get CreationDate`) do set "FileDateTime=%%T" & goto :next
:next
goto :eof