Solved

Windows file deletion through batch job.

Posted on 2013-11-15
11
504 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 83

Expert Comment

by:oBdA
ID: 39651621
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
ID: 39651665
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 83

Expert Comment

by:oBdA
ID: 39651679
"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
ID: 39651754
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 83

Accepted Solution

by:
oBdA earned 500 total points
ID: 39651778
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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

Author Comment

by:cj_cb
ID: 39651800
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
ID: 39651907
Figured it out.

Thanks a million!!!!
0
 

Author Comment

by:cj_cb
ID: 39651914
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 52

Expert Comment

by:Bill Prew
ID: 39652075
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 83

Expert Comment

by:oBdA
ID: 39652116
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 52

Expert Comment

by:Bill Prew
ID: 39652141
Or I guess this approach...

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

Open in new window

~bp
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Many companies are making the switch from Microsoft to Google Apps (https://www.google.com/work/apps/business/). Use this article to learn more about what Google Apps has to offer and to help if you’re planning on migrating to Google Apps. It is …
The new Microsoft OS looks great, is easier than ever to upgrade to, it is even free.  So what's the catch?  If you don't change the privacy settings, Microsoft will, in accordance with the (EULA) you clicked okay to without reading, collect all the…
This tutorial will walk an individual through locating and launching the BEUtility application to properly change the service account username and\or password in situation where it may be necessary or where the password has been inadvertently change…
This tutorial will show how to configure a new Backup Exec 2012 server and move an existing database to that server with the use of the BEUtility. Install Backup Exec 2012 on the new server and apply all of the latest hotfixes and service packs. The…

911 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

19 Experts available now in Live!

Get 1:1 Help Now