批量重命名文件夹

时间:2017-03-25 21:28:20

标签: 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
    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>
来自&#34; base&#34;每个人下面的每个子文件夹。所有这些都是从今天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>

2 个答案:

答案 0 :(得分: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 ..

)

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

答案 1 :(得分:0)

不确定是否有人还在寻找它,由于缺少代表,我无法在以上答案中发表评论,因此希望在此提供一些说明。我在以下方面取得了一些成功:

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

我还没有验证:* - =%是否会完全按照您的期望进行操作,但是这种命令以前并没有使我失败。