Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 批处理文件-循环目录并按字母顺序重命名_Batch File - Fatal编程技术网

Batch file 批处理文件-循环目录并按字母顺序重命名

Batch file 批处理文件-循环目录并按字母顺序重命名,batch-file,Batch File,我正在尝试编写一个批处理脚本,查找所有目录和子目录,并将它们重命名为字母表中的单个字母 这是我到目前为止所拥有的 @echo off SET "alfa=0abcdefghijklmnopqrstuvwxyz" SET count=1 FOR /D /r %%G in ("*") DO (call :subroutine "%%G") GOTO :eof :subroutine echo %count%:%1 ::Get the letter from %alfa% at the index

我正在尝试编写一个批处理脚本,查找所有目录和子目录,并将它们重命名为字母表中的单个字母

这是我到目前为止所拥有的

@echo off
SET "alfa=0abcdefghijklmnopqrstuvwxyz"
SET count=1
FOR /D /r %%G in ("*") DO (call :subroutine "%%G")
GOTO :eof

:subroutine
 echo %count%:%1
::Get the letter from %alfa% at the index %count%
::Rename the directory %1 to the single char letter retrieved in line above
 SET /a count+=1
IF %count%==26 (
 SET /a count=1
)
 GOTO :eof
文件夹重命名为什么并不重要,只要它是a)只有一个字母,b)该目录中不存在同名的目录

注意:一个目录中的目录不应超过26个


感谢您的帮助

下面的解决方案假设目录名只有一个字符,该字符是一个字母。如果不是这样,则必须插入其他代码

@echo off
setlocal EnableDelayedExpansion
call :treeProcess
goto :EOF


:treeProcess
set "alfa=0abcdefghijklmnopqrstuvwxyz"

rem Create "name" array with directory names
set "count=0"
for /D %%d in (*) do (
   set "dir=%%d"
   rem If dir name have more than one letter
   if "!dir:~1!" neq "" (
      rem ... insert it in "name" array
      set /A count+=1
      set "name[!count!]=%%d"
   ) else (
      rem ... remove such letter from the alfa string
      set "alfa=!alfa:%%d=!"
   )
)

rem Rename the directories from "name" array to just one letter from alfa string
for /L %%i in (1,1,%count%) do (
   ren "!name[%%i]!" "!alfa:~%%i,1!"
   set "name[%%i]="
)

rem Recursively call this subroutine to process nested directories
for /D %%d in (*) do (
    cd %%d
    call :treeProcess
    cd ..
)
exit /b
@ECHO关闭
SETLOCAL
设置“targetdir=U:\sourcedir”
设置“alphas=a b c d e f g h i j k l m n o p q r s t u v w x y z”
::在子树中的每个dirname前面加上“#”,以避免名称冲突。
对于/f“delims=“%%t IN('dir/s/b/ad”%targetdir%%“^ | sort/r')DO REN“%%t”“#%~nxt”
::重复扫描并重命名
对于/f“delims=“%%t IN('dir/s/b/ad”%targetdir%“^ sort/r'),请执行以下操作:(
设置“重命名=”
对于(%alphas%)中的%%r,如果未定义,则重命名,如果不存在“%%~dpt%%r”REN“%%t”“%%r”并设置“重命名=%%r”
)
后藤:EOF
您需要更改
targetdir
的设置以适应您的环境

首先,重命名子树b中的每个目录,在其名称前加上与任何子目录名称或文件名开头不匹配的字符串。这可以变得智能化,但我只是简单地使用了

通过按相反顺序对名称进行排序,任何子目录名称都会出现在其父目录之前,因此不会出现试图重命名其父目录名称已更改的子目录的情况

然后,使用相同的原则,尝试根据需要将子目录的名称更改为单个字母,方法是检查在该级别是否已经有一个子目录,该子目录中的字符有问题

我建议对一个虚拟子树进行此操作,以评估其适用性