Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Delete .bat file

Posted on 2008-11-19
52
Medium Priority
?
1,281 Views
Last Modified: 2012-08-13
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
0
Comment
Question by:asp_net2
  • 23
  • 22
  • 3
  • +2
52 Comments
 
LVL 2

Expert Comment

by:anauj0101
ID: 22998164
erase C:\New Folder\*.*
0
 
LVL 8

Expert Comment

by:CoyotesIT
ID: 22998165
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
 
LVL 2

Expert Comment

by:anauj0101
ID: 22998186
O, and you may need erase "C:\New Folder\*.*" because of the space in New folder
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 4

Author Comment

by:asp_net2
ID: 22998291
Hi CoyotesIT,

Can you brake down what your saying. Thanks in advance!!
0
 
LVL 4

Author Comment

by:asp_net2
ID: 22998324
Hi anauj0101,

How can i get your example to work without the prompt.
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 22998389
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
 
LVL 8

Expert Comment

by:CoyotesIT
ID: 22998934
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
 
LVL 67

Expert Comment

by:sirbounty
ID: 22999142
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
 
LVL 8

Expert Comment

by:CoyotesIT
ID: 22999246
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
 
LVL 67

Expert Comment

by:sirbounty
ID: 22999268
...perhaps the next version of rmdir, eh? :^)
0
 
LVL 4

Author Comment

by:asp_net2
ID: 23004474
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
 
LVL 67

Expert Comment

by:sirbounty
ID: 23004516
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
 
LVL 4

Author Comment

by:asp_net2
ID: 23004623
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
 
LVL 67

Expert Comment

by:sirbounty
ID: 23004674
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
 
LVL 4

Author Comment

by:asp_net2
ID: 23004693
awesome, do i have to add something such as an interval so that it runs after the delete and not before?
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 23004708
nope - it's all sequenced execution...nothing executes until the prior command is finished.
0
 
LVL 4

Author Comment

by:asp_net2
ID: 23004795
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
 
LVL 67

Expert Comment

by:sirbounty
ID: 23004828
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
 
LVL 4

Author Comment

by:asp_net2
ID: 23004968
still no luck :(

When i run the code i see a DOS command prompt starting at the first command.
0
 
LVL 4

Author Comment

by:asp_net2
ID: 23005006
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
 
LVL 67

Expert Comment

by:sirbounty
ID: 23005033
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
 
LVL 67

Expert Comment

by:sirbounty
ID: 23005129
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
 
LVL 4

Author Comment

by:asp_net2
ID: 23005226
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
 
LVL 4

Author Comment

by:asp_net2
ID: 23005337
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
 
LVL 67

Expert Comment

by:sirbounty
ID: 23005340
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
 
LVL 4

Author Comment

by:asp_net2
ID: 23005415
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
 
LVL 67

Expert Comment

by:sirbounty
ID: 23005855
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
 
LVL 4

Author Comment

by:asp_net2
ID: 23005947
okay i get a message in the cmd prompt that says:

the system cannot find the path specified
del *.* /q
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 23006317
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
 
LVL 4

Author Comment

by:asp_net2
ID: 23006594
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
 
LVL 67

Expert Comment

by:sirbounty
ID: 23006614
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
 
LVL 4

Author Comment

by:asp_net2
ID: 23006656
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
 
LVL 67

Expert Comment

by:sirbounty
ID: 23006711
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
 
LVL 4

Author Comment

by:asp_net2
ID: 23006777
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
 
LVL 67

Expert Comment

by:sirbounty
ID: 23006809
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
 
LVL 4

Author Comment

by:asp_net2
ID: 23006849
Okay vbscript it is :)

How would i create this using the GUI Scheduled Task Wizard?
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 23006869
Use cscript as the filename and for the parameters point to the full path to the script.
0
 
LVL 4

Author Comment

by:asp_net2
ID: 23006936
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
 
LVL 67

Expert Comment

by:sirbounty
ID: 23006958
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
 
LVL 4

Author Comment

by:asp_net2
ID: 23006992
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
 
LVL 67

Expert Comment

by:sirbounty
ID: 23007015
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
 
LVL 4

Author Comment

by:asp_net2
ID: 23007052
\\ 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
 
LVL 67

Expert Comment

by:sirbounty
ID: 23007070
Odd.
Can you try using the UNC instead of the mapped drive?
0
 
LVL 4

Author Comment

by:asp_net2
ID: 23007091
sorry, what does UNC mean?
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 23007109
Oops - use the complete path to the network location.
I.e.
\\servername\sharepath\folder
instead of
S: which is mapped to \\servername\sharepath
0
 
LVL 21

Expert Comment

by:AmazingTech
ID: 23011034
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
 
LVL 4

Author Comment

by:asp_net2
ID: 23028169
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
 
LVL 67

Expert Comment

by:sirbounty
ID: 23032507
Can you post the version you're using?
0
 
LVL 4

Author Comment

by:asp_net2
ID: 23034193
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
 
LVL 67

Expert Comment

by:sirbounty
ID: 23042210
There doesn't seem to be a reference to HISTDIR - is that perhaps erroring from your SERVER.exe app?
0
 
LVL 21

Accepted Solution

by:
AmazingTech earned 2000 total points
ID: 23043098
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
 
LVL 4

Author Closing Comment

by:asp_net2
ID: 31518442
thank you for all your help, i was busy and didn't get a chance to reply back. thanks again!!!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month21 days, 7 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question