Question

I am trying to create a batch script to rename downloaded files. After downloading, the files have similar names that include a timestamp. The timestamps in the name differ from the value of the "last modified" timestamp (typically by only a few seconds). For example:

Export_2013_11_06_15_13_31.csv
Export_2013_11_06_15_13_41.csv
Export_2013_11_06_15_13_51.csv

etc.

Each of these files needs to be renamed to a SPECIFIC name in alphabetical order according to their last modified timestamp (not the timestamp in the name). The most recent file must be named Bart.csv, the next Carol.csv, and the oldest June.csv.

Is there a way to ensure the files are renamed in the correct order?

Was it helpful?

Solution

@echo off&setlocal
set "name1=Bart"
set "name2=Carol"
for /f "delims=" %%a in ('dir /b /a-d /o-d') do (
    set "fname=%%~a"
    set /a counter+=1
    SETLOCAL ENABLEDELAYEDEXPANSION
    call set "nname=%%name!counter!%%"
    echo ren "!fname!" "!nname!%%~xa"
    endlocal
)

Remove echo to get it working.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top