如何在windows命令中显示以dot开头的文件夹?

时间:2018-01-16 10:06:42

标签: batch-file cmd

  1. 我的操作系统是Windows 7;
  2. 以点(。)开头的文件夹是隐藏文件夹;
  3. 我的目标文件夹中有几个子文件夹,如.android,.cache等。
  4. 我想要的是找到所有文件夹,然后删除它们及其内容。
  5. 我所做的是在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")
    
  6. 任何人都有解决方案吗?

1 个答案:

答案 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

“ zDebug = 1”时输出

;[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"}

“ zDebug = 0”时输出

;[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"}