urgent!!
Hi all,
i am a beginner in Vbs.Actually i am writing a script to delete folders and subfolders from ramote machine.in this i am using mapping of the given folder to local machine.My script works for mapping and deletion but it is not able to remove mapped drive.Can you look into the script plz
' Sets Up Log File
Const OPEN_FILE_FOR_WRITE = 2
Const OPEN_FILE_FOR_APPEND = 8
Set objFileSystem = CreateObject("Scripting.FileSystemObject")
strOutputLog = "C:\Temp\FolderCheck.log"
Set LogFile = objFileSystem.OpenTextFile(strOutputLog,OPEN_FILE_FOR_APPEND,true)
'LogFile.Writeline (Now & " Username : " & strWinntUserName)
'LogFile.Writeline (Now & " Computer Name : " & strComputerName)
'LogFile.Writeline ""
arrComputers = Split("10.102.16.55", ";")
For Each strComputer In arrComputers
if strComputer <> "" Then
Set objFso = CreateObject("Scripting.FileSystemObject")
Set WshNetwork = CreateObject("WScript.Network")
WshNetwork.MapNetworkDrive "P:", "\\" & StrComputer & "\C$\ Documents and Settings\NetworkService\Local Settings\Temporary Internet Files\Content.IE5"
Set objFolder = objFso.GetFolder("P:\")
killFiles(objFolder) 'Delete files
getSubfolders(objFolder) 'Delete files in subfolders
Set objFolder = Nothing
Msgbox strComputer &"fle deleted"
WshNetwork.RemoveNetworkDrive "P:"
else
Msgbox strComputer & "has not been processed."
End If
Next
Sub killFiles (ByRef objFolder) 'Delete all files in folder
Set colFiles = objFolder.Files
On Error Resume Next
For Each objFile In colFiles
objFile.Delete
Next
On Error Goto 0
End Sub
Sub getSubFolders(ByRef objParentFolder) 'If subfolders exist, process files
' in each folder
Set colSubfolders = objParentFolder.SubFolders
For Each objSubfolder In colSubfolders
killFiles(objSubfolder)
If objSubfolder.SubFolders.Count > 0 Then
getSubFolders(objSubFolder)
End If
objSubfolder.Delete
Next
End Sub
i am a beginner in Vbs.Actually i am writing a script to delete folders and subfolders from ramote machine.in this i am using mapping of the given folder to local machine.My script works for mapping and deletion but it is not able to remove mapped drive.Can you look into the script plz
' Sets Up Log File
Const OPEN_FILE_FOR_WRITE = 2
Const OPEN_FILE_FOR_APPEND = 8
Set objFileSystem = CreateObject("Scripting.FileSystemObject")
strOutputLog = "C:\Temp\FolderCheck.log"
Set LogFile = objFileSystem.OpenTextFile(strOutputLog,OPEN_FILE_FOR_APPEND,true)
'LogFile.Writeline (Now & " Username : " & strWinntUserName)
'LogFile.Writeline (Now & " Computer Name : " & strComputerName)
'LogFile.Writeline ""
arrComputers = Split("10.102.16.55", ";")
For Each strComputer In arrComputers
if strComputer <> "" Then
Set objFso = CreateObject("Scripting.FileSystemObject")
Set WshNetwork = CreateObject("WScript.Network")
WshNetwork.MapNetworkDrive "P:", "\\" & StrComputer & "\C$\ Documents and Settings\NetworkService\Local Settings\Temporary Internet Files\Content.IE5"
Set objFolder = objFso.GetFolder("P:\")
killFiles(objFolder) 'Delete files
getSubfolders(objFolder) 'Delete files in subfolders
Set objFolder = Nothing
Msgbox strComputer &"fle deleted"
WshNetwork.RemoveNetworkDrive "P:"
else
Msgbox strComputer & "has not been processed."
End If
Next
Sub killFiles (ByRef objFolder) 'Delete all files in folder
Set colFiles = objFolder.Files
On Error Resume Next
For Each objFile In colFiles
objFile.Delete
Next
On Error Goto 0
End Sub
Sub getSubFolders(ByRef objParentFolder) 'If subfolders exist, process files
' in each folder
Set colSubfolders = objParentFolder.SubFolders
For Each objSubfolder In colSubfolders
killFiles(objSubfolder)
If objSubfolder.SubFolders.Count > 0 Then
getSubFolders(objSubFolder)
End If
objSubfolder.Delete
Next
End Sub
0 Comments
[ + ] Show comments
Answers (6)
Please log in to answer
Posted by:
anonymous_9363
14 years ago
arrComputers = Split("10.102.16.55", ";")
You are attempting to split a string using a character which doesn't appear in the string. Presumably you've removed another IP address from the string and that the list is actually delimited using semi-colon?
WshNetwork.MapNetworkDrive "P:", "\\" & StrComputer & "\C$\ Documents and Settings\NetworkService\Local Settings\Temporary Internet Files\Content.IE5"
I think the fact that the drive is mapped to the actual folder, rather than the share, keeps a handle on that folder open, meaning that it cannot be deleted. I think you probably want to map only as far as the hidden share (C$) and then add the folder information to the file deletion code.
Posted by:
ritutiw
14 years ago
Posted by:
anonymous_9363
14 years ago
Posted by:
pjgeutjens
14 years ago
drive is mapping to the share folder only
WshNetwork.MapNetworkDrive "P:", "\\" & StrComputer & "\C$\ Documents and Settings\NetworkService\Local Settings\Temporary Internet Files\Content.IE5"
Dunno if you did a straight copy-paste of your script, but is that a space I detect between C$\ and Documents?
PJ
Posted by:
anonymous_9363
14 years ago
We're a couple of clods, aren't we, Pieter?
@ritutiw, why bother mapping the drive? Just refer to the remote machine as a UNC. That is, pass KillFiles the entire path:
Oh and BTW, you want the 'objFolder' variable in 'Sub KillFiles' to be referred to by value (ByVal) rather than by reference (ByRef). Check out MSDN for a good explanation of the difference.
@ritutiw, why bother mapping the drive? Just refer to the remote machine as a UNC. That is, pass KillFiles the entire path:
Set objFolder = objFso.GetFolder("\\" & StrComputer & "\C$\Documents and Settings\NetworkService\Local Settings\Temporary Internet Files\Content.IE5" )
killFiles(objFolder)
You should also add some error-trapping by testing for the folder's existence before attempting to delete it and also after deletion, the latter to ensure that the deletion happened.Oh and BTW, you want the 'objFolder' variable in 'Sub KillFiles' to be referred to by value (ByVal) rather than by reference (ByRef). Check out MSDN for a good explanation of the difference.
Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.
so that the conversation will remain readable.