Solved

Problem with vbscript

Posted on 2011-02-17
16
422 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:ob1_
  • 8
  • 7
16 Comments
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
What code is in c:\users\administrator\desktop\test.vbs ?

~bp
0
 
LVL 6

Author Comment

by:ob1_
Comment Utility
just 1 line: msgbox("hello")
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
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
 
LVL 6

Author Comment

by:ob1_
Comment Utility
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
 
LVL 51

Assisted Solution

by:Bill Prew
Bill Prew earned 200 total points
Comment Utility
Okay, so it sounds like you are never leaving the FOR loop, let me take a closer look at that...

~bp
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
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
 
LVL 6

Author Comment

by:ob1_
Comment Utility
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
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
Can you provide samples of the files it's reading so I can test here?

~bp
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 65

Accepted Solution

by:
RobSampson earned 300 total points
Comment Utility
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
 
LVL 6

Author Comment

by:ob1_
Comment Utility
sorry for the delay - thanks for the comments, i will check this when i get back into the office thursday or friday.
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
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
 
LVL 6

Author Comment

by:ob1_
Comment Utility
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
 
LVL 6

Author Comment

by:ob1_
Comment Utility
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
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
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
 
LVL 6

Author Comment

by:ob1_
Comment Utility
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
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
Welcome.

~bp
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Unlike scripting languages such as C# where a semi-colon is used to indicate the end of a command, Microsoft's VBScript language relies on line breaks to determine when a command begins and ends. As you can imagine, this quickly results in messy cod…
Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now