【问题标题】:Batch Rename Folders Recursively递归批量重命名文件夹
【发布时间】:2017-08-18 17:30:39
【问题描述】:

这是当前文件结构:标题文件夹、人员文件夹,然后是与该人员相关的一组文件。例如,这里只是文件夹。哦,顺便说一句,显然我不会花几天时间处理卡通人物的数据;这些只是说明性的,希望能让理解更容易。

The Simpsons
  Homer Simpson
    Homer Simpson - At the Plant
    Homer Simpson - At Duffs Bar
    Homer Simpson - On a Date with Marge
  Bart Simpson
    Bart Simpson - Bart Writing on the Chalkboard
    Bart Simpson - Playing with Millhouse
    Bart Simpson - Earns Money for Camp Krusty
    Bart Simpson - Sings with Lisa
  Etc...

而且因为重复这个名字在很大程度上是多余的,所以我一直在尝试做:

The Simpsons
  Homer Simpson
    At the Plant
    At Duffs Bar
    On a Date with Marge
  Bart Simpson
    Bart Writing on the Chalkboard
    Playing with Millhouse
    Earns Money for Camp Krusty
    Sings with Lisa
  Etc...

基本上,我一直在手动剥离

<artist name><space><dash><space>

来自每个人下方每个子文件夹的“基础”。今天从 Windows 文件资源管理器中的一千个文件夹中手动进行所有操作,并尝试研究/尝试不同的批处理文件以仅以编程方式进行重命名。我有数千人要去,所以我想我会寻求帮助。

我知道 StackOverflow 不是文件写入服务,如果其他人认为他们可以让其他人完成他们的工作,这是一种侮辱。我完全明白了。

我有十几个我“认为”应该起作用的东西,但它们没有。 我开始列出我尝试过的代码,但我认为这会更简洁:

for recursive directories tokens=3 delims=dash %%<some variable> in (*. ) do rename <filename element 1><space><dash><space><filename element 2> with <filename element2>

【问题讨论】:

    标签: windows batch-file rename directory


    【解决方案1】:

    我不习惯回答这类问题,但你的要求对我来说似乎很有趣,所以就在这里!

    @echo off
    setlocal EnableDelayedExpansion
    
    rem Enter to "heading" folder
    cd "The Simspons"
    
    rem Process all "person" folders here
    for /D %%a in (*) do (
    
       rem Enter to this "person" folder
       cd "%%a"
    
       rem Rename all folders here, as requested
       for /F "delims=" %%b in ('dir /B /A:D') do (
          set "folder=%%b"
    
          rem For new name: remove all characters from beginning until " - "
          ren "%%b" "!folder:* - =!"
       )
    
       rem Go back to the "heading" folder
       cd ..
    
    )
    

    如果文件夹名称可能包含感叹号! 字符,则此程序将失败。

    【讨论】:

    • 感谢您的快速响应并为帮助解决此问题付出了努力!我测试过,例程不会 cd 进入该人的目录(Homer 或 Bart)。您可以通过在“当前”文件夹(蝙蝠所在的位置)放置一个虚拟 txt 进行测试,然后尝试将其复制到该人的目录中。我把 "somefile.txt" 放在蝙蝠所在的地方,然后编辑你的代码说: rem Enter to this "person" folder cd "%%a" copy "f:\sandbox\somefile.txt" ) 然后结束 for 循环在这里并暂时删除其余部分以测试 CD 行为。
    • 顺便说一句,我逐字阅读您的文件,对人类来说,逻辑看起来不错并且读起来非常合乎逻辑,但是语法在某个地方关闭了......这就是批处理文件的问题......他们太不直观了。我看不出这种场景(从文件夹名称中删除元素)不是自 1990 年以来没有被覆盖过十亿次的东西。我还可以通过以下方式创建输入文件:cmd、cd,然后是 dir *。 /s > somefile.txt 然后在文本编辑器中编写一个宏,以使用正确的重命名语法标记每一行。试过这个:ren "Homer Simpson - At the Plant" "At the Plant" 例如对于每个文件夹,但不行...
    • 好吧,您可以尝试自己努力找出问题所在。例如,你可以去掉@echo off这一行,这样你就可以在屏幕上看到命令的执行,或者你可以在程序的几个地方插入echo命令,这样你就可以看到变量的值等. 你说:“别人认为他们可以让别人做他们的工作是一种侮辱”... :/
    【解决方案2】:

    不确定是否有人还在寻找这个,由于我缺乏代表,我无法对上述答案发表评论,所以我希望我能在这里提供一些澄清。我在以下方面取得了一些成功:

    除了使用ren "%%b" "!folder:* - =!",您需要对上述Aacini 版本使用不同的方法,而是使用move 命令。

    RenRename 只会重命名文件而不是文件夹。

    使用Move,您可以在此过程中移动文件夹以及更改目录名称。

    借用 Aacini 的代码,你可以试试这样:

    @echo off
    setlocal EnableDelayedExpansion
    
    rem Use the following line so you can run this batch file directly from the main dir, I.E.
    rem "The Simpsons"
    pushd %~dp0
    
    rem Process all "person" folders here
    for /D %%a in (*) do (
    
       rem Set a new variable for the recursive directory searches
       set ndir=%%a
    
       rem Set a variable to make referencing the future directories easier as well as
       rem entering this "person" folder (you may need to place a "\" between %~dp0 and %ndir%)
    
       rem As I mention below -- may need a subroutine for this as well.
       set fdir=%~dp0%ndir%
       cd "%fdir%"
    
       rem Rename all folders here, as requested
       for /F "usebackq tokens=* delims=" %%b in (`dir /B /AD`) do (
    
          rem For making the New Directory Name, we will need to set %%b to a new 
          rem variable and then call out a 'subroutine' from within the for loop.
          set fnm=%%b
    
          call :Moverino
       )
    
    rem Failing to call out to a 'subroutine' will kill the variables in my experience.
    :Moverino
    
    rem Adding an echo here proves your fnm variable took the filename - not required
    echo %fnm%
    
    rem Here you are substituting every character before the ' - ' to equal "nothing"
    set fnm1=%fnm:* - =%
    
    rem Adding another echo here to echo the new fnm1 variable - this will prove whether the
    rem edit works in strings at all, regardless if it is file vs directory.
    echo %fnm1%
    
    rem This timeout was just to check to make sure things worked in debugging
    rem The code moves so fast, it's easy to miss what's happening.
    REM timeout /t 1
    
    rem Here is the sweet-sauce.  "Ren" or "Rename" will not rename directories, however,
    rem Moving directories from one to another as a different name will change the name!
    Move "%~dp0%fnm%" "%~dp0%fnm1%"
    

    请记住,在 For 循环中有些事情会严重失败,因此如果更改目录不起作用,您可能需要将其调出到另一个子例程中。如果是这种情况,您将需要设置某种校验和,以防止您也使用代码的移动部分。这应该只更改目录中文件夹的名称。

    我还没有验证:* - =% 是否会完全按照您的希望执行,但是这种类型的命令以前没有让我失望过。

    【讨论】:

    • 让我难过的另一件事是,这只深入了一层。我一直在努力尝试更深入,并且可以部分完成第二个工作,但它变得丑陋。
    • 在外面抛出一个For /R %%D in (%CD%) do () 循环。
    猜你喜欢
    • 2012-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-13
    • 2018-08-22
    • 1970-01-01
    • 2014-08-11
    • 1970-01-01
    相关资源
    最近更新 更多