Link to home
Start Free TrialLog in
Avatar of chinniatluri
chinniatluriFlag for Afghanistan

asked on

Batch File to clean contents on a shared Folder.

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.
Avatar of PeteEngineer
PeteEngineer
Flag of India image

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
Avatar of chinniatluri

ASKER

what is the command to remove fiels?
Avatar of oBdA
oBdA

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

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
@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?
oBdA,

 Any VBscript that does with same functionality
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).
oBdA,

Thanks for the explanation. Any VBscript that does work with same functionality, Please let me know?
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ObDa Thanks for the Help & timely response