Windows 以递归方式批重命名文件夹

Windows 以递归方式批重命名文件夹,windows,batch-file,rename,directory,Windows,Batch File,Rename,Directory,这是当前的文件结构:一个标题文件夹、一个人文件夹,然后是一组与此人相关的文件。例如,这里只是文件夹。哦,顺便说一句,显然我不会花很多天来处理卡通人物的数据;这些只是说明性的,希望能让理解更容易 The Simpsons Homer Simpson Homer Simpson - At the Plant Homer Simpson - At Duffs Bar Homer Simpson - On a Date with Marge Bart Simpson

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

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>
对于递归目录,令牌=3 delims=dash%%in(*)使用重命名

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

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

)

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

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

除了使用
ren“%%b”!folder:*-=!”
之外,您还需要使用不同的方法来更新上述Aacini版本,而不是使用
move
命令

Ren
Rename
将仅重命名文件而不是文件夹

使用
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循环中会严重失败,因此如果change目录不起作用,您可能需要将其调用到另一个子例程中。如果是这种情况,您将需要设置某种校验和,以防止您也使用代码的移动部分。这只会更改目录中文件夹的名称


我还没有验证
:*-=%
是否能完全满足您的期望,但这种命令以前从未让我失望过。

感谢您的快速响应和帮助解决此问题的努力!我进行了测试,该例程不会cd到用户的目录中(荷马或巴特)。您可以通过在“当前”文件夹(bat所在的文件夹)中放置一个虚拟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“荷马·辛普森-在工厂”“在工厂”例如,对于每个文件夹,但不成功……好吧,您可以尝试自己努力找到问题。例如,您可以删除
@echo off
行,这样您就可以在屏幕上看到命令的执行,或者您可以在程序中的多个位置插入
echo
命令,这样您就可以看到变量的值,等等。您说:“其他人认为他们可以让其他人做他们的工作,这是对他们的侮辱”<代码>:/
另一件让我伤心的事是,这只深入了一层。我一直在努力深入研究,可以让第二个部分发挥作用,但它会变得很难看。在(%CD%)do()中为/R%%D抛出一个
For/R%%D循环。