Solved

Windows file deletion through batch job.

Posted on 2013-11-15
11
507 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
[X]
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
  • 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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 54

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 54

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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Sometimes drives fill up and we don't know why.  If you don't understand the best way to use the tools available, you may end up being stumped as to why your drive says it's not full when you have no space left!  Here's how you can find out...
This tutorial will walk an individual through the steps necessary to configure their installation of BackupExec 2012 to use network shared disk space. Verify that the path to the shared storage is valid and that data can be written to that location:…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

740 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