Solved

Windows file deletion through batch job.

Posted on 2013-11-15
11
505 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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
 

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 53

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 53

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

New Windows 7 Installations take days for Windows-Updates to show up and install. This can easily be fixed. I have finally decided to write an article because this seems to get asked several times a day lately. This Article and the Links apply to…
This article explains how to install and use the NTBackup utility that comes with Windows Server.
This tutorial will walk an individual through the steps necessary to enable the VMware\Hyper-V licensed feature of Backup Exec 2012. In addition, how to add a VMware server and configure a backup job. The first step is to acquire the necessary licen…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

809 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