1

Hello this is my first post so here goes nothing. I'm currently working on a task with a supervisor where he wants me to create a Batch script that will whip out all the contents in a student (G) drive. When I run this command it only deletes files. Folders and applications do not get removed at all.

This is what I put in my .bat script

forfiles -p "G:\" -s -m *.* /D -0 /C "cmd /c del @path"
Hackoo
  • 18,337
  • 3
  • 40
  • 70
  • Why not simply format the drive? – Magoo Oct 03 '19 at 00:41
  • Probably because you use the search mask *.*, most folders probably won't have a period in them. Use the * search mask instead. – Dijkgraaf Oct 03 '19 at 00:59
  • @Magoo Probably because he means a mapped User Drive which is really a folder on a file server somewhere – Ben Personick Oct 03 '19 at 04:15
  • Please read carefully from top to bottom my answer on [How to delete files/subfolders in a specific directory at the command prompt in Windows?](https://stackoverflow.com/a/50656521/3074564) Most contributed solutions fail to delete really __all__ files and folders including hidden files and folders, read-only files, etc. – Mofi Oct 03 '19 at 06:24
  • [format](https://ss64.com/nt/format.html) /Q /X /V:StudDrv /FS:NTFS – Stephan Oct 03 '19 at 07:01
  • @Stephan I suspect G:\ is a network drive. – Gerhard Oct 03 '19 at 07:27
  • What about `rmdir /S /Q "G:\."` and `del /Q "G:\*.*"`? – aschipfl Oct 03 '19 at 12:55
  • If you open up a Command Prompt window and enter `forfiles /?` you'll be able to read its usage information. In that you should see `@isdir`, which should return `TRUE` if the 'filetype' is a directory. `Del` deletes files, and `RD` removes directories, you can type `rd /?` to read its usage information. Armed with that you should be able to adjust your `/C`ommand to use `If`, `@IsDir==TRUE`, `RD`, `Else` and `Del` all together. However, I would advise against using `Forfiles` for this task. – Compo Oct 07 '19 at 13:09

2 Answers2

0

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
Ben Personick
  • 3,074
  • 1
  • 22
  • 29
  • That is a lot of code for such a simple task, is it not? – Gerhard Oct 03 '19 at 07:24
  • @GerhardBarnard I like your answer if . will delete hidden read-only files etc, I usually prefer to delete the root directory and recreate instead od having a separate DEL to handle any file in the root directory so your code seems excellent, and plan to test it myself tomorrow, it would certainly be a rare instance where I prefer to popD and PushD in future, as I generally map my own sym links or junctions instead do using push and pop, as I only bother when running into issues due to path length, but that's really a matter of preference.. For this, it seems a much nicer shortcut! :) – Ben Personick Oct 03 '19 at 07:39
  • 1
    The batch file ignores directories starting with `;` in directory name because of not using `"eol=| delims="`. A directory with a semicolon at beginning of directory name is unusual, but nevertheless possible. And the two `DEL` could be just one `DEL` on using on first one also option `/A` (any attribute) which on being used overrides built-in default `/A-H` (all except hidden attribute). I have explained that all in [How to delete files/subfolders in a specific directory at the command prompt in Windows?](https://stackoverflow.com/a/50656521/3074564) You would just need to read this answer. – Mofi Oct 04 '19 at 05:33
0

How about just doing:

rd G:\. /S /Q

You could also pushd to the dir and then do the delete simply by using && operator to ensure the pushd command completes before executing the rd command.

pushd G:\ && rd . /S /Q
Gerhard
  • 22,678
  • 7
  • 27
  • 43
  • Sorry, but first command line can delete too much. Drive `G:` is a network drive as written by Michael.Panchu which could be mapped for example with `net use G: "\\Server\Share\UserFolder"`. The first command line deletes `"\\Server\Share\UserFolder"` completely if the user has the permission to delete `UserFolder` on this share. But the goal is to delete only the subfolders and the files in `"\\Server\Share\UserFolder"` and not the folder `UserFolder`. The second command line is much better, but misses the command `popd` to switch current directory back to initial current directory. – Mofi Oct 03 '19 at 07:43