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?
 
merowingerConnect With a Mentor Commented:
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
 
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
thesurg3onAuthor Commented:
all the forfiles is supposed to do is delete any file older than 365 days with the extension .done
0
 
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
 
thesurg3onAuthor Commented:
AWESOME!!!

Exactly what I wanted. I really appreciate it.
0
 
thesurg3onAuthor Commented:
EXCELLENT. Exactly what i needed
0
All Courses

From novice to tech pro — start learning today.