根据文件夹名称重命名文件 [英] Rename Files Based On Folder Name

查看:399
本文介绍了根据文件夹名称重命名文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下脚本用字符串"Application"重命名(前缀)子文件夹中的文件.

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

例如以下文件:

C:\ temp \ Lodgements \ Smith Street 10

C:\temp\Lodgements\10 Smith Street

10001.doc
10002.doc

重命名为:

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 \ Smith Street 10

C:\temp\Lodgements\10 Smith Street

10001.doc
10002.doc

重命名为:

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

致谢

乔治·麦肯齐小猫

推荐答案

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

(请注意,只需回显rename / ren命令以进行解密)

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

Magoo的挠痒痒:通过消除以下可能性:如果运行了该程序,则名为"10 Smith Street-10001.doc"的文件将被重命名为"10 Smith Street-10 Smith Street-10001.doc"的可能性不止一次...

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

这篇关于根据文件夹名称重命名文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆