尝试批量重命名文件夹

问题描述 投票:0回答:1
SETLOCAL enabledelayedexpansion
(
FOR /f "delims=" %%a IN ('dir /ad /b /S "*-ABC*"') DO (

    REM Get the foldername without the path name and without the extension (i.e. Folder1-ABC)
    SET "eir_foldername=%%~na"
    echo "!eir_foldername!"

    REM Remove "-ABC" from the filename (i.e. Folder1)
    SET "reg_foldername=!eir_foldername:-EIR=!" 
    echo "%%~dpa" "!reg_foldername!"    

    rem for /D /r %%~dpa!reg_foldername! in (*) do rmdir /q /s %%~dpa!reg_foldername!
    for /D /r %%i in (%%~dpa!reg_foldername!) do echo %%i

    Rename "%%~dpa" "!reg_foldername!"
    REM Rename the file (with full path and filename) to the new filename (that does not have "-Eir" in it + the original extension)   
)
)

echo Successfully renamed!
rem Endlocal

Endlocal

pause

:end

我有一个路径,其中包含多个具有相同名称的文件夹。例如,在pash C:/Test/First/Second/中,我有文件夹Folder1Folder1-ABCFolder2Folder2-ABC

[我想做的是删除没有-ABC的文件夹,如果文件夹名称包含文件夹名称,例如-ABC应该变成Folder1-ABC,则删除Folder1

windows batch-file cmd
1个回答
0
投票
@echo off

for /f "delims=" %%A in ('dir /ad /b /s ^| findstr /i /v "\-ABC$"') do (
    if /i exist "%%~A-ABC" (
        echo rd /q /s "%%~A"
        echo move "%%~A-ABC" "%%~A"
    )
)

pause

具有成对的文件夹名称,即namename-ABC,您正在寻找后者的名称,并从名称中删除-ABC。附加-ABC可能比删除-ABC容易。

findstr/v一起将由于使用-ABC锚点而在行尾打印与$不匹配的行。现在,循环中处理的每个路径都将-ABC附加到%%A,以检查配对名称是否确实存在。如果是这样,则将执行rdmove命令。

echord前面的move命令用于测试。如果满意,请删除echo命令。

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