Batch file. Delete all files and folders in a directory

前端 未结 14 1246
无人共我
无人共我 2020-11-30 19:20

I want to have a batch file that will delete all the folders and files in my cache folder for my wireless toolkit.

Currently I have the following:

cd         


        
相关标签:
14条回答
  • 2020-11-30 19:35

    Better yet, let's say I want to remove everything under the C:\windows\temp folder.

    @echo off
    rd C:\windows\temp /s /q
    
    0 讨论(0)
  • 2020-11-30 19:39

    del *.* will only delete files, but not subdirectories. To nuke the contents of a directory, you can use this script:

    @echo off
    setlocal enableextensions
    if {%1}=={} goto :HELP
    if {%1}=={/?} goto :HELP
    goto :START
    
    :HELP
    echo Usage: %~n0 directory-name
    echo.
    echo Empties the contents of the specified directory,
    echo WITHOUT CONFIRMATION. USE EXTREME CAUTION!
    goto :DONE
    
    :START
    pushd %1 || goto :DONE
    rd /q /s . 2> NUL
    popd
    
    :DONE
    endlocal
    

    The pushd changes into the directory of which you want to delete the children. Then when rd asks to delete the current directory and all sub directories, the deletion of the sub directories succeed, but the deletion of the current directory fails - because we are in it. This produces an error which 2> NUL swallows. (2 being the error stream).

    0 讨论(0)
  • 2020-11-30 19:40
    set "DIR_TO_DELETE=your_path_to_the_folder"
    
    IF EXIST %DIR_TO_DELETE% (
        FOR /D %%p IN ("%DIR_TO_DELETE%\*.*") DO rmdir "%%p" /S /Q
        del %DIR_TO_DELETE%\*.* /F /Q
    )
    
    0 讨论(0)
  • 2020-11-30 19:44
    @echo off
    @color 0A
    
    echo Deleting logs
    
    rmdir /S/Q c:\log\
    
    ping 1.1.1.1 -n 5 -w 1000 > nul
    
    echo Adding log folder back
    
    md c:\log\
    

    You was on the right track. Just add code to add the folder which is deleted back again.

    0 讨论(0)
  • 2020-11-30 19:46

    Use

    set dir="Your Folder Path Here"
    rmdir /s %dir%
    mkdir %dir%
    

    This version deletes without asking:

    set dir="Your Folder Here"
    rmdir /s /q %dir%
    mkdir %dir%
    

    Example:

    set dir="C:\foo1\foo\foo\foo3"
    rmdir /s /q %dir%
    mkdir %dir%
    

    This will clear C:\foo1\foo\foo\foo3.

    (I would like to mention Abdullah Sabouin's answer. There was a mix up about me copying him. I did not notice his post. I would like to thank you melpomene for pointing out errors!)

    0 讨论(0)
  • 2020-11-30 19:49

    del *.* instead of del *.db. That will remove everything.

    0 讨论(0)
提交回复
热议问题