Rename File with Creation Date & Time in Windows Batch

不羁的心 提交于 2019-11-30 15:26:07
@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);

set your own extension and directory to rename all files with given extension in directory to their creation date.The format will be YYYYMMDDhhmm.Renaming is echoed so you can see if everything is ok.If it is remove the echo word from the 9th line.

Here is a native Windows CMD Method of doing this (no vb/java script needed).

If you want to be really quick and simple just use this at the CMD window instead of writing a batch at all:

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") )

If you still prefer a batch/CMD file, this is a re-write of your script to work as the CMD, and re-write all of the files in a directory matching a certain search pattern to be in the format "YYYYMMDDHHM[Name][extention]"

@(
  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%
)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!