Solved

Windows file deletion through batch job.

Posted on 2013-11-15
11
498 Views
Last Modified: 2013-11-15
I need a batch file that will delete the contents of a directory without deleting the directory itself. it is on a NFS share so recreating the directory messes up permissions.

I need the contents of d:\share deleted but save directory structure
0
Comment
Question by:cj_cb
  • 5
  • 4
  • 2
11 Comments
 
LVL 82

Expert Comment

by:oBdA
Comment Utility
Try this; it's in test mode and will only display the delete commands it would normally run; remove the two uppercase ECHOs to run it for real (note: this will delete subdirectories of the folder given, sounds like you only want to keep the root):
@echo off
set Folder=D:\Share
REM Delete all subfolders in the given directory:
for /d %%a in ("%Folder%\*.*") do ECHO rd /s /q "%%a"
REM Delete all remaining files in the root of the given directory:
ECHO del /f /q "%Folder%"

Open in new window

0
 

Author Comment

by:cj_cb
Comment Utility
This is close but its looking for a yes or no and does not delete.

C:\batch>del *.* /s
C:\batch\*.*, Are you sure (Y/N)? n
C:\batch>loc_delegated.bat
0
 
LVL 82

Expert Comment

by:oBdA
Comment Utility
"del" wants a "/q" to suppress warnings when using wildcards.
del /s /q "C:\SomeFolder" will delete all files in all subfolders of SomeFolder, but will leave all folders.
You current command (del *.* /s) would delete all files in and under "C:\batch"
0
 

Author Comment

by:cj_cb
Comment Utility
Thanks for the response, what i showed is when i try to test the batch you sent me. it is only the output. the files are not deleteing and i believe because it is the yes/No defaulting to no
0
 
LVL 82

Accepted Solution

by:
oBdA earned 500 total points
Comment Utility
What you showed in http:#a39651665 is unrelated to my script; there is no "del *.* /s" in my script.
The script is not deleting anything because, as I mentioned in my answer, it's in test mode and will only display the rd/del commands it would normally run until you remove the two uppercase "ECHO"s.
This is a live version that will actually delete, so handle with care:
@echo off
set Folder=D:\Share
REM Delete all subfolders in the given directory:
for /d %%a in ("%Folder%\*.*") do rd /s /q "%%a"
REM Delete all remaining files in the root of the given directory:
del /f /q "%Folder%"

Open in new window

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:cj_cb
Comment Utility
oBdA,

thanks for the removal script, but the actual path is d:\share\files that i need to delete and leave the "\share\files" directory intact would it be just to add the path to the "Set Folder"?
0
 

Author Comment

by:cj_cb
Comment Utility
Figured it out.

Thanks a million!!!!
0
 

Author Comment

by:cj_cb
Comment Utility
final version


@echo off
set Folder=D:\Share\
REM Delete all subfolders in the given directory:
for /d %%a in ("%Folder%\files\*.*") do rd /s /q "%%a"
REM Delete all remaining files in the root of the given directory:
del /f /q "%Folder%\files"
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
Just for future reference, it's even a bit easier than that. You can use a DOS trick of changing to the base directory first, and then deleting it.  Since it's the current directory, everything underneath it gets deleted, but not the base folder.  So this would work:

@echo off
pushd D:\Share
rd /s /q .
popd

Open in new window

~bp
0
 
LVL 82

Expert Comment

by:oBdA
Comment Utility
You should only do that if you're testing pushd's (or cd's) errorlevel.
If you try to delete an absolute path that doesn't exist, all that happens is an error message.
But if you pushd/cd into a path that doesn't exist (typos happen), and then continue to delete, you're eradicating the current folder ...
@echo off
pushd D:\Share || goto :eof
rd /s /q .
popd

Open in new window

0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
Or I guess this approach...

@echo off
pushd D:\Share
rd /s /q D:\Share
popd

Open in new window

~bp
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

You might have come across a situation when you have Exchange 2013 server in two different sites (Production and DR). After adding the Database copy in ECP console it displays Database copy status unknown for the DR exchange server. Issue is strange…
OfficeMate Freezes on login or does not load after login credentials are input.
This tutorial will show how to configure a single USB drive with a separate folder for each day of the week. This will allow each of the backups to be kept separate preventing the previous day’s backup from being overwritten. The USB drive must be s…
This tutorial will walk an individual through setting the global and backup job media overwrite and protection periods in Backup Exec 2012. Log onto the Backup Exec Central Administration Server. Examine the services. If all or most of them are stop…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now