Panchu.
In CMD you should use RD
(RMDIR
) command to completely erase a folder and all files underneath it.
However, you will still need to delete the FIles themselves that are in the G:\ Drive since you can not delete the folder there.
This should do the needful:
@(SETLOCAL
ECHO OFF
SET "_Path=G:\"
)
REM Delete all Subdirectories and their File Contents
FOR /F "delims=:" %%_ IN ('
dir /B /A:D "%_Path%*" ') do (
RD /S /Q "%_Path%%%_\")
REM Delete all files in Root Folder:
DEL /F /Q "%_Path%*" & DEL /F /Q /A:H "%_Path%*"
As Mofi noted, Hidden Directories are not shown by default, so I either had to run two loops or use a For Loop, since he brought up a concern about directories with leading or trailing spaced I amended touse DIR instead and parse it with a FOR /F Loop, instead of a FOR /D
However if you don't have those requirements, this is all fluff.
Note to delete files with trailing spaces DEL gets the job done.
Example of Trying to Delete Hidden/System/Read Only directories - these work ( so long as you are in an elevated command prompt of course)
C:\Admin>MD D:\Hidden
C:\Admin>MD D:\System
C:\Admin>MD D:\ReadOnly
C:\Admin>Attrib +H D:\Hidden
C:\Admin>Attrib +S D:\System
C:\Admin>Attrib +R D:\ReadOnly
C:\Admin>for /D %A IN (D:\*) DO @(ECHO.%A)
D:\DCIM
D:\temp
D:\srtFtpLogs
D:\srtFtpData
D:\Bkp
D:\System
D:\ReadOnly
C:\Admin>attrib D:\Hidden
H D:\Hidden
C:\Admin>attrib D:\System
S D:\System
C:\Admin>attrib D:\ReadOnly
R D:\ReadOnly
C:\Admin>RD /S /Q D:\ReadOnly
C:\Admin>RD /S /Q D:\System
C:\Admin>RD /S /Q D:\Hidden
C:\Admin>attrib D:\ReadOnly
File not found - D:\ReadOnly
C:\Admin>attrib D:\System
File not found - D:\System
C:\Admin>attrib D:\ReadOnly
File not found - D:\ReadOnly