如何在目录树中递归地用所有文件和文件夹名称中的下划线替换所有空格? [英] How to replace all spaces by underscores in all file and folder names recursively in a directory tree?

查看:82
本文介绍了如何在目录树中递归地用所有文件和文件夹名称中的下划线替换所有空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在文件夹的所有文件名中用下划线替换所有空格?
它包含的解决方案可以通过用下划线替换空格来重命名文件夹中具有一个或多个空格的所有文件.

There is How to replace all spaces by underscores in all file names of a folder?
It contains solutions to rename all files in a folder with one or more spaces in file name by replacing the spaces by underscores.

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

How do I recursively rename the whole path including every directory name of that file rather than only the file name itself?

例如,当前目录为 C:\ example ,其中包含:

For example the current directory is C:\example and it contains:

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

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

The folders and files should be renamed to:

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

如何对递归文件和文件夹进行重命名?

How can this recursive file and folder renames be done?

推荐答案

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

Here is a batch code for this task:

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

rem Get the absolute path of the path of the start folder.
for %%I in ("%StartFolder%") do set "StartFolder=%%~fI"
rem Get the length of the absolute path of the start folder.
setlocal EnableDelayedExpansion
set "FolderPath=A!StartFolder!
set "StartPathLength=0"
for /L %%I in (12,-1,0) do set /A "StartPathLength|=1<<%%I" & for %%J in (!StartPathLength!) do if "!FolderPath:~%%J,1!"=="" set /A "StartPathLength&=~1<<%%I"
endlocal & set "StartPathLength=%StartPathLength%"

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"
setlocal EnableDelayedExpansion
set "FolderPath=!FolderPath:~12!"
endlocal & set "FolderPath=%FolderPath%"
if not exist "%StartFolder%%FolderPath%" set "FolderPath=%FolderPath: =_%"
set "FullFolderName=%StartFolder%%FolderPath%%~nx1"
if not exist "%FullFolderName%\" echo Error finding folder "%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

它也适用于隐藏的文件和文件夹,以及包含全限定文件/文件夹名称中带有感叹号的文件和文件夹.

It works also for hidden files and folders and for files and folders containing an exclamation mark in full qualified file/folder name.

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

The batch file outputs an error message if renaming a file or folder failed because of following reasons:

  1. 同一文件夹中的现有文件/文件夹已经具有新的文件/文件夹名称.
  2. 使用的用户帐户没有重命名文件/文件夹所需的权限.
  3. 要重命名的文件夹是任何正在运行的进程的当前目录.
  4. 要重命名的文件是由正在运行的进程打开的,该进程具有该进程设置的文件访问权限,从而防止了重命名打开的文件.
  5. 正在重命名的文件夹中的文件或其子文件夹之一由正在运行的进程打开,该进程具有由该进程设置的文件访问权限,从而防止了文件的重命名或删除,这也导致阻止了此路径中任何文件夹的重命名或删除.打开的文件.
  6. 由于在批处理文件处理整个文件夹树时被删除,因此找不到重命名的文件或文件夹.

如果发生任何文件/文件夹重命名错误,则批处理文件将在末尾暂停,以便用户双击该批处理文件可以读取错误消息.如果在执行过程中没有发生重命名错误,则不会暂停.

The batch file pause at end if any file/folder renaming error occurred so that a user double clicking on the batch file can read the error messages. There is no pause if no renaming error occurred during execution.

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

The batch file does not try to rename as much folders with a space in name as possible. So if for example a folder on level 2 with at least one space in name could not be renamed, all subfolders with a space in name on level 4 and below are also not renamed. The batch file contains only code to handle the situation that none of the folders in path containing one or more spaces to current folder could be renamed before. In this case it tries to rename nevertheless the current subfolder with space in folder name.

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

The batch file sets current directory for the running command process temporarily to Windows directory to make sure that the current command process does not prevent renaming of folder in folder tree.

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

The batch file should not contain a space in file name and should not be in one of the subfolders of the folder assigned to environment variable StartFolder. There is no code added to verify those two requirements before starting the file/folder renaming procedure.

要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读每个命令显示的所有帮助页面.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • attrib/?
  • 呼叫/?
  • dir/?
  • echo/?
  • endlocal/?
  • 用于/?
  • 转到/?
  • 如果/?
  • 暂停/?
  • ren/?
  • 设置/?
  • setlocal/?
  • attrib /?
  • call /?
  • dir /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • pause /?
  • ren /?
  • set /?
  • setlocal /?

另请参阅:

  • Where does GOTO :EOF return to?
  • Using command redirection operators for an explanation of 2>nul.
  • Single line with multiple commands using Windows batch file
  • Function strlen on DosTips of which code was used with small related modifications to get the length of the absolute path of the start folder.

重定向操作符> 必须在两个 FOR 命令行上均使用脱字符号 ^ 进行转义,以在Windows命令时解释为原义字符解释程序在执行命令 FOR 之前处理此命令行,该命令在以 cmd.exe/C dir 命令行>.

The redirection operator > must be escaped with caret character ^ on both FOR command lines to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir command line in a separate command process started in background with cmd.exe /C.

这篇关于如何在目录树中递归地用所有文件和文件夹名称中的下划线替换所有空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆