Solved

Batch File to clean contents on a shared Folder.

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

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
Flexible connectivity for any environment

The KE6900 series can extend and deploy computers with high definition displays across multiple stations in a variety of applications that suit any environment. Expand computer use to stations across multiple rooms with dynamic access.

 

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 84

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 84

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

Suggested Solutions

Title # Comments Views Activity
VMWare Calculate number of processors 10 66
Selecting Right Partition 6 69
how to count files? 4 31
Configuring DAG with different CU level ? 6 12
If you get continual lockouts after changing your Active Directory password, there are several possible reasons.  Two of the most common are using other devices to access your email and stored passwords in the credential manager of windows.
While rebooting windows server 2003 server , it's showing "active directory rebuilding indices please wait" at startup. It took a little while for this process to complete and once we logged on not all the services were started so another reboot is …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

828 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