windows - How to rename files based on timestamp, with batch script?
2014-07
Suppose I have 10 files generated by the system every day under D:\Temp. The names are RANDOMLY generated.
Assume I need to rename the oldest one to be 'aaa', the second oldest one to be 'bbb', then 'ccc', 'ddd' and so on.
assume these are the target names I will use:
Beijing
shanghai
hangzhou
suzhou
newyork
lanzhou
huzhou
guangzhou
tianjin
sichuang
Can someone help with a batch script to accomplish this?
I actually asked this question yesterday and was given a wonderful answer using PowerShell, but today I notice in company's VM I don't have PowerShell installed (not allowed to install by yourself), so need to post question again so that people who already answered can retain their (accept) votes.
@echo off
setlocal disableDelayedExpansion
set "file1=aaa"
set "file2=bbb"
set "file3=ccc"
set "file4=ddd"
set "file5=eee"
set "file6=fff"
set "file7=ggg"
set "file8=hhh"
set "file9=iii"
set "file10=jjj"
for /f "tokens=1,2* delims=: " %%A in (
'wmic datafile where "drive='d:' and path='\\temp\\'" get creationDate^, name ^| findstr "^[0-9]" ^| sort ^| findstr /n "^"'
) do for /f "delims=" %%F in ("%%C") do (
setlocal enableDelayedExpansion
for %%N in ("!file%%A!") do (
endlocal
ren "%%F" %%N
)
)
If you want to change the path, then you must be sure to double up all \
as \\
and make sure the path starts and ends with \\
. The appropriate drive letter (with colon) must appear in the drive option.
Both the drive and path values must be enclosed in single quotes - WMIC uses SQL syntax.
This should work for you. It renames 26 files to aaa
to zzz
, oldest creation date first. Look at the output and remove the echo
command, if it is OK.
@echo off&setlocal
set "startfolder=D:\temp"
set "alphas=abcdefghijklmnopqrstuvwxyz"
cd /d "%startfolder%"
set /a counter=0
for /f "tokens=3*" %%a in ('dir /a-d /od /tc ^| findstr "^[0-9]"') do set "fname=%%b"&call:process
goto:eof
:process
setlocal enabledelayedexpansion
set "nname=!alphas:~%counter%,1!!alphas:~%counter%,1!!alphas:~%counter%,1!"
if not defined nname goto:eof
echo rename "%fname%" "!nname!"
endlocal
set /a counter+=1
goto:eof
If your dir
command showes the time with AM/PM, set tokens=4*
.
I want to create a batch file to copy files or folders in a specified directory and append the date and time to their names.
Here's my actual code:
@echo off
Set _bpath=T:\Backup\
if [%1]==[] goto :eof
:loop
Set _file=%~n1%
Set _ext=%~x1%
For /f "tokens=1-3 delims=1234567890 " %%a in ("%time%") Do Set "delims=%%a%%b%%c"
For /f "tokens=1-4 delims=%delims%" %%G in ("%time%") Do (
Set _hh=%%G
Set _min=%%H
Set _ss=%%I
Set _ms=%%J
)
copy %1 "%_bpath%%_file%(%date:/=-% %_hh%h%_min%m%_ss%s)%_ext%"
shift
if not [%1]==[] goto loop
This one works for files only and I could adapt it easily for folders with xcopy /E %1 "%_bpath%%_file%(%date:/=-% %_hh%h%_min%m%_ss%s)
but I would like to avoid dealing with two batch files.
So how to copy/rename them indiscriminately (I have try with copy, xcopy and robocopy without success) or how to distinguish them to create two IF branch? (Using %~x1% is too unreliable...)
This might be useful to you, see this question