【问题标题】:How to rename only sub-folders without changing parent folder names如何仅重命名子文件夹而不更改父文件夹名称
【发布时间】:2019-10-15 19:00:39
【问题描述】:

我有一个文件夹,其中包含数百个Name, ID 格式的子文件夹。这些文件夹中的每一个都包含几个子文件夹,其中一些文件夹的名称中包含空格。我想通过用下划线替换空格来仅重命名子文件夹而不是父文件夹,例如C:\Location\John, 1234\My DocumentsC:\Location\John, 1234\My_Documents

我已尝试修改我在此处找到的一段脚本,但它也更改了父文件夹

这是未经编辑的代码:

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

cd /D %SystemRoot%
set "RenameError="

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


: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

正如我所说,每个子文件夹的预期输出应该是 C:\Location\John, 1234\My_Documents 而不是 C:\Location\John, 1234\My Documents。目前使用我的代码,我得到C:\Tydelik\John,_1234\My_Documents

【问题讨论】:

  • 是否总是要替换空间的“叶子”(最低的子文件夹,下面没有其他文件夹)?
  • 是的,没错。
  • 如果您想在C:\Tydelik\Name, ID" 以下的所有子目录级别中用下划线替换所有空格,您可能需要确定每个级别的最低级别分支,并在升级时重命名每个级别。想象一下有C:\Tydelik\John, 1234\My Documents\Work Files,如果你的代码首先重命名My Documents,即C:\Tydelik\John, 1234\My_Documents,那么最初解析的子目录C:\Tydelik\John, 1234\My Documents\Work Files将不复存在,因为它将是C:\Tydelik\John, 1234\My_Documents\Work Files。因此Work Files可能需要先处理。
  • 我只是快速检查了一下,所有文件夹的最低级别是 C:\Tydelik\John, 1234\X 其中 X 是我的文档、图片、视频等。在此之下没有其他文件夹。

标签: windows batch-file


【解决方案1】:

虽然 Compo 的解决方案将文件夹重命名为“depth=2”,但它只重命名了“叶子”(树的最后一个文件夹,“depth=last”)。我保留了您的 call 方法以避免延迟扩展和可能导致的问题(带有 ! 的文件夹名称 - 在您的情况下不太可能,但永远不知道......)

@echo off
setlocal
set "sourcedir=..\..\"
for /f "delims=" %%I in ('dir "%sourcedir%" /ad /b /s 2^>nul') do call :RenameFolder "%%I"
goto :eof

:RenameFolder
dir /ad /b /s "%~1" 2>nul | find /v "" >nul && goto :eof  ::skip renaming, if a subfolder exists
set "leaf=%~nx1"
ECHO ren "%~1" "%leaf: =_%"
goto :eof

注意:出于安全原因,我通过回显它禁用了ren 命令。如果它按预期工作,请删除 ECHO

【讨论】:

    【解决方案2】:

    这是我认为您正在寻找的示例,基于您只对重命名 "C:\Tydelik\Name, ID" 的子目录感兴趣,而不是这些子目录中包含的任何内容:

    @Echo Off
    SetLocal DisableDelayedExpansion
    Set "SourceDir=C:\Tydelik"
    For /F "EOL=?Delims=" %%A In ('Dir /B/AD "%SourceDir%" 2^>NUL'
    )Do Set "TargetDir="&For /F "EOL=?Delims=" %%B In (
        'Dir /B/AD "%SourceDir%\%%A" 2^>NUL') Do (Set "TargetDir=%%B"
        SetLocal EnableDelayedExpansion
        If Not "!TargetDir: =!"=="!TargetDir!" (
            Ren "%SourceDir%\%%A\%%B" "!TargetDir: =_!")
        EndLocal)
    

    【讨论】:

      猜你喜欢
      • 2018-08-21
      • 1970-01-01
      • 1970-01-01
      • 2016-05-07
      • 1970-01-01
      • 1970-01-01
      • 2016-09-11
      • 1970-01-01
      • 2014-08-10
      相关资源
      最近更新 更多