Problem with vbscript

I have a script that calls another script. I have used the code below to do this. It works fine when i put it in the beginning or middle of my script, but if i put it at the end of my script like it is supposed to be then the 2nd script does not get called and wscript.exe just runs indefinitely in Task Manager.

the code that calls the next script is in the 2 lines towards the end:

Set WSHShell = CreateObject("WScript.Shell")
WSHShell.Run "wscript c:\users\administrator\desktop\test.vbs", , True
On Error Resume Next  

Dim fso, folder, NewsFile,sFolder, files
Dim counter
Dim filenames()

Set fso = CreateObject("Scripting.FileSystemObject")  
sFolder = "c:\test"

If sFolder = "" Then      
	Wscript.Echo "No Folder parameter was passed"      
	Wscript.Quit  
End If  

'Set NewFile = fso.CreateTextFile(sFolder&"\FileList.txt", True)  
Set folder = fso.GetFolder(sFolder)  
Set files = folder.Files    

counter = 0

' load requests into array
For each textfile In files  
	ReDim Preserve filenames(counter)
	filenames(counter) = textfile.Name
	counter = counter + 1
Next

' build & display msgbox text for current requests
Dim strMsgboxtext
strMsgboxtext = "There are " & counter & " requests to process. Please click OK to continue." & vbcrlf

for i = 0 to counter
	strMsgboxtext = strMsgboxtext & vbcrlf & filenames(i)
next

msgbox(strMsgboxtext)

'msgbox "There are " & counter & " requests to process. Please click OK to continue."

Set objFSO = CreateObject("Scripting.FileSystemObject") 

for j = 0 to counter
	msgbox("Please click OK to process request #" & counter & ": " & filenames(counter))
		
		' read information from current request
		Set objFile = objFSO.OpenTextFile("c:\test\" & filenames(j), ForReading) 
		 
		Const ForReading = 1 
		
		' dim array to store contents of current request
		Dim arrFileLines() 
		
		' load contents of current request into array
		k = 0 
		
		Do Until objFile.AtEndOfStream 
			Redim Preserve arrFileLines(k) 
			arrFileLines(k) = objFile.ReadLine 
			k = k + 1 
		Loop
		
		objFile.Close 
		
		' init msgbox text to display contents of current request
		Dim strMsgboxtext2
		strMsgboxtext2 = "displaying contents of request #" & j+1 & ": " & vbcrlf & vbcrlf
		
		' build msgbox text to display contents of current request (k = number of lines in request)
		For l = 0 to k
			strMsgboxtext2 = strMsgboxtext2 & arrFileLines(l) & vbcrlf
		Next
		
		' show contents of current request
		msgbox(strMsgboxtext2)

Next

' call next script
Set WSHShell = CreateObject("WScript.Shell") 
WSHShell.Run "wscript c:\users\administrator\desktop\test.vbs", , True 

' indicate script is complete
msgbox("script complete")

'NewFile.Close

Open in new window

LVL 6
ob1_Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

 
Bill PrewCommented:
What code is in c:\users\administrator\desktop\test.vbs ?

~bp
0
 
ob1_Author Commented:
just 1 line: msgbox("hello")
0
 
Bill PrewCommented:
If you add a msgbox right before the execute, do you see it, like:

' call next script
msgbox("calling next script")
Set WSHShell = CreateObject("WScript.Shell")
WSHShell.Run "wscript c:\users\administrator\desktop\test.vbs", , True

Also, are you sure one or both of the msgbox windows aren't popping up behind other windows?

How are you launching the first script?

~bp
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
ob1_Author Commented:
nope it doesn't run. also if i move the 2 lines to call the next script up into the for/next loop above it the 2nd script will run. i made sure the window is not popping up in the background. i am just double-clicking on the first script.
0
 
Bill PrewCommented:
Okay, so it sounds like you are never leaving the FOR loop, let me take a closer look at that...

~bp
0
 
Bill PrewCommented:
It sounds like the loop is either taking a long time to run, or is stuck with a msgbox displayed.

Are you getting one of these for every file processed:

msgbox(strMsgboxtext2)

How many files are you processing, and how many lines are in each?

Can you describe what you are trying top accomplish, based on the current code I think there may be better approaches to take.

~bp
0
 
ob1_Author Commented:
probably - it seems like my code is inefficient b/c i am looping through the same array or dataset several times to do different things. i was going to try to get the individual pieces working and then consolidate the code if i could.

right now there are 4 files being processed. i get a msgbox(strMsgboxtext2) for each one. it just says "displaying the contents of request #1: filename.txt then lists the lines in the file. each request (txt file) has 9 lines but some could have less in the future.

once it loops through each file and displays the contents of it in the big for/next loop i was going to run another for/next loop to go  back through each file, and for each file execute a 2nd script while passing arguments to it for the lines in the file. the 2nd script will store those arguments as variables that it will use to build a user in Active Directory.
0
 
Bill PrewCommented:
Can you provide samples of the files it's reading so I can test here?

~bp
0
 
RobSampsonCommented:
Hi, this should work.  You had a few errors in there, and the loops need to be
For i = 0 To counter - 1

because the array starts at zero.

Regards,

Rob.
'On Error Resume Next

Dim fso, folder, NewsFile,sFolder, files
Dim counter
Dim filenames()
Const ForReading = 1 

Set fso = CreateObject("Scripting.FileSystemObject")  
sFolder = "c:\test"

sFolder = "C:\Temp\Temp\Test Script\Test"

If sFolder = "" Then      
	Wscript.Echo "No Folder parameter was passed"      
	Wscript.Quit  
End If 
If Right(sFolder, 1) <> "\" Then sFolder = sFolder & "\"

'Set NewFile = fso.CreateTextFile(sFolder&"\FileList.txt", True)  
Set folder = fso.GetFolder(sFolder)  
Set files = folder.Files    

counter = 0

' load requests into array
For each textfile In files  
	ReDim Preserve filenames(counter)
	filenames(counter) = textfile.Name
	counter = counter + 1
Next

' build & display msgbox text for current requests
Dim strMsgboxtext
strMsgboxtext = "There are " & counter & " requests to process. Please click OK to continue." & vbcrlf

for i = 0 to counter - 1
	strMsgboxtext = strMsgboxtext & vbcrlf & filenames(i)
next

msgbox(strMsgboxtext)

'msgbox "There are " & counter & " requests to process. Please click OK to continue."

Set objFSO = CreateObject("Scripting.FileSystemObject") 

for j = 0 to counter - 1
	msgbox("Please click OK to process request #" & j & ": " & filenames(j))
		
		' read information from current request
		Set objFile = objFSO.OpenTextFile(sFolder & filenames(j), ForReading) 
		 
		' dim array to store contents of current request
		ReDim arrFileLines(0)
		
		' load contents of current request into array
		k = 0 
		
		Do Until objFile.AtEndOfStream 
			Redim Preserve arrFileLines(k) 
			arrFileLines(k) = objFile.ReadLine 
			k = k + 1 
		Loop
		
		objFile.Close 
		
		' init msgbox text to display contents of current request
		Dim strMsgboxtext2
		strMsgboxtext2 = "displaying contents of request #" & j+1 & ": " & vbcrlf & vbcrlf
		
		' build msgbox text to display contents of current request (k = number of lines in request)
		For l = 0 to k - 1
			strMsgboxtext2 = strMsgboxtext2 & arrFileLines(l) & vbcrlf
		Next
		
		' show contents of current request
		msgbox(strMsgboxtext2)

Next

' call next script
Set WSHShell = CreateObject("WScript.Shell") 
MsgBox "Running script"
'WSHShell.Run "wscript c:\users\administrator\desktop\test.vbs", 1, True 

' indicate script is complete
msgbox("script complete")

'NewFile.Close

Open in new window

0

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
 
ob1_Author Commented:
sorry for the delay - thanks for the comments, i will check this when i get back into the office thursday or friday.
0
 
Bill PrewCommented:
Based on your description and original code, see how this looks.  It takes the folder to scan as a command line parm.  I'm not sure you want all the MSGBOX's in along the way, but I left them in for testing.  I did add a couple of CANCEL options to two of them along the way though, in case that makes sense.  Also, it seemed like the call to the second script needed to be once for each line read from each file.  Hope this helps.

Option Explicit

' Define needed constants
Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2

' Define variables
Dim strBaseFolder, strMsgText, strData, strLine
Dim arrData
Dim intCount, intReturn
Dim objFSO, objShell, objBaseFolder, objFile, objDataFile

' Get input folder name from command line parm
If (WScript.Arguments.Count > 0) Then
  strBaseFolder = WScript.Arguments(0)
Else
  WScript.Echo "No folder name specified."
  WScript.Quit
End If

' Create needed objects
Set objFSO = CreateObject("Scripting.FileSystemObject")  
Set objShell = CreateObject("WScript.Shell") 

'Set NewFile = fso.CreateTextFile(sFolder&"\FileList.txt", True)  

' Access folder where control files exist
Set objBasefolder = objFSO.GetFolder(strBaseFolder)  

' Display message about number of files
strMsgtext = "There are " & objBaseFolder.Files.Count & " requests to process. Please click OK to continue." & vbCrLf
For Each objFile In objBaseFolder.Files
    strMsgtext = strMsgtext & vbCrLf & objFile.Name
Next
intReturn = MsgBox(strMsgtext, vbOKCancel)
If intReturn = vbCancel Then
    Wscript.Quit
End If

' Loop through all files and process them
intCount = 0
For Each objFile In objBaseFolder.Files
    intCount = intCount + 1
    intReturn = MsgBox("Please click OK to process request #" & intCount & ": " & objFile.Name, vbOKCancel)
    If intReturn = vbCancel Then
        Wscript.Quit
    End If

    ' read all lines from current file into array
    Set objDataFile = objFSO.OpenTextFile(objFile.Path, ForReading) 
    strData = objDataFile.ReadAll
    If Right(strData, 2) = vbCrLf Then
        strData = Left(strData, Len(strData)-2)
    End If
    arrData = Split(strData, vbCrLf)
    objDataFile.Close

    ' Info box for current file
    MsgBox("displaying contents of request #" & intCount & ": " & vbCrLf & vbCrLf & strData)

    ' For each line from this file, execute next script
    for Each strLine In arrData
        MsgBox("Processing line: " & strLine)
        ' call next script
        'objShell.Run "wscript c:\users\administrator\desktop\test.vbs """ & strline & """", , True 

        ' indicate script is complete
        MsgBox("script complete")
    Next
Next

Open in new window

~bp
0
 
ob1_Author Commented:
sorry for the delay, i have been traveling and out of the office. i will work on this today and post back if i can get it working.



thanks,
ob1
0
 
ob1_Author Commented:
billprew - how can i pass all of the "strlines" from 1 file (request) to the 2nd script when it is called?

my original script put them in an array so i could try to pass each part of the array as an argument to the next script...
0
 
Bill PrewCommented:
You can't pass arrays per se in VBS, it's not supported.  The typical approach is to string together the array elements in a comma delimited list, and pass that string parm to the second script.  The second script can then separate the elements again back into an array.  The JOIN and SPLIT commands make this quite simple to do in a single statement on each side, as ling as the array elements don't contain commas (or whatever other delimiter you decide to use in the passed string.  If this would would for you and you want an example let me know.

~bp
0
 
ob1_Author Commented:
actually i think i got it - i think i am going to use RobSampson's script just because I already have my items in an array that I can pass via arguments to the 2nd script and I understand it a little better (i am brand new to VBS and learning a chunk at a time). in the 2nd script I will use Wscript.Arguments to retrieve the items and put them in variables.

thanks for your help!!!!!
0
 
Bill PrewCommented:
Welcome.

~bp
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.