How to delete all files and directories with an exception

后端 未结 2 919
一生所求
一生所求 2021-01-28 14:40

I have a few files and directories, (all directories contain files):

C:\\ABC
│   file1.txt
│
├───folder1
│       oneormorefiles.ext
│
├───folder2
│              


        
相关标签:
2条回答
  • 2021-01-28 15:21

    I would suggest this as a possibile alternative to using FOR loop and/or a mixture of DEL and RD:

    MD "%TEMP%\$_.dummy"&&ROBOCOPY "%TEMP%\$_.dummy" "C:\abc" /E /XD logs /PURGE>NUL 2>&1&RD "%TEMP%\$_.dummy"
    
    0 讨论(0)
  • 2021-01-28 15:27

    I would do it the following way:

    rem // Change to the target root directory:
    pushd "C:\ABC" && (
        rem // Loop over all immediate sub-directories:
        for /F "delims= eol=|" %%F in ('dir /B /A:D "*"') do (
            rem // Remove sub-directory tree unless name is `log`:
            if /I not "%%F" == "logs" rd /S /Q "%%F"
        )
        rem // Delete files located in the root directory:
        del /A /F /Q "*.*"
        rem // return to the original directory:
        popd
    )
    
    0 讨论(0)
提交回复
热议问题