Delete .bat file

Hello,

I'm looking for a way to create a .bat (batch file) to remove everything listed within the following location below. So in other words i want to keep the folder New Folder but want to remove all files and subfolders inside it.

C:\New Folder
LVL 4
asp_net2Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

anauj0101Commented:
erase C:\New Folder\*.*
0
CoyotesITCommented:
cd /d "C:\New Folder"

del /f /q *
rmdir /s /q *



this should delete all files/folders without prompting in that directory... be careful. ;)
0
anauj0101Commented:
O, and you may need erase "C:\New Folder\*.*" because of the space in New folder
0
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

asp_net2Author Commented:
Hi CoyotesIT,

Can you brake down what your saying. Thanks in advance!!
0
asp_net2Author Commented:
Hi anauj0101,

How can i get your example to work without the prompt.
0
sirbountyCommented:
This should do it...


@echo off
cd /d "c:\new folder"
for /f %%a in ('dir /ad /b') do rmdir /s /q "%%a"
del *.* /q

Open in new window

0
CoyotesITCommented:
I gave you a batch file example. You can take that and just copy/paste into a .bat and run it from anywhere on the system.

The cd /d is just if you are starting off on another drive/directory and change your working directory into C:\New Folder

The del /f /q * deletes all files in that folder

and rmdir /s /q * deletes any directories and sub-folders/files in those directories

the /q options to each are suppressing a confirmation dialog for deleting the files.

:: BEGIN COPY
@echo off
cd /d "C:\New Folder"

del /f /q *
rmdir /s /q *
::END COPY

Place that in a delete.bat file on your desktop for example and run it. Anything in the C:\New Folder will be deleted and the New Folder itself will remain.

Another way of doing this would be to just delete the C:\New Folder

rmdir /s /q "C:\New Folder"
:: Recreate it
mkdir "C:\New Folder"


0
sirbountyCommented:
I don't believe rmdir works with wildcards...thus rmdir /s /q * will error.
You could certainly wipe out the folder and reproduce it, but that depends on security permissions on the folder - they might need to be reapplied as well.
My batch script iterates all subfolders, passing the folder name to the rmdir command and then deletes all files in the folder as well...
0
CoyotesITCommented:
sirbounty, yes sorry you are right.

the way around this, which will error out on the "New Folder" since it is in use during the script is

@echo off
cd /d "C:\New Folder"
rmdir /s /q .  >NUL 2>&1

this removes the need for del /f /q

and passes the expected error to >NUL

both solutions will work for your need.
@echo off
cd /d "C:\New Folder"
rmdir /s /q .  >NUL 2>&1

Open in new window

0
sirbountyCommented:
...perhaps the next version of rmdir, eh? :^)
0
asp_net2Author Commented:
sirbounty,

Thank you and can you let me know if your own words what everything means. I will award you all points once you can explain so that i can understand. Thanks in advance!!
0
sirbountyCommented:
Broken down line-by-line,
#1 indicates to simply suppress messages to the screen.
#2 states to change to "new folder" (enclosed in quotes because of the space) on the root of C (the /d indicates that the drive should also be changed if the current path is on any other drive).
#3 states to setup a loop through all folders returned from a dir command.  /ad parameter shows only directories and /b shows the basic information only - no header/details/sizes, etc - just the folder name.
With each pass - i.e, each folder name found, that folder is assigned to variable %%a (again, enclosed in quotes, in case it has a space in the name).  The variable %%a is then issued against the rmdir command with the /s parameter to remove all subfolders as well, and the /q parameter to keep quiet while processing (in other words - don't nag you about confirming the deletion action).
Finally, line #4 indicates to delete all files (*.*), also using the /q (quiet) switch.

The problem with spaces is that typically many commands will recognize the subsequent path of the folder name that contains spaces as an additional parameter(s).  Not all commands are affected by this, but to be on the safe side, and avoid syntax errors, it's good practice to enclose all folder paths inside quote marks.

~sb

0
asp_net2Author Commented:
GREAT!!

I have one other question related to this but if you like i can create another post and let you know the URL to it. I also need to be able to run an .exe after the Delete command runs but withing the same .bat file. Is this possible??
0
sirbountyCommented:
Sure, just drop this line at the end of it (update the exe as needed)
start "c:\your folder\filename.exe"

Open in new window

0
asp_net2Author Commented:
awesome, do i have to add something such as an interval so that it runs after the delete and not before?
0
sirbountyCommented:
nope - it's all sequenced execution...nothing executes until the prior command is finished.
0
asp_net2Author Commented:
okay, i copied what you supplied for the .exe and added to what you provided but when i now run the .bat file it stops at the first command within the ms DOS Prompt. Not sure why i will send you what i have.

@echo off
cd /d "c:\new folder"
for /f %%a in ('dir /ad /b') do rmdir /s /q "%%a"
del *.* /q
start  "m:\new folder\file.exe"
0
sirbountyCommented:
I think this should fix it...the addition of ~f to the %%a variable indicates to reference the <f>ull path to the folder...
otherwise, remove the @echo off line to allow it to show you what it's doing.


@echo off
cd /d "c:\new folder"
for /f %%a in ('dir /ad /b') do rmdir /s /q "%%~fa"
del *.* /q
start  "m:\new folder\file.exe"

Open in new window

0
asp_net2Author Commented:
still no luck :(

When i run the code i see a DOS command prompt starting at the first command.
0
asp_net2Author Commented:
oh no. i ran that file on my desktop and somehow it removed my files on my desktop. is there a way to get those back?
0
sirbountyCommented:
I don't see how that could happen - if the 2nd line states to change to c:\new folder?
There are some undelete programs that may - but you need to stop what you're doing and try those before any other disk activity occurs...
0
sirbountyCommented:
remove the echo off line and see where its breaking down...
REM @echo off
cd /d "c:\new folder"
pause
for /f %%a in ('dir /ad /b') do (
 rmdir /s /q "%%~fa"
 pause
)
del *.* /q
pause
start  "m:\new folder\file.exe"

Open in new window

0
asp_net2Author Commented:
very odd. it asked me to click a button to continue but then once i clicked to continue it ran the file with no problem but removed my file from my desktop :( VERY ODD!!
0
asp_net2Author Commented:
it did it's job for deleting on the delete part and then ran the .exe part fine without removing data there but only removed data on my desktop.
0
sirbountyCommented:
Oh...I thought you were running this from a command-line.
That would be the problem - clicking the file to open it would probably be the reason it removed your desktop.
You'll need to run it from a command-line:  Start->Run->cmd <Enter>
Then type the name of the batch file to launch it.

Were you able to find an undelete program?  Snapfiles.com has a very good one (free).
0
asp_net2Author Commented:
No biggy, i just need a way to double click on the file to do the Delete and run the Executeable. Is there a way to do this instead of running the .bat file from cmd prompt?
0
sirbountyCommented:
Try this version - I've added echo's there so that nothing's deleted until you're sure it's working as expected...
@echo off
set fld="C:\New folder"
for /f %%a in ('dir %fld% /ad /b') do (
  echo rmdir /s /q "%%~fa"
 pause
)
echo del *.* /q
pause
start  "m:\new folder\file.exe"

Open in new window

0
asp_net2Author Commented:
okay i get a message in the cmd prompt that says:

the system cannot find the path specified
del *.* /q
0
sirbountyCommented:
did you remove the echo?
at any rate, change
echo del *.* /q
to read
echo del %fld%\*.* /q
to ensure it's the correct folder...
0
asp_net2Author Commented:
Okay, when i first run this .bat file it prompts me to Press any key. Then when i press a key it goes to a command prompt.
0
sirbountyCommented:
This version is non-destructive and should only report what 'would' be deleted.
The attached version, if this one works for you, will delete the files/folders...and start the exe file.

@echo off
set fld="C:\New folder"
for /f %%a in ('dir %fld% /ad /b') do (
  echo rmdir /s /q "%%~fa"
)
echo del *.* /q
REM start  "m:\new folder\file.exe"
@echo off
set fld="C:\New folder"
for /f %%a in ('dir %fld% /ad /b') do (
  rmdir /s /q "%%~fa"
)
del *.* /q
start  "m:\new folder\file.exe"

Open in new window

0
asp_net2Author Commented:
Sorry, not sure which one to use. I used the first one above the code in the textbox and ran it and it just flashed the DOS prompt without displaying anything. Now should i use the code within the textbox?
0
sirbountyCommented:
What OS are you using?
And would you be opposed to a vbscript?
'Save as CleanNewFolder.vbs or something similar
Folder = "F:\New Folder"
AppToRun = "M:\New Folder\file.exe"
 
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objShell : Set objShell = CreateObject("Wscript.Shell")
 
Set objFolder = objFSO.GetFolder(Folder)
For Each subfolder in objFolder.SubFolders
  objFSO.DeleteFolder subfolder
Next
 
For Each file in objFolder.Files
  objFSO.DeleteFile file
Next
 
objShell.Run AppToRun

Open in new window

0
asp_net2Author Commented:
i would probally prefer to use a .bat file or whatever you think is better to use. after this i have to find a way to run either the .bat or .vbs file every day by itself.
0
sirbountyCommented:
With that vbscript, simply run

schtasks /create /ru system /sc daily /st 22:00:00 /tn FolderCleanup /tr "cscript c:\folderclean.vbs"

which will create an automated task to run it every day at 10pm.

If you'd still rather have a bat, I'll keep working on this, but personally would prefer a vbscript if it were me...
0
asp_net2Author Commented:
Okay vbscript it is :)

How would i create this using the GUI Scheduled Task Wizard?
0
sirbountyCommented:
Use cscript as the filename and for the parameters point to the full path to the script.
0
asp_net2Author Commented:
Okay below is the scipt you provided along with the correct file paths. When i double click the .vbs file i recieve the following error below.

Script: c:\documents and settings\user\desktop\ccc.vbs
Line: 7
Char: 1
Error: Path not found
Code: 800A004C
Source: Microsoft VBScript runtime error
0
sirbountyCommented:
and the Folder variable points to your folder?  My test was on F:\New Folder - you'd need to change that to C:\New Folder by your examples...
0
asp_net2Author Commented:
Yes, i did make that change before i ran it. But still gave me the message.

Folder = "S:\HISTORY\SYSTAT"
AppToRun = "M:\LOCALCAI\TOOLS\server.exe"
 
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objShell : Set objShell = CreateObject("Wscript.Shell")
 
Set objFolder = objFSO.GetFolder(Folder)
For Each subfolder in objFolder.SubFolders
  objFSO.DeleteFolder subfolder
Next
 
For Each file in objFolder.Files
  objFSO.DeleteFile file
Next
 
objShell.Run AppToRun
0
sirbountyCommented:
Did you run it via the scheduled task?  By the command I gave (or even the GUI if setup that way), you would only have local access using the default SYSTEM account.  In order to use a mapped drive (which is what this appears to be), you would need to use domain/network credentials to run the task...
On the other hand, if you simply double-clicked the script, then I'm not seeing the problem... :\
0
asp_net2Author Commented:
\\ Did you run it via the scheduled task?
No

\\ mapped drive
Yes, both locations are mapped drives and i doube clicked the .vbs file on my desktop to run to test.

When i double click the file it does display the errors on the previous post 23006936.
0
sirbountyCommented:
Odd.
Can you try using the UNC instead of the mapped drive?
0
asp_net2Author Commented:
sorry, what does UNC mean?
0
sirbountyCommented:
Oops - use the complete path to the network location.
I.e.
\\servername\sharepath\folder
instead of
S: which is mapped to \\servername\sharepath
0
AmazingTechCommented:
Hmm...

If you want to go back to the batch file. Try this.
@echo off
set fld=C:\New folder
for /f "tokens=*" %%a in ('dir "%fld%" /ad /b') do (
  rmdir /s /q "%fld%\%%a"
)
del "%fld%\*.*" /q
start "" "m:\new folder\file.exe"

Open in new window

0
asp_net2Author Commented:
Hi sirbounty,

when i run your .vbs code with the modification to the servername i now receive the following error below.

Environment error: HISTDIR not set.
0
sirbountyCommented:
Can you post the version you're using?
0
asp_net2Author Commented:
Folder = "\\mbsd-web\History\HISTORY\SYSTAT"
AppToRun = "\\mbsd-web\CAI1\LOCALCAI\TOOLS\SERVER.exe"
 
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objShell : Set objShell = CreateObject("Wscript.Shell")
 
Set objFolder = objFSO.GetFolder(Folder)
For Each subfolder in objFolder.SubFolders
  objFSO.DeleteFolder subfolder
Next
 
For Each file in objFolder.Files
  objFSO.DeleteFile file
Next
 
objShell.Run AppToRun
0
sirbountyCommented:
There doesn't seem to be a reference to HISTDIR - is that perhaps erroring from your SERVER.exe app?
0
AmazingTechCommented:
If it is in the Server.exe then running my updated batch file should produce the same error message.
@echo off
set fld=\\mbsd-web\History\HISTORY\SYSTAT
for /f "tokens=*" %%a in ('dir "%fld%" /ad /b') do (
  rmdir /s /q "%fld%\%%a"
)
del "%fld%\*.*" /q
start "" "\\mbsd-web\CAI1\LOCALCAI\TOOLS\SERVER.exe"

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
asp_net2Author Commented:
thank you for all your help, i was busy and didn't get a chance to reply back. thanks again!!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.