Deleting a folder via Batch file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
konjis1
Posts: 4
Joined: 02 Jul 2014 13:43

Deleting a folder via Batch file

#1 Post by konjis1 » 16 Jul 2014 09:18

If i want to delete a folder, say it was named "Test Folder", on my H drive.....is this how i would write it because i am not having any luck:


CD H:\
ECHO
forfiles /p "H:\Neals Personal Info" /s /m Test*" /c "cmd /c Del Test*"
pause



Thank you in advance!

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Deleting a folder via Batch file

#2 Post by foxidrive » 16 Jul 2014 09:32

Do you know the location of the folder?

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Deleting a folder via Batch file

#3 Post by Squashman » 16 Jul 2014 09:52

If you know the folder name why are you using a File Mask with a wildcard?
The file mask could also match files so you should also look at using the @ISDIR variable.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Deleting a folder via Batch file

#4 Post by Compo » 16 Jul 2014 09:58

Regardless of the above, there is one thing which appears quite obvious to me...

Do you really want to use a command named FORFILES to work with a DIRECTORY and then use DELETE as opposed to ReMoveDIRectory on it?

konjis1
Posts: 4
Joined: 02 Jul 2014 13:43

Re: Deleting a folder via Batch file

#5 Post by konjis1 » 16 Jul 2014 11:26

There are backup files/folders that are created every night at 11pm, and those files start with the word of "Test". So that is why i was using the wildcard. The location of those files are H:\Neals Personal\. So what i was really trying to do was every night i want to delete a backup folder that is a day old. So you all say i should just use the "DELETE" command? How would i specify the date parameters?

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Deleting a folder via Batch file

#6 Post by Squashman » 16 Jul 2014 11:49

konjis1 wrote:How would i specify the date parameters?

Code: Select all

/D    date          Selects files with a last modified date greater
                    than or equal to (+), or less than or equal to
                    (-), the specified date using the
                    "MM/dd/yyyy" format; or selects files with a
                    last modified date greater than or equal to (+)
                    the current date plus "dd" days, or less than or
                    equal to (-) the current date minus "dd" days. A
                    valid "dd" number of days can be any number in
                    the range of 0 - 32768.
                    "+" is taken as default sign if not specified.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Deleting a folder via Batch file

#7 Post by Compo » 16 Jul 2014 13:25

konjis1 wrote:There are backup files/folders that are created every night at 11pm, and those files start with the word of "Test". So that is why i was using the wildcard.
konjis1 wrote:The location of those files are H:\Neals Personal\. So what i was really trying to do was every night i want to delete a backup folder that is a day old.
Please be more specific, are you deleting files which begin with Test located at "H:\Neals Personal"? or are you removing a directory beginning with Test located at "H:\Neals Personal"? or both of the above?

konjis1
Posts: 4
Joined: 02 Jul 2014 13:43

Re: Deleting a folder via Batch file

#8 Post by konjis1 » 16 Jul 2014 13:30

Well they are folders that have content. So the location is H:\Neals Personal\Test12345, where Test12345 contains the backup content. I am trying to delete the folder Test12345.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Deleting a folder via Batch file

#9 Post by Compo » 16 Jul 2014 15:53

Code: Select all

@Echo Off & SetLocal
For /F "Tokens=*" %%A In ('Dir/B/AD/OD "H:\Neals Personal\Test*"') Do Set _D="%%~fA"
Echo( RD/S/Q %_D%&Pause
Remove the first six and last six characters on the last line if you are happy with the result in the console window!

Post Reply