如何在目录树中递归替换所有文件和文件夹名称中的下划线的所有空格?

问题描述 投票:0回答:1

How to replace all spaces by underscores in all file names of a folder? 它包含通过用下划线替换空格来重命名文件夹中具有一个或多个空格的文件夹中的所有文件的解决方案。

如何递归重命名整个路径,包括该文件的每个目录名,而不仅仅是文件名本身?

例如,当前目录是C:\example,它包含:

C:\example\some stupid file path with whitespace\my file.exe
C:\example\another stupid whitespaced dir\another file.exe

文件夹和文件应重命名为:

C:\example\some_stupid_file_path_with_whitespace\my_file.exe
C:\example\another_stupid_whitespaced_dir\another_file.exe

如何重命名这个递归文件和文件夹?

windows batch-file cmd directory rename
1个回答
1
投票

以下是此任务的批处理代码:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "StartFolder=C:\example"

cd /D %SystemRoot%
set "RenameError="

rem Rename all files containing at least one space character in file name.
for /F "delims=" %%I in ('dir "%StartFolder%\* *" /A-D /B /S 2^>nul') do call :RenameFile "%%I"

rem Rename all folders containing at least one space character in folder name.
for /F "delims=" %%I in ('dir "%StartFolder%\* *" /AD /B /S 2^>nul') do call :RenameFolder "%%I"

if defined RenameError echo/& pause
rem Restore initial environment and exit this batch file.
endlocal
goto :EOF


:RenameFile
set "NewFileName=%~nx1"
set "NewFileName=%NewFileName: =_%"

set "FileAttributes=%~a1"
if "%FileAttributes:~3,1%" == "h" %SystemRoot%\System32\attrib.exe -h %1

ren %1 "%NewFileName%" 2>nul
if errorlevel 1 goto ErrorFileRename

if "%FileAttributes:~3,1%" == "h" %SystemRoot%\System32\attrib.exe +h "%~dp1%NewFileName%"
goto :EOF

:ErrorFileRename
echo Error renaming file %1
set "RenameError=1"
if "%FileAttributes:~3,1%" == "h" %SystemRoot%\System32\attrib.exe +h %1
goto :EOF


:RenameFolder
set "NewFolderName=%~nx1"
set "NewFolderName=%NewFolderName: =_%"

set "FolderPath=%~dp1"
if not exist "%FolderPath%" set "FolderPath=%FolderPath: =_%"
set "FullFolderName=%FolderPath%%~nx1"
if not exist "%FullFolderName%\" set "RenameError=1" & goto :EOF

for %%J in ("%FullFolderName%") do set "FolderAttributes=%%~aJ"
if "%FolderAttributes:~3,1%" == "h" %SystemRoot%\System32\attrib.exe -h "%FullFolderName%"

ren "%FullFolderName%" "%NewFolderName%" 2>nul
if errorlevel 1 goto ErrorFolderRename

if "%FolderAttributes:~3,1%" == "h" %SystemRoot%\System32\attrib.exe +h "%FolderPath%%NewFolderName%"
goto :EOF

:ErrorFolderRename
echo Error renaming folder "%FullFolderName%"
set "RenameError=1"
if "%FolderAttributes:~3,1%" == "h" %SystemRoot%\System32\attrib.exe +h "%FullFolderName%"
goto :EOF

它也适用于隐藏文件和文件夹以及包含完整限定文件/文件夹名称中的感叹号的文件和文件夹。

如果由于以下原因重命名文件或文件夹失败,批处理文件将输出错误消息:

  1. 同一文件夹中的现有文件/文件夹已具有新文件/文件夹名称。
  2. 使用的用户帐户没有重命名文件/文件夹所需的权限。
  3. 要重命名的文件夹是任何正在运行的进程的当前目录。
  4. 要重命名的文件由正在运行的进程打开,该进程具有由进程设置的文件访问权限,从而阻止重命名打开的文件。
  5. 要重命名的文件夹或其子文件夹之一的文件由正在运行的进程打开,该进程具有由进程设置的文件访问权限,防止重命名或删除文件,这也会阻止重命名或删除此打开文件的路径中的任何文件夹。

如果发生任何文件/文件夹重命名错误,批处理文件将在结束时暂停,以便用户双击批处理文件可以读取错误消息。如果在执行期间没有发生重命名错误,则没有暂停。

批处理文件不会尝试使用名称中的空格重命名尽可能多的文件夹。因此,例如,如果无法重命名级别2上且名称中至少有一个空格的文件夹,则不会重命名在级别4及以下名称中具有空格的所有子文件夹。批处理文件仅包含用于处理以下情况的代码,即之前可以重命名包含当前文件夹的一个或多个空格的路径中的任何文件夹。在这种情况下,它尝试重命名当前子文件夹,文件夹名称中包含空格。

批处理文件将运行命令进程的当前目录临时设置为Windows目录,以确保当前命令进程不会阻止重命名文件夹树中的文件夹。

批处理文件不应包含文件名中的空格,并且不应位于分配给环境变量StartFolder的文件夹的子文件夹之一中。在启动文件/文件夹重命名过程之前,没有添加任何代码来验证这两个要求。

要了解使用的命令及其工作方式,请打开命令提示符窗口,执行以下命令,并完全阅读为每个命令显示的所有帮助页面。

  • attrib /?
  • call /?
  • dir /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • pause /?
  • ren /?
  • set /?
  • setlocal /?

也可以看看:

重定向运算符>必须在两个FOR命令行中使用插入符号^进行转义,以便在执行命令FOR时执行嵌入式dir命令行并在后台启动的单独命令进程中执行命令FOR时将其解释为文字字符。 cmd.exe /C

© www.soinside.com 2019 - 2024. All rights reserved.