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

thesurg3onAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

merowingerCommented:
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

0
thesurg3onAuthor Commented:
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
0
thesurg3onAuthor Commented:
all the forfiles is supposed to do is delete any file older than 365 days with the extension .done
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

merowingerCommented:
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
0
thesurg3onAuthor Commented:
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.
0
merowingerCommented:
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)
0
thesurg3onAuthor Commented:
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.
0
merowingerCommented:
yes but what exactly shell be deleted?
0
thesurg3onAuthor Commented:
any file with extension .done that is older than 3 months.

0
thesurg3onAuthor Commented:
no need to recurse sub-directories. folders shouldn't be deleted, just the files.
0
merowingerCommented:
all text files which are older than 365 days? I don't know the forfiles syntax
0
thesurg3onAuthor Commented:
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

0
merowingerCommented:
ok give that a try i just cannot test it
Set objNet = CreateObject("Wscript.network") 
set objFSO = CreateObject("Scripting.FileSystemObject")
 
objNet.MapNetworkDrive "Z:","\\localhost\c$",True

set objFolder = obJFSO.getFolder("\\localhost\c$")
For each file in objFolder.Files
	If ucase(right(file,4)) = "DONE" Then
		If Now - 90 > objFSO.GetFile(file).DateLastModified Then
			objFSO.DeleteFile file,true
		End If
	End If

Next


objNet.RemoveNetworkDrive "Z:",True,True

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
thesurg3onAuthor Commented:
AWESOME!!!

Exactly what I wanted. I really appreciate it.
0
thesurg3onAuthor Commented:
EXCELLENT. Exactly what i needed
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.