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/
中,我有文件夹Folder1
,Folder1-ABC
,Folder2
,Folder2-ABC
。
[我想做的是删除没有-ABC
的文件夹,如果文件夹名称包含文件夹名称,例如-ABC
应该变成Folder1-ABC
,则删除Folder1
。
@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
具有成对的文件夹名称,即name
和name-ABC
,您正在寻找后者的名称,并从名称中删除-ABC
。附加-ABC
可能比删除-ABC
容易。
findstr
与/v
一起将由于使用-ABC
锚点而在行尾打印与$
不匹配的行。现在,循环中处理的每个路径都将-ABC
附加到%%A
,以检查配对名称是否确实存在。如果是这样,则将执行rd
和move
命令。
echo
和rd
前面的move
命令用于测试。如果满意,请删除echo
命令。