sorting and renaming files using a batch?

October 29, 2010 at 11:06:11
Specs: microsoft office

I have a folder containing seven .txt files with random filenames, but each file is a different file size.

Using a batch, I'd like to rename these seven files according to a naming scheme that depends on the relative sizes of the files. That is, the smallest file gets renamed "A", the next smallest "B", and so on to "G."

How do I sort these files according to their sizes, then rename them to achieve the desired order? The final product should be the original folder containing seven files labaled A to G in size order.

Complete code would be appreciated.


See More: sorting and renaming files using a batch?

Report •

October 29, 2010 at 13:38:19
:: MYREN.BAT Usage: myre Folder_Name
@echo off & setlocal EnableDelayedExpansion

set folder=%*
if not defined folder (echo.Folder "%folder%" not found & goto :EOF)
set folder=%folder:"=%
if not exist "%folder%" (echo.Folder "%folder%" not found & goto :EOF)

set label= ABCDEFG
set cnt=0

pushd %*
for /F "delims=" %%j in ('dir /OS /A-D /B') do call :REN "%%j"
goto :EOF

  set /A cnt+=1
  if %cnt% gtr 7 (echo.More than 7 files, rename suspended & goto :EOF)
  ren "%~1" "!label:~%cnt%,1!%~x1"
goto :EOF

:: End_Of_Batch

Report •
Related Solutions

Ask Question