Batch Rename Files With Similar Name

假装没事ソ 提交于 2019-12-13 18:35:14

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/19817912/batch-rename-files-with-similar-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!