我有一个包含数千个pdfs和tif的目录树。一个文件夹可能包含多个pdf或tifs,在这种情况下,它们编号为.pdf,2.pdf等...我必须使它们可用并确保它们是首先经过处理的最旧文件 - 所以我想用它们重命名它们创建日期和时间(1.pdf - > 20150415481876.pdf):
目前我使用
@echo off
set datetime=%~t1
set name=%~n1
set extension=%~x1
set year=%datetime:~6,4%
set month=%datetime:~3,2%
set day=%datetime:~0,2%
set hour=%datetime:~11,2%
set min=%datetime:~14,2%
ren %1 "%year%%month%%day%%hour%%min%%name%%extension%"
现在可以正确地将文件重命名为1.tif到2014052513241.tif(文件创建于25.05.2014 13:24)。但是,如果我用batch.bat * .tif调用批处理,我如何能够在同一个文件夹中处理多个文件(例如1.tif 2.tif 3.tif)? 谢谢
答案 0 :(得分:2)
@if (@X)==(@Y) @end /* JScript comment
@echo off
set "extension=tiff"
set "directory=c:\somedir"
pushd "%directory%"
setlocal enableDelayedExpansion
for %%a in (*%extension%) do (
for /f %%# in ('cscript //E:JScript //nologo "%~f0" %%a') do set "cdate=%%#"
echo ren "%%a" "!cdate!%%~xa"
)
rem cscript //E:JScript //nologo "%~f0" %*
exit /b %errorlevel%
@if (@X)==(@Y) @end JScript comment */
FSOObj = new ActiveXObject("Scripting.FileSystemObject");
var ARGS = WScript.Arguments;
var file=ARGS.Item(0);
var d1=FSOObj.GetFile(file).DateCreated;
d2=new Date(d1);
var year=d2.getFullYear();
var mon=d2.getMonth();
var day=d2.getDate();
var h=d2.getHours();
var m=d2.getMinutes();
var s=d2.getSeconds();
var ms=d2.getMilliseconds();
if (mon<10){mon="0"+mon;}
if (day<10){day="0"+day;}
if (h<10){h="0"+h;}
if (m<10){m="0"+m;}
if (s<10){s="0"+s;}
if (ms<10){ms="00"+ms;}else if(ms<100){ms="0"+ms;}
WScript.Echo(""+year+mon+day+h+m+s+ms);
设置您自己的扩展名和目录,将目录中具有给定扩展名的所有文件重命名为其创建日期。格式为YYYYMMDDhhmm
。重命名为echo
,以便您可以查看是否一切正常。如果是从第9行删除echo
字。
答案 1 :(得分:0)
这是执行此操作的本机Windows CMD方法(不需要vb / java脚本)。
如果你想要非常快速和简单,只需在CMD窗口使用它,而不是编写批处理:
FOR /R "Drive:\folder\subfolder\" %Z IN (*.tiff) DO @( FOR /F "Tokens=1-6 delims=:-\/. " %A IN ("%~tZ") DO @( ren "%~dpnxZ" "%~C%~A%~B%~D%~E%~F%~nZ%~xZ") )
如果您仍然喜欢批处理/ CMD文件,这是重写您的脚本以充当CMD,并重新编写与特定搜索模式匹配的目录中的所有文件,格式为& #34; YYYYMMDDHHM [名称] [extention]&#34;
@(
SETLOCAL
echo off
SET "_Skip=NO"
Set "_elvl=0"
)
IF /I "%~2" EQU "" (
SET "_Skip=YES"
)
IF /I "%~1" EQU "/?" (
SET "_Skip=YES"
)
IF /I "%~1" EQU "?" (
SET "_Skip=YES"
)
IF /I "%_Skip%" EQU "YES" (
ECHO. Usage:
ECHO.
ECHO. Rename.bat "Drive:\Path\" "File Glob to match"
ECHO.
ECHO. Example:
ECHO.
ECHO. Rename.bat "C:\Users\%Username%\Desktop\" "*.lnk"
ECHO.
Set "_elvl=2"
GOTO :Finish
)
ECHO. Searching through "%~1" for Files matching this pattern: "%~2"
ECHO.
FOR /R "%~1" %%Z IN (%~2) DO ( FOR /F "Tokens=1-6 delims=:-\/. " %%A IN ("%%~tZ") DO ( REN "%%~dpnxZ" "%%~C%%~A%%~B%%~D%%~E%%~F%%~nZ%%~xZ) )
ECHO.
ECHO. Completed rename of all files matching pattern "%~2" which were found within path: "%~1"
ECHO.
:Finish
(
EndLocal
EXIT /B %_elvl%
)