Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing a file and re-running a function 1

Status
Not open for further replies.

woter324

Technical User
Jan 26, 2007
179
GB
Hi,

I have a list of usernames in a text file that are validated against AD. I have a msgbox that triggers when a username doesn't match. The msgbox has Abort / Retry / Ignore buttons.

If a mismatch between the username in the text file and AD ocures, I'd like to give the user the option to correct the username within the file, save the file and re-run the code if the user clicks Retry.

I am having difficulty getting the function that reads the text file to 'notice' the change to the file. I've tried various ways but still haven't managed it. Currently, it ignores the changes and returns the correct usernames.

Below is the function call and the function itself that reads the text file:

Code:
For Each u In getUserList(strUsername)
   WScript.Echo i
Next

Function getUserList(strUsername)
Dim vRerun, vProblem
vRerun = False
vProblem = False
Set oDict = CreateObject("Scripting.Dictionary")

If InStr(1,strUsername, ":\") Then
	If fso.FileExists(strUsername) Then
		Set oTextFile = fso.OpenTextFile(strUsername, 1)
		i = 0
		Do Until oTextFile.AtEndOfStream
    		strNextLine = oTextFile.Readline
			i=i+1
			If validateUsername(strNextLine) Then
				'only add valid usernames.
				oDict.Add i,strNextLine
			Else
				vProblem = True
				intOut = MsgBox("A user with the name " & Chr(34) & strNextLine & Chr(34) & " cannot be found in AD." & VbCrLf & _
							"Please correct the username in the file " & strUsername & ", save the file and click Retry.",18,"Error!")
				If intOut = 3 Then 'Abort
					MsgBox "You have aborted this script!",16,"Abort"
					'validateUsername = False
					WScript.Quit
				ElseIf intOut = 4 Then 'Retry
					'MsgBox "Out: " & intOut
					oTextFile.close
					Set oTextFile = fso.OpenTextFile(strUsername, 1)
					'vRerun = True
					'Exit Do
					
				End If
			End If	
		Loop
		oTextFile.Close
		If vRerun = True Then
		 	getUserList(strUsername)
		End If
	End If
Else
	MsgBox "Cannot find specified file.",16,"Missing File."
End If

'If vProblem = False Then
	getUserList = oDict.Items
'End If

End Function

If anyone could point me in the right direction on how to get the function to re-check the file, I'd be most grateful

Many thanks

T
 
Here's what comes to the top of my head... The new code is outdented to make it more visible...

Code:
For Each u In getUserList(strUsername)
   WScript.Echo i
Next

Function getUserList(strUsername)
	Dim vRerun, vProblem
	vRerun = False
	vProblem = False
	Set oDict = CreateObject("Scripting.Dictionary")

' Enter loop that continues if vRerun gets set to true
Do
' Force vRerun to false on each loop
vRerun = False
	If InStr(1,strUsername, ":\") Then
		If fso.FileExists(strUsername) Then
' Get current file timestamp for future reference
dFileDate = fso.GetFile(strUsername).DateLastModified
			Set oTextFile = fso.OpenTextFile(strUsername, 1)
			i = 0
			Do Until oTextFile.AtEndOfStream
				strNextLine = oTextFile.Readline
				i=i+1
				If validateUsername(strNextLine) Then
					'only add valid usernames.
					oDict.Add i,strNextLine
				Else
' 					vProblem = True
					intOut = MsgBox("A user with the name " & Chr(34) & strNextLine & Chr(34) & " cannot be found in AD." & VbCrLf & _
					"Please correct the username in the file " & strUsername & ", save the file and click Retry.",18,"Error!")

					If intOut = 3 Then 'Abort
						MsgBox "You have aborted this script!",16,"Abort"
						'validateUsername = False
						WScript.Quit

					ElseIf intOut = 4 Then 'Retry
						'MsgBox "Out: " & intOut
						oTextFile.close
' 						Set oTextFile = fso.OpenTextFile(strUsername, 1)
						vRerun = True
' Go into a loop, waiting for the file timestamp to change
Do While dFileDate = fso.GetFile(strUsername).DateLastModified
	WScript.Sleep 1000
	iCtr = iCtr + 1
	If iCtr = 60 Then
		intOut = MsgBox("Do you want me to keep waiting for you to update the file?", vbQuestion + vbYesNo, "Keep Waiting?"
		If intOut = vbYes Then iCtr = 0 Else WScript.Quit
	End If
Loop
	
						'Exit Do

					End If
				End If    
			Loop
			oTextFile.Close
			If vRerun = True Then
			getUserList(strUsername)
			End If
		End If
	Else
		MsgBox "Cannot find specified file.",16,"Missing File."
	End If
Loop Until vRerun = False

	'If vProblem = False Then
	getUserList = oDict.Items
	'End If

End Function

PSC
[—] CCNP (R&S/Wireless) [•] CCSP [•] MCITP: Enterprise Admin [•] MCSE [—]

Governments and corporations need people like you and me. We are samurai. The keyboard cowboys. And all those other people out there who have no idea what's going on are the cattle. Mooo! --Mr. The Plague, from the movie "Hackers
 
Thank PSC.

I had to remove the dictionary object as it was getting a key conflict.

Apart from that it works.

Many many thanks

Here is the code:
Code:
Function getUserList(strUsername)
    Dim vRerun, vProblem
    vRerun = False
    vProblem = False
    Set oDict = CreateObject("Scripting.Dictionary")

' Enter loop that continues if vRerun gets set to true
Do
' Force vRerun to false on each loop
vRerun = False
    If InStr(1,strUsername, ":\") Then
        If fso.FileExists(strUsername) Then
' Get current file timestamp for future reference
dFileDate = fso.GetFile(strUsername).DateLastModified
            Set oTextFile = fso.OpenTextFile(strUsername, 1)
            i = 0
            Do Until oTextFile.AtEndOfStream
                strNextLine = oTextFile.Readline
                i=i+1
                If validateUsername(strNextLine) Then
                    'only add valid usernames.
                    oDict.Add i,strNextLine
                Else
'                     vProblem = True
                    intOut = MsgBox("A user with the name " & Chr(34) & strNextLine & Chr(34) & " cannot be found in AD." & VbCrLf & _
                    "Please correct the username in the file " & strUsername & ", save the file and click Retry.",18,"Error!")

                    If intOut = 3 Then 'Abort
                        MsgBox "You have aborted this script!",16,"Abort"
                        WScript.Quit

                    ElseIf intOut = 4 Then 'Retry
                        oDict.RemoveAll
                        vRerun = True
' Go into a loop, waiting for the file timestamp to change
Do While dFileDate = fso.GetFile(strUsername).DateLastModified
    WScript.Sleep 1000
    iCtr = iCtr + 1
    If iCtr = 30 Then '30 seconds
        intOut = MsgBox("Do you want to keep waiting for you to update the file?", vbQuestion + vbYesNo, "Keep Waiting?")
        If intOut = vbYes Then iCtr = 0 Else WScript.Quit
    End If
Loop
    
                        'Exit Do

                    End If
                End If    
            Loop
            oTextFile.Close
            If vRerun = True Then
            getUserList(strUsername)
            End If
        End If
    Else
        MsgBox "Cannot find specified file.",16,"Missing File."
    End If
Loop Until vRerun = False

getUserList = oDict.Items


End Function
 
Actually, you can just clear the dictionary on each loop by doing an oDict.RemoveAll. This would solve the duplicate key issue. Or you could do a quick check to verify that they key doesn't exist with an "if oDict.Exist" statement.

PSC
[—] CCNP (R&S/Wireless) [•] CCSP [•] MCITP: Enterprise Admin [•] MCSE [—]

Governments and corporations need people like you and me. We are samurai. The keyboard cowboys. And all those other people out there who have no idea what's going on are the cattle. Mooo! --Mr. The Plague, from the movie "Hackers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top