Solved

Batch File to clean contents on a shared Folder.

Posted on 2012-03-18
10
356 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
 

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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to prioritize LOGONSERVER for clients? 1 42
sccm client without collection 1 41
run cmd.exe remotely 5 46
How to "print screen" an active window on Windows 10 OS 6 50
Issue: One Windows 2008 R2 64bit server on the network unable to connect to a buffalo Device (Linkstation) with firmware version 1.56. There are a total of four servers on the network this being one of them. Troubleshooting Steps: Connect via h…
This is an article about Leadership and accepting and adapting to new challenges. It focuses mostly on upgrading to Windows 10.
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

863 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

23 Experts available now in Live!

Get 1:1 Help Now