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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

My loop dosen't work

Status
Not open for further replies.

josephk73

Technical User
Aug 1, 2002
115
0
0
GB
okay Guys


I'm trying to achieve the following objective.

I need to look for a file on a computer and if it is not there, I would like to look for it for x number of times using a For..Next loop and waiting for a specified amount of times between each iteration of the loop.

If the file is found, then it should go to another subroutine, which will move the file of the computer and if not, the script should move onto the next computer in the file.

My problem is the loop that should look for the file and then acting appropriately depending on whether the file was there or not. For starters, when the file is there, it is reporting as not being there and then the script bombs out without ever getting to the second hostname that has been read into a dictionary object from a file.

The code is as below:

Code:
For Each computer In oDictionary

compHost=oDictionary.item(computer)

WScript.Echo "Attempting to move log file from " & compHost 

Call fileThere (compHost)
If fileThere=False Then
WScript.Echo "Despite 5 attempts, the file cannot be found"
Else 
copyFile
End If 
Next



function fileThere(compHost)


				sourceFile="\\"&compHost&"\c$\"&UCase(compHost)&".txt"
WScript.Echo sourceFile


			For i = 1 To 2
			WScript.Echo "attempt " & i & " of 2"
				Err.Clear()

					If Not moveObjFSO.FileExists(sourceFile) Then
						WScript.Sleep (3000)
				End If 
			Next
		
			fileThere=False
End Function


Sub copyFile (compHost)

WScript.Echo "Found"
objNetWork.MapNetWorkDrive "w:",path
If Err.Number=0 Then

moveObjFSO.Movefile sourceFile, logFileDest
Err.Clear
Else
'the machine may be in a workgroup so try the local admin account
objNetWork.MapNetWorkDrive "w:", path,,Tuser,TpassWord
If Err.Number<>0 Then
WScript.Echo compHost & " - cannot be connected to. Try manually"
Err.Clear
End If 
End If 

objNetWork.RemoveNetWorkDrive "w:"

End Sub

Any help would be great to get this working. Cheers.
 

You're losing the return value from fileThere by using Call.

Try replacing:
Code:
Call fileThere (compHost)
If fileThere=False Then
with:
Code:
If Not fileThere(compHost) Then
 
Revision of script by josephk73.
[tt]
For Each computer In oDictionary[blue].keys[/blue]
compHost=oDictionary.item(computer)
WScript.Echo "Attempting to move log file from " & compHost
If [red]fileThere(compHost)[/red]=False Then
WScript.Echo "Despite 5 attempts, the file cannot be found"
Else
copyFile[red] compHost[/red]
End If
Next

function fileThere(compHost)
sourceFile="\\"&compHost&"\c$\"&UCase(compHost)&".txt"
WScript.Echo sourceFile
For i = 1 To 2
WScript.Echo "attempt " & i & " of 2"
'Err.Clear()
If Not moveObjFSO.FileExists(sourceFile) Then
WScript.Sleep (3000)
fileThere=False
[red]else
fileThere=true
exit function[/red]
End If
Next
End Function

Sub copyFile (compHost)
[red]'Where is the use of compHost????????[/red]
[red]'How path depends on compHost????[/red]
[red]'You have some dependence on global variables path, sourceFile, logFileDest, Tuser, Tpassword
'---not an excellent practice[/red]
WScript.Echo "Found"
[red]on error resume next[/red]
objNetWork.MapNetWorkDrive "w:",path 'make sure path is some valid global variable
If Err.Number=0 Then
moveObjFSO.Movefile sourceFile, logFileDest
'Err.Clear
Else
'the machine may be in a workgroup so try the local admin account
[red]err.clear[/red]
objNetWork.MapNetWorkDrive "w:", path,,Tuser,TpassWord
If Err.Number<>0 Then
WScript.Echo compHost & " - cannot be connected to. Try manually"
Err.Clear
[red]else[/red]
moveObjFSO.Movefile sourceFile, logFileDest
End If
End If
objNetWork.RemoveNetWorkDrive "w:"
on error goto 0
End Sub
[/tt]
 
antykreist & tsuji thanks for your replies. Again you guys must be praised, helping us 'amateurs' with your words of wisdom. Much appreciated, really. Anycase, I've got a long public holiday which started 5 mins ago, so I wont be testing until Tuesday, but I'll give both suggestions a go and let you know how I get on.

Thanks again.
 
Hi guys,

Back from my hols and well refreshed. Anycase to get the script to loop and pause for a specified I came up with this code and all seems well.
Code:
For Each computer In oDictionary
i=0

compHost=oDictionary.item(computer)
sourceFile="\\"&compHost&"\c$\"&logType&"_"&UCase(compHost)&".txt"
path="\\"&compHost&"\c$"
WScript.Echo "Attempting to move log file from " & compHost 
for i=1 to 3

Err.Clear

If moveObjFSO.FileExists(sourceFile) Then
WScript.Echo "Found"
objNetWork.MapNetWorkDrive "w:",path
moveObjFSO.Movefile sourceFile, logFileDest
objNetWork.RemoveNetWorkDrive "w:"
Err.Clear
Exit for 
ElseIf Err.Number = 1326 Then
objNetWork.MapNetWorkDrive "w:", path,,Tuser,TpassWord
moveObjFSO.Movefile sourceFile, logFileDest
objNetWork.RemoveNetWorkDrive "w:"
Err.Clear
Exit for
Else 
WScript.Echo compHost & " - file cannot be found, sleeping....."
  Wscript.sleep (5000)
Err.Clear
if i=3 Then
  Wscript.Echo "We tried 3 times, but no luck with " & compHost

End If

 
End If

next
next

Guys, thanks for your help, but here comes ANOTHER request.

For those computers that do not have the log file present, I want to create a dictionary object and store the host names in the dictionary.

When the main body of the script has run, I need to have a subroutine that will iterate through the dictionary making a connection to each comuter and moving the log file, if present.

As each computer is connected to and the log file moved, I then need to remove that computer from the dictionary. This process should continue until the dictionary object does not conatain any further computer names.

Any ideas?
 
For Each computer In oDictionary.keys
compHost=oDictionary.item(computer)
WScript.Echo "Attempting to move log file from " & compHost
If fileThere(compHost)=False Then
WScript.Echo "Despite 5 attempts, the file cannot be found"
If Not dicFailedHosts.Exists(LCase(CStr(compHost))) Then
dicFailedHosts.Add(LCase(CStr(compHost)))
End If
Else
copyFile compHost
End If
Next

'end of the script double check?
For Each aComputer In dicFailedHosts
If fileThere(aComputer)=False Then
WScript.Echo "Despite 5 attempts, the file cannot be found"
Else
copyFile aComputer
dicFailedHosts.Remove aComputer
End If
Next



'that being said i would say you should just reuse your oDictionary, it seems you are just doing the same thing twice.
why not create you main script as a sub which you pass the oDictionary and call it recursively? each time have the oDictionary.remove on a successful file copy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top