0

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.

5
  • Does it have to be batch? Can you use any portable GUI renaming programs?
    – Karan
    May 16, 2013 at 1:06
  • yes it has to be batch. Can't use any GUI tool as it's a team-shared server and takes a long procedure to install anything there
    – user224703
    May 16, 2013 at 7:01
  • Ella, please register your account here. You seem to keep losing your cookie, and you should edit your original question or leave comments rather than posting answers below. See superuser.com/help/user-merge for more.
    – slhck
    May 16, 2013 at 7:59
  • If you need the creation date, you should show your date format, the output of dir /a-d.
    – Endoro
    May 16, 2013 at 8:47
  • hi slhck, have registered. Should be working fine now. Thanks for remindering.
    – ella
    May 16, 2013 at 15:21

2 Answers 2

0
@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.

3
  • Thanks dbenham, but when I change the path in your script and run it i get 'Description = Invalid query" error code. Here is the script after I changed it. May looks silly but i'm almost totally new to scripting. I also noticed you set "chars= abcdefghij", but what if I need to rename the 10 files differently? Say here are target file names, from oldest to newest ones: Beijing shanghai hangzhou suzhou newyork lanzhou huzhou guangzhou tianjin sichuang
    – ella
    May 16, 2013 at 15:45
  • @ella - You should be more precice with your questions. I've updated the answer to facillitate setting of any target names. You should be able to specify any valid drive and path. Just be sure the syntax is as I've outlined above. If you still have problems, then you should provide the specific path that is not working.
    – dbenham
    May 16, 2013 at 18:26
  • ,thanks for the help. I'll try again when i get office today.
    – ella
    May 19, 2013 at 23:31
0

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*.

2
  • thanks and sorry I have not put it clearly. When I say 'aaa','bbb','ccc',,,i didn't literally mean that. let's assume the target names are : Beijing shanghai hangzhou suzhou newyork lanzhou huzhou guangzhou tianjin sichuang
    – ella
    May 16, 2013 at 15:22
  • oh, I see. In this case you should make a new question.
    – Endoro
    May 16, 2013 at 17:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .