【问题标题】:How to change all foldernames with a point to a space using a bat file如何使用 bat 文件将所有文件夹名称更改为指向空格
【发布时间】:2014-12-04 10:39:02
【问题描述】:

我的驱动器上有大量文件夹 (+20.000),我喜欢按名称排序。 但有些文件夹使用“。” (点)而不是“”(空格)。

所以我想创建一个 .bat 文件,并将其放入该特定文件夹(其中包含 +20k 个文件夹)。

执行时,它会将文件夹名称中的所有 de 点更改为空格。

一些困难:

  • 不是文件(显然)。
  • 而不是下面的文件夹。

f.e.

之前

  • 电影
    • 钢铁侠2.(2018)
    • 钢铁侠 1 (2018)
    • unharmed.file.mp3
    • change.bat

之后

  • 电影
    • 钢铁侠 1 (2018)
    • 钢铁侠2 (2018)
    • unharmed.file.mp3
    • change.bat

有人可以帮我解决这个问题或为我指明正确的方向吗?谢谢。

【问题讨论】:

    标签: batch-file cmd rename space


    【解决方案1】:
    @echo off
        setlocal enableextensions disabledelayedexpansion
    
        Rem Check for presence of parameter
        if not exist "%~f1" goto :eof
    
        Rem For each folder under the indicated one
        for /d %%a in ("%~f1\*") do (
            Rem Retrieve folder name and extension
            set "folderName=%%~nxa"
            Rem Delayed expansion needed to access the folderName variable
            setlocal enabledelayedexpansion
            Rem Get the folderName value without dots and store it in the
            Rem for replaceable parameter
            for %%b in ("!folderName:.= !") do (
                Rem disable the activation of delayed expansion
                endlocal 
                Rem If the target name does not exist, do the rename
                if not exist "%%~dpa\%%~b" (
                    echo ren "%%~fa" "%%~b"
                )
                Rem The variable will be used as a switch to determine
                Rem if the outer setlocal has been ended with the
                Rem inner endlocal
                set "folderName="
            )
            Rem If the last setlocal has not been ended, end it
            if defined folderName endlocal
        )
    

    没有cmets

    @echo off
        setlocal enableextensions disabledelayedexpansion
    
        if not exist "%~f1" goto :eof
    
        for /d %%a in ("%~f1\*") do (
            set "folderName=%%~nxa"
            setlocal enabledelayedexpansion
            for %%b in ("!folderName:.= !") do (
                endlocal 
                if not exist "%%~dpa\%%~b" (
                    echo ren "%%~fa" "%%~b"
                )
                set "folderName="
            )
            if defined folderName endlocal
        )
    

    【讨论】:

      【解决方案2】:

      这是一个简单的解决方案,只需在要修复名称的目录中运行它即可。

      @echo off
      setlocal ENABLEDELAYEDEXPANSION
      for /d %%a in (*) do (
          set dirName=%%a
          echo ren "%%a" "!dirName:.= !"
      )
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-02-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-01
        相关资源
        最近更新 更多