Rename Files Based On Folder Name

纵饮孤独 提交于 2019-12-11 20:57:00

问题


I am using the following script to rename (prefix) files in subfolders with the string "Application".

pushd "%temporarydirectory%"
for /r %%j in (*) do (
rename "%%j" "Application - %%~nxj"
)
Popd

For example files in:

C:\temp\Lodgements\10 Smith Street

10001.doc
10002.doc

Are renamed to:

Application - 10001.doc
Application - 10002.doc

What I would like to do is to change the script so that files are renamed (prefixed) with the name of the folder in which they are contained. For example files in:

C:\temp\Lodgements\10 Smith Street

10001.doc
10002.doc

Are renamed to:

10 Smith Street - 10001.doc
10 Smith Street - 10002.doc

Regards

george Mackenzie kitten


回答1:


pushd "%temporarydirectory%" && (
    for /r /d %%a in (.) do for %%b in ("%%~fa\*") do (
        echo ren "%%~fb" "%%~nxa - %%~nxb"
    rem Magoo's tickle begin
    echo "%%~nxb"|FINDSTR /l /c:"%%~nxa - " >NUL&IF ERRORLEVEL 1 echo ren "%%~fb" "%%~nxa - %%~nxb"
    rem Magoo's tickle end
    )
)

For each folder, for each file inside this folder, rename the file to the name and extension of the folder followed by the name and extension of the file

(noting that the rename / ren command is simply echoed for deomstration)

Magoo's tickle: Extends the proposed answer by removing the possibiity that a file named "10 Smith Street - 10001.doc" would be re-renamed "10 Smith Street - 10 Smith Street - 10001.doc" if the procedure was run more than once...



来源:https://stackoverflow.com/questions/25909445/rename-files-based-on-folder-name

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