Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Windows file deletion through batch job.

Posted on 2013-11-15
11
506 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 84

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 84

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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

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 84

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 84

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

Suggested Solutions

Title # Comments Views Activity
Application Crash 2 25
Setting up RAID5 on HP ProLiant DL380G5 15 70
How to run a process periodically from the server 2 14
Domain trust created by PDC name 6 31
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…
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 tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

791 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