[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

VB Script Help

Posted on 2012-09-14
4
Medium Priority
?
662 Views
Last Modified: 2012-09-15
Hi,
I need to write a vb script to do the following:
Delete any open drive with letter L
Login to a network path using username and pwd
Loop through all the sub-folders under the network path until I find a sub-folder matching a name ("Log")
If this sub-folder is older then 1 day, delete it
Disconnect network drive

I wrote this code, but somehow my code is not looping through all the sub-folder.

********************************************************************

'Delete Log files older than 1 day from vFiler - DB Backups
Option Explicit
Dim objNetwork
Dim strDriveLetter, strRemotePath, strUser, strPassword, strProfile
Dim objShell, DriveLetter1
Dim strFolderToSearch, objFSO, objRootFolder, objFolder, colSubfolders, strOutput
Const strMatch = "Log"
Const intDays = 1


Set objShell = CreateObject("WScript.Shell")
Set objNetwork = CreateObject("WScript.Network")
On Error Resume Next
objNetwork.RemoveNetworkDrive "L:"


Set objNetwork = CreateObject("Wscript.Network")
objNetwork.MapNetworkDrive "L:", "\\I.P.\data\test", False, "username", "pwd"


strFolderToSearch = "\\I.P\data\test"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objRootFolder = objFSO.GetFolder(strFolderToSearch)
'msgbox objRootFolder
Set colSubfolders = objRootFolder.SubFolders

For Each objFolder in colSubfolders
msgbox objFolder.name
    If InStr(objFolder.Name, strMatch) Then
      msgbox "hiii"
       If DateDiff("d", objFolder.DateCreated, Now) > intDays Then
        Wscript.Echo "Folder to be deleted: " & objFolder.Path
           objFolder.Delete
        End If
      End If
Next
0
Comment
Question by:sunexchange
  • 2
  • 2
4 Comments
 
LVL 59

Accepted Solution

by:
Bill Prew earned 2000 total points
ID: 38400939
Are you wanting to recursively drill into sub sub folders, or only go down one level?  It looks like you will get all of the folders one level below the starting folder, but not below that.  That's easy to fix with a recursive subroutine, like this.

'Delete Log files older than 1 day from vFiler - DB Backups
Option Explicit

Dim objNetwork
Dim strDriveLetter, strRemotePath, strUser, strPassword, strProfile
Dim objShell, DriveLetter1
Dim strFolderToSearch, objFSO, objRootFolder, objFolder, colSubfolders, strOutput
Const strMatch = "Log"
Const intDays = 1


Set objShell = CreateObject("WScript.Shell")
Set objNetwork = CreateObject("WScript.Network")
On Error Resume Next
objNetwork.RemoveNetworkDrive "L:"


Set objNetwork = CreateObject("Wscript.Network")
objNetwork.MapNetworkDrive "L:", "\\I.P.\data\test", False, "username", "pwd"


strFolderToSearch = "\\I.P\data\test"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objRootFolder = objFSO.GetFolder(strFolderToSearch)
'msgbox objRootFolder

SearchFolders objRootFolder

Wscript.Quit

Sub SearchFolders(objBaseFolder)
   For Each objFolder in objBaseFolder.SubFolders
      msgbox objFolder.Path
      If InStr(objFolder.Name, strMatch) Then
         msgbox "hiii"
         If DateDiff("d", objFolder.DateCreated, Now) > intDays Then
            Wscript.Echo "Folder to be deleted: " & objFolder.Path
            objFolder.Delete
         Else
            SearchFolders objFolder
         End If
      Else
         SearchFolders objFolder
      End If
   Next 
End Sub

Open in new window

~bp
0
 

Author Comment

by:sunexchange
ID: 38400981
Bill,

Thanks for fixing the code. However, I am still unable to delete the folder, although the code is able to get to the sub-folder

Here is the folder structure for your info:

\\I.P.\data\test
|
|_09-13-2012
   |_Log
|_09-14-2012
   |_Log

Here, I want to delete the Log folder only for the data folder 09-13-2012, since it is more than 1 day old.

Looks like the folder format needs to be in mm-dd-yyyy form so my datediff function will work.
0
 

Author Comment

by:sunexchange
ID: 38400987
never mind, I found the issue in my code wit respect to delete. Thanks a lot for your solution.
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 38401537
Glad that helped, thanks for the feedback.

~bp
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month19 days, 7 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question