Link to home
Start Free TrialLog in
Avatar of thesurg3on
thesurg3on

asked on

VBScript: Map Network Drive, Run Command, Disconnect Network Drive

Hi,

I am hoping to get some help on a script that does the following:
map a network drive to a UNC path
run a command: forfiles /p "z:\" /m *.txt /d -365 /c "cmd del /q @file"
disconnect the network drive from the UNC path

Avatar of merowinger
merowinger
Flag of Germany image

This should to it
Set objNet = CreateObject("Wscript.network")
Set objShell = CreateObject("Wscript.Shell")

objNet.MapNetworkDrive "Z:","\\localhost\c$",True
objShell.Run "forfiles /p " &Chr(34) &"z:\" &Chr(34) &" /m *.txt /d -365 /c " &chr(34) &"cmd del /q @file" &Chr(34),1,True
objNet.RemoveNetworkDrive "Z:",True,True

Open in new window

Avatar of thesurg3on
thesurg3on

ASKER

i didnt use your vestion with the &chr(34) &, nevertheless, it seems like the same results. the network drive runs, but the forfiles never runs.


Set objNet = CreateObject("Wscript.network")
Set objShell = CreateObject("Wscript.Shell")
 
'objNet.MapNetworkDrive "Z:","\\server\path",True
objShell.Run "forfiles /p ""z:\"" /m *.done /d -365 /c ""cmd del /q @file"""
'objNet.RemoveNetworkDrive "Z:",True,True
all the forfiles is supposed to do is delete any file older than 365 days with the extension .done
you have to use the version with the chr(34) option as vbscript else cannot handle the quotes. Please use my original version and try again
i did use your version and it did not work

what is chr(34)? is it a single quote or a double quote? my initial question shows the quotes exactly as if you were to run it from the command line. I thought you had to escape a quote out with preceding quote, which is why i removed yoru chr(34) and put in a " instead.
because vbscript will interpret them and your command line will be destroyed.
Chr(34) is a single quote. do you get any errors?

Please compare the output from this code with the running command.

wscrip.echo "forfiles /p " &Chr(34) &"z:\" &Chr(34) &" /m *.txt /d -365 /c " &chr(34) &"cmd del /q @file" &Chr(34)
can vbscript delete files from a network share without mapping a drive? if so, it'd probably be easier to just write it so that we can delete the files.
yes but what exactly shell be deleted?
any file with extension .done that is older than 3 months.

no need to recurse sub-directories. folders shouldn't be deleted, just the files.
all text files which are older than 365 days? I don't know the forfiles syntax
forfiles /p "PATH" /m *.txt /d -90 /c "cmd del /q @file"

/p and then the path to the files
/m to match the files to be deleted
/d for date. -90 means anything older than 90 days
/c means to run a command and the command is del /q @file"
/q is quiet
@file means to delete the files that were matched in the /m switch

ASKER CERTIFIED SOLUTION
Avatar of merowinger
merowinger
Flag of Germany image

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
AWESOME!!!

Exactly what I wanted. I really appreciate it.
EXCELLENT. Exactly what i needed