Solved

Batch File to clean contents on a shared Folder.

Posted on 2012-03-18
10
359 Views
Last Modified: 2012-03-19
I need a batch file which can do the job of mapping a shared network folder & clearing the contents of the shared folder.

It would be helpful if you can provide the VB script for the same.
0
Comment
Question by:chinniatluri
  • 6
  • 3
10 Comments
 
LVL 8

Expert Comment

by:PeteEngineer
ID: 37736464
This article helps you to map a network drive :

https://msmvps.com/blogs/kwsupport/archive/2004/11/03/17830.aspx

Clearing its just removing command in the batch..

Good luck !

-Pete
0
 

Author Comment

by:chinniatluri
ID: 37736507
what is the command to remove fiels?
0
 
LVL 83

Expert Comment

by:oBdA
ID: 37736546
Here's a little batch script; if it's really just about deleting the folder, there's no need to map a drive, you can just use a UNC path.
Save this as Whatever.cmd; the script is currently in test mode and will only display the delete commands it would normally run. It will first delete all files in the folder specified, then all subfolders. Remove the uppercase "ECHO"s to run it for real.
@echo off
setlocal
set Folder=\\SomeServer\SomeShare\SomeFolder
for %%a in ("%Folder%\*.*") do (
  ECHO del "%%~a"
)
for /d %%a in ("%Folder%\*.*") do (
  ECHO rd /s /q "%%~a"
)

Open in new window

0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 

Author Comment

by:chinniatluri
ID: 37736569
I had modified the script as below :
@echo off
setlocal
set Folder=\\192.168.1.22\da$
for %%a in ("%Folder%\*.*") do (
 
)
for /d %%a in ("%Folder%\*.*") do (
 
)
             


While running I'm getting ) was unexpected at this time
0
 

Author Comment

by:chinniatluri
ID: 37736574
@echo off
setlocal
set Folder=\\192.168.1.22\da$
for %%a in ("%Folder%\*.*") do (
   del "%%~a"
)
for /d %%a in ("%Folder%\*.*") do (
   rd /s /q "%%~a"
)

This done the job for me ! If you dont mind can you give a brief of each line, about what it does?
0
 

Author Comment

by:chinniatluri
ID: 37736655
oBdA,

 Any VBscript that does with same functionality
0
 
LVL 83

Expert Comment

by:oBdA
ID: 37736709
As for what the batch script does:
for %%a in ("%Folder%\*.*") do (
   del "%%~a"
)

That's a standard "for" loop over all files in the directory that's defined in the variable "folder"; in the loop, the currently processed file can be referenced using the loop variable "%%a".

for /d %%a in ("%Folder%\*.*") do (
   rd /s /q "%%~a"
)

Basically the same as above, but with the option "/d" to process directories instead of files.
rd /s /q "%%~a" will then delete the currently processed directory quietly (/q), including all subfolders (/s).
0
 

Author Comment

by:chinniatluri
ID: 37736732
oBdA,

Thanks for the explanation. Any VBscript that does work with same functionality, Please let me know?
0
 
LVL 83

Accepted Solution

by:
oBdA earned 500 total points
ID: 37739951
Try this; it's in test mode and will display the files and folders it would delete.
Remove the "comment" apostrophe to run it for real.
To avoid a popup box for each file/folder, either run it with cscript.exe, or comment out the "Wscript.Echo" lines.
strFolder = "\\SomeServer\SomeShare\SomeFolder"
Set fso = CreateObject("Scripting.FileSystemObject")
For Each objFile In fso.GetFolder(strFolder).Files
  Wscript.Echo "Deleting file: " & objFile.Path
'  objFile.Delete
Next
For Each objSubfolder In fso.GetFolder(strFolder).Subfolders
  Wscript.Echo "Deleting folder: " & objSubfolder.Path
'  objSubfolder.Delete
Next

Open in new window

0
 

Author Closing Comment

by:chinniatluri
ID: 37740762
ObDa Thanks for the Help & timely response
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

This is an article about Leadership and accepting and adapting to new challenges. It focuses mostly on upgrading to Windows 10.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

773 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