我所做的是在cmd文件中运行follow命令,但似乎根本无法正常工作:
for /f "delims=" %%i in ('dir "%targetfolder%\\.android" /s /b /a') do (echo "%%i" && rd /s /q "%%i")
for /f "delims=" %%i in ('dir "%targetfolder%\\.cache" /s /b /a') do (echo "%%i" && rd /s /q "%%i")
任何人都有解决方案吗?
答案 0 :(得分:0)
我构建了下面的示例,该示例在我的所有测试中均有效。导致失败的唯一条件是将父目录定位到批处理文件,因为它无法删除当前正在执行的工作进程。
使用“ pushd”解决该问题。并且在任何正常使用中,“ popd”会将工作目录返回到原始位置。
@echo off
rem setlocal EnableDelayedExpansion
set zDebug=0
if %zDebug% NEQ 0 echo ;[i] Execution started from {%cd%}
for %%A in ("c:\temp","c:\temp\zTest-DeleteStuff","c:\temp\zTest-DeleteStuff\.Android","c:\temp\zTest-DeleteStuff\DoNotDeleteThis","c:\temp\zTest-DeleteStuff\DoNotDeleteThis\.OkDeleteThis","c:\temp\zTest-DeleteStuff\.Android\Dir1","c:\temp\zTest-DeleteStuff\.Android\Dir2","c:\temp\zTest-DeleteStuff\.Android\Dir3","c:\temp\zTest-DeleteStuff\.Android\Dir3\.TriggerAlreadyDeletedClause") do (
if exist "%%~A" (
if %zDebug% NEQ 0 (echo ;[i] Directory Already Exists {"%%~A"})
) else (
mkdir "%%~A" && (if %zDebug% NEQ 0 echo ;[i] Directory Created {"%%~A"})
)
)
set "targetfolder=c:\temp\zTest-DeleteStuff"
if %zDebug% NEQ 0 echo ;[i] Changing Dir To Begin Removing .Directories {%targetfolder%}
pushd "%targetfolder%" 2>nul && (
for /f "tokens=*" %%A in ('dir /b /s /ad ".*"') do (
if exist "%%~A" (
rd /s /q "%%~A" >nul 2>&1 && (echo ;[i] Deleted {"%%~A"}) || (echo ;[e] Failed To Delete {"%%~A"})
) else (
echo ;[i] Already Deleted {"%%~A"}
)
)
) || (
echo ;[e] Failed To Change Directory To Target Folder {%targetfolder%}
)
goto :zEOF
:zExitEarly
echo ;[i] Exiting Early...
:zEOF
popd >nul 2>&1 || echo ;[i] Starting Directory No Longer Exists
exit /b
;[i] Execution started from {C:\}
;[i] Directory Already Exists {"c:\temp"}
;[i] Directory Already Exists {"c:\temp\zTest-DeleteStuff"}
;[i] Directory Created {"c:\temp\zTest-DeleteStuff\.Android"}
;[i] Directory Already Exists {"c:\temp\zTest-DeleteStuff\DoNotDeleteThis"}
;[i] Directory Created {"c:\temp\zTest-DeleteStuff\DoNotDeleteThis\.OkDeleteThis"}
;[i] Directory Created {"c:\temp\zTest-DeleteStuff\.Android\Dir1"}
;[i] Directory Created {"c:\temp\zTest-DeleteStuff\.Android\Dir2"}
;[i] Directory Created {"c:\temp\zTest-DeleteStuff\.Android\Dir3"}
;[i] Directory Created {"c:\temp\zTest-DeleteStuff\.Android\Dir3\.TriggerAlreadyDeletedClause"}
;[i] Changing Dir To Begin Removing .Directories {c:\temp\zTest-DeleteStuff}
;[i] Deleted {"c:\temp\zTest-DeleteStuff\.Android"}
;[i] Already Deleted {"c:\temp\zTest-DeleteStuff\.Android\Dir3\.TriggerAlreadyDeletedClause"}
;[i] Deleted {"c:\temp\zTest-DeleteStuff\DoNotDeleteThis\.OkDeleteThis"}
;[i] Deleted {"c:\temp\zTest-DeleteStuff\.Android"}
;[i] Already Deleted {"c:\temp\zTest-DeleteStuff\.Android\Dir3\.TriggerAlreadyDeletedClause"}
;[i] Deleted {"c:\temp\zTest-DeleteStuff\DoNotDeleteThis\.OkDeleteThis"}