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.
Answer
@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.
No comments:
Post a Comment