通过添加文件夹的名称并将它们移动到另一个文件夹来递归重命名文件

时间:2013-06-06 20:49:16

标签: windows cmd command

我有这个:

>upperFolder
  >folder 0
    >file 0
    >file ...
    >file n
  >...
  >foldern
    >file 0
    >file ...
    >file n

......我想要这个

>upperFolder
  >folder 0_file 0
  >folder 0_file ...
  >folder 0_file n
  >...
  >folder n_file 0
  >folder n_file ...
  >folder n_file n

例如:

我有这个:

>Pictures
  >Wallpapers
    >Reaper
    >Skull
    >Night
  >Avatars
    >InuYasha
    >Battler
    >Valentino

......我想要这个

>Pictures
  >Wallpapers_Reaper
  >Wallpapers_Skull
  >Wallpapers_Night
  >Avatars_InuYasha
  >Avatars_Battler
  >Avatars_Valentino

我需要这个来重命名Windows cmd中的大量文件。手动完成这一切变得越来越困难。提前谢谢。

3 个答案:

答案 0 :(得分:2)

如果您只需要将文件移动一级深度,则可以从命令行使用以下命令:

for /d %D in (*) do @for %F in ("%D\*") do @move "%F" "%D_%~nxF" >nul

如果要在批处理脚本中运行命令,则加倍所有百分比。

如果您确实希望以递归方式将层次结构中的所有文件移动到父文件夹,则以下批处理脚本将起作用:

for /f "eol=: delims=" %%D in ('dir /b /s /ad *^|sort /r') do (
  pushd "%%D"
  for %%F in (*) do move "%%F" "..\%%~nxD_%%F" >nul
  popd
)

所以像

这样的目录结构
>Pictures
  moveFiles.bat
  pic1.jpg
  pic2.jpg
  >Avatars
    Battler.jpg
    InuYasha.jpg
    Valentino.jpg
  >Wallpapers
    pic1.jpg
    pic2.jpg
    >Nature
      Forest.jpg
      Ocean.jpg
    >Sports
      Baseball.jpg
      Basketball.jpg
      Football.jpg

变为:

>Pictures
  Avatars_Battler.jpg
  Avatars_InuYasha.jpg
  Avatars_Valentino.jpg
  moveFiles.bat
  pic1.jpg
  pic2.jpg
  Wallpapers_Nature_Forest.jpg
  Wallpapers_Nature_Ocean.jpg
  Wallpapers_pic1.jpg
  Wallpapers_pic2.jpg
  Wallpapers_Sports_Baseball.jpg
  Wallpapers_Sports_Basketball.jpg
  Wallpapers_Sports_Football.jpg
  >Avatars
  >Wallpapers
    >Nature
    >Sports

答案 1 :(得分:1)

试试这个,查看输出并删除echo之前的move,如果可以的话:

@echo off&setlocal
set "startfolder=X:\path\upperFolder"
cd /d "%startfolder%"
for /d %%i in (*) do (
    for %%j in ("%%~i\*") do (
        if not exist "%startfolder%\%%~i_%%~nxj" (
        echo move "%%~fj" "%startfolder%\%%~i_%%~nxj"
        ) else echo ERROR: "%startfolder%\%%~i_%%~nxj" already exist.
    )
)

输入:

C:\UPPERFOLDER
├───Folder1
│       File11
│       File12
│       File13
│       File14
│       File15
│       File16
│       File17
│       File18
│       File19
│
├───Folder2
│       File21
│       File22
│       File23
│       File24
│       File25
│       File26
│       File27
│       File28
│       File29
│
├───Folder3
│       File31
│       File32
│       File33
│       File34
│       File35
│       File36
│       File37
│       File38
│       File39
│
├───Folder4
│       File41
│       File42
│       File43
│       File44
│       File45
│       File46
│       File47
│       File48
│       File49
│
├───Folder5
│       File51
│       File52
│       File53
│       File54
│       File55
│       File56
│       File57
│       File58
│       File59
│
├───Folder6
│       File61
│       File62
│       File63
│       File64
│       File65
│       File66
│       File67
│       File68
│       File69
│
├───Folder7
│       File71
│       File72
│       File73
│       File74
│       File75
│       File76
│       File77
│       File78
│       File79
│
├───Folder8
│       File81
│       File82
│       File83
│       File84
│       File85
│       File86
│       File87
│       File88
│       File89
│
└───Folder9
        File91
        File92
        File93
        File94
        File95
        File96
        File97
        File98
        File99

..输出:

C:\UPPERFOLDER
│   Folder1_File11
│   Folder1_File12
│   Folder1_File13
│   Folder1_File14
│   Folder1_File15
│   Folder1_File16
│   Folder1_File17
│   Folder1_File18
│   Folder1_File19
│   Folder2_File21
│   Folder2_File22
│   Folder2_File23
│   Folder2_File24
│   Folder2_File25
│   Folder2_File26
│   Folder2_File27
│   Folder2_File28
│   Folder2_File29
│   Folder3_File31
│   Folder3_File32
│   Folder3_File33
│   Folder3_File34
│   Folder3_File35
│   Folder3_File36
│   Folder3_File37
│   Folder3_File38
│   Folder3_File39
│   Folder4_File41
│   Folder4_File42
│   Folder4_File43
│   Folder4_File44
│   Folder4_File45
│   Folder4_File46
│   Folder4_File47
│   Folder4_File48
│   Folder4_File49
│   Folder5_File51
│   Folder5_File52
│   Folder5_File53
│   Folder5_File54
│   Folder5_File55
│   Folder5_File56
│   Folder5_File57
│   Folder5_File58
│   Folder5_File59
│   Folder6_File61
│   Folder6_File62
│   Folder6_File63
│   Folder6_File64
│   Folder6_File65
│   Folder6_File66
│   Folder6_File67
│   Folder6_File68
│   Folder6_File69
│   Folder7_File71
│   Folder7_File72
│   Folder7_File73
│   Folder7_File74
│   Folder7_File75
│   Folder7_File76
│   Folder7_File77
│   Folder7_File78
│   Folder7_File79
│   Folder8_File81
│   Folder8_File82
│   Folder8_File83
│   Folder8_File84
│   Folder8_File85
│   Folder8_File86
│   Folder8_File87
│   Folder8_File88
│   Folder8_File89
│   Folder9_File91
│   Folder9_File92
│   Folder9_File93
│   Folder9_File94
│   Folder9_File95
│   Folder9_File96
│   Folder9_File97
│   Folder9_File98
│   Folder9_File99
│
├───Folder1
├───Folder2
├───Folder3
├───Folder4
├───Folder5
├───Folder6
├───Folder7
├───Folder8
└───Folder9

答案 2 :(得分:0)

这在Windows上有点棘手;)

但你可以通过以下方式实现:

在文件夹外创建2个批处理文件,应该“扫描” 第一个应命名为findfiles.bat

forfiles /p %1 /s /c "cmd /c if @isdir==FALSE %~p0\renameit.bat @relpath @file %1 %~p0\"

第二个应命名为renameit.bat

@echo off
set oldname=%1
set oldname=%oldname:.\=%
set oldname=%oldname:"=%
set tmpname=%2
set newname=%oldname:\=_%
set newname=%newname:"=%

move "%4%3\%oldname%" "%4%3\%newname%"

现在您可以使用主文件夹作为参数执行findfiles.bat。 (所以在你的例子中:图片)

因此,例如,如果您的图片文件夹位于D:\,则在D:\中创建2个批处理文件 然后转到cmd并执行findfiles.bat Pictures

但要小心!首先尝试在真实文件夹的副本上执行此操作!我不想成为一个负责你丢失照片或其他什么的人;)

唯一的缺点是你最终会有文件被移出的空文件夹,但我认为这是可以忍受的东西:)