Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
Read value from text file line, process, then Loop until EOF
I have a simple text file that contains a list of hostnames and each are on
their own seperate line. I'm having a great deal of trouble figuring out how code a way to begin reading the file on the first line, pass the value into a variable, perform a command using the variable, then LOOP back to read the next line and so on until EOF. Any help would be greatly appreciated!!! Thanks, Pcola |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
Read value from text file line, process, then Loop until EOF
Sub Gettext() Const ForReading = 1, ForWriting = 2, ForAppending = 3 Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0 Dim Data(8) 'default folder Folder = "C:\temp" ChDir (Folder) Set fsread = CreateObject("Scripting.FileSystemObject") FName = Application.GetOpenFilename("CSV (*.csv),*.csv") Set fread = fsread.GetFile(FName) Set tsread = fread.OpenAsTextStream(ForReading, TristateUseDefault) RowCount = 1 Do While tsread.atendofstream = False InputLine = tsread.ReadLine Range("A" & RowCount) = InputLine RowCount = RowCount + 1 Loop tsread.Close End Sub "PcolaITGuy" wrote: I have a simple text file that contains a list of hostnames and each are on their own seperate line. I'm having a great deal of trouble figuring out how code a way to begin reading the file on the first line, pass the value into a variable, perform a command using the variable, then LOOP back to read the next line and so on until EOF. Any help would be greatly appreciated!!! Thanks, Pcola |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
Read value from text file line, process, then Loop until EOF
If your text file is less than 20 to 30 Megs in size, than quicker than
reading the file in one-line at a time is reading the entire file in all at once, splitting it into individual lines and then process those.... Sub DoSomethingToLinesInFile() Dim X As Long Dim FileNum As Long Dim TotalFile As String Dim Records() As String FileNum = FreeFile Open "c:\Temp\TestText.txt" For Binary As #FileNum TotalFile = Space(LOF(FileNum)) Get #FileNum, , TotalFile Close #FileNum ' Now that the entire file is now contained in the TotalFile ' variable, let's split the file into individual lines Records = Split(TotalFile, vbCrLf) ' Okay, now process each line from the file For X = 0 To UBound(Records) ' ' Process each line from the file as needed... they are in Records(X) ' Next End Sub The only thing you need to do is replace my "c:\Temp\TestText.txt" sample path and filename with your own file's path and filename, and put the code you want to use to process each line from the file inside the For-Next loop where indicated. Rick "PcolaITGuy" wrote in message ... I have a simple text file that contains a list of hostnames and each are on their own seperate line. I'm having a great deal of trouble figuring out how code a way to begin reading the file on the first line, pass the value into a variable, perform a command using the variable, then LOOP back to read the next line and so on until EOF. Any help would be greatly appreciated!!! Thanks, Pcola |
#4
Posted to microsoft.public.excel.programming
|
|||
|
|||
Read value from text file line, process, then Loop until EOF
Joel,
I really appreciate the time you took to answer my question, however I'm confused what your code is doing. What is 'DATA (8)' is that the variable I'm passing the value into? If so, I do not see where the value is being passed. Also, I'm not clear on where I would be able to perform my command using the variable within the loop. Could you elaborate? Thanks, Pcola "Joel" wrote: Sub Gettext() Const ForReading = 1, ForWriting = 2, ForAppending = 3 Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0 Dim Data(8) 'default folder Folder = "C:\temp" ChDir (Folder) Set fsread = CreateObject("Scripting.FileSystemObject") FName = Application.GetOpenFilename("CSV (*.csv),*.csv") Set fread = fsread.GetFile(FName) Set tsread = fread.OpenAsTextStream(ForReading, TristateUseDefault) RowCount = 1 Do While tsread.atendofstream = False InputLine = tsread.ReadLine Range("A" & RowCount) = InputLine RowCount = RowCount + 1 Loop tsread.Close End Sub "PcolaITGuy" wrote: I have a simple text file that contains a list of hostnames and each are on their own seperate line. I'm having a great deal of trouble figuring out how code a way to begin reading the file on the first line, pass the value into a variable, perform a command using the variable, then LOOP back to read the next line and so on until EOF. Any help would be greatly appreciated!!! Thanks, Pcola |
#5
Posted to microsoft.public.excel.programming
|
|||
|
|||
Read value from text file line, process, then Loop until EOF
I'm not getting any result from this code. I added a watch to each of the
variables and expressions but nothing appears to be passed into them. I even added a simple text box to the form and within the loop portion of the code I tried setting the text of the textbox to the Records(X) value. I don't get any errors but I also don't get any result. I did replace the example path and filename with my actual one. My text file has 4 lines in it as follows: Server1 Server2 Server3 Server4 Any ideas? I should have mentioned that I have Excel 2000. Thanks, Scott "Rick Rothstein (MVP - VB)" wrote: If your text file is less than 20 to 30 Megs in size, than quicker than reading the file in one-line at a time is reading the entire file in all at once, splitting it into individual lines and then process those.... Sub DoSomethingToLinesInFile() Dim X As Long Dim FileNum As Long Dim TotalFile As String Dim Records() As String FileNum = FreeFile Open "c:\Temp\TestText.txt" For Binary As #FileNum TotalFile = Space(LOF(FileNum)) Get #FileNum, , TotalFile Close #FileNum ' Now that the entire file is now contained in the TotalFile ' variable, let's split the file into individual lines Records = Split(TotalFile, vbCrLf) ' Okay, now process each line from the file For X = 0 To UBound(Records) ' ' Process each line from the file as needed... they are in Records(X) ' Next End Sub The only thing you need to do is replace my "c:\Temp\TestText.txt" sample path and filename with your own file's path and filename, and put the code you want to use to process each line from the file inside the For-Next loop where indicated. Rick "PcolaITGuy" wrote in message ... I have a simple text file that contains a list of hostnames and each are on their own seperate line. I'm having a great deal of trouble figuring out how code a way to begin reading the file on the first line, pass the value into a variable, perform a command using the variable, then LOOP back to read the next line and so on until EOF. Any help would be greatly appreciated!!! Thanks, Pcola |
#6
Posted to microsoft.public.excel.programming
|
|||
|
|||
Read value from text file line, process, then Loop until EOF
It might have helped if you showed us the code you tried. In any event, here
is a sample I just tried and it worked fine. Add a UserForm to your project and put a TextBox and CommandButton on it (default names should be TextBox1 and CommandButton1); then copy/paste this code into the UserForm's code window... Private Sub CommandButton1_Click() Dim X As Long Dim FileNum As Long Dim TotalFile As String Dim Records() As String FileNum = FreeFile Open "c:\Temp\Test.txt" For Binary As #FileNum TotalFile = Space(LOF(FileNum)) Get #FileNum, , TotalFile Close #FileNum ' Now that the entire file is now contained in the TotalFile ' variable, let's split the file into individual lines Records = Split(TotalFile, vbCrLf) ' Okay, now process each line from the file For X = 0 To UBound(Records) If X 0 Then TextBox1.Value = TextBox1 & vbCrLf TextBox1.Value = TextBox1.Value & Records(X) Next End Sub Again, change the path and filename to your file's path and filename. Now, run the UserForm and click the button. On my system, the TextBox is filled with the lines from the file. Rick "PcolaITGuy" wrote in message ... I'm not getting any result from this code. I added a watch to each of the variables and expressions but nothing appears to be passed into them. I even added a simple text box to the form and within the loop portion of the code I tried setting the text of the textbox to the Records(X) value. I don't get any errors but I also don't get any result. I did replace the example path and filename with my actual one. My text file has 4 lines in it as follows: Server1 Server2 Server3 Server4 Any ideas? I should have mentioned that I have Excel 2000. Thanks, Scott "Rick Rothstein (MVP - VB)" wrote: If your text file is less than 20 to 30 Megs in size, than quicker than reading the file in one-line at a time is reading the entire file in all at once, splitting it into individual lines and then process those.... Sub DoSomethingToLinesInFile() Dim X As Long Dim FileNum As Long Dim TotalFile As String Dim Records() As String FileNum = FreeFile Open "c:\Temp\TestText.txt" For Binary As #FileNum TotalFile = Space(LOF(FileNum)) Get #FileNum, , TotalFile Close #FileNum ' Now that the entire file is now contained in the TotalFile ' variable, let's split the file into individual lines Records = Split(TotalFile, vbCrLf) ' Okay, now process each line from the file For X = 0 To UBound(Records) ' ' Process each line from the file as needed... they are in Records(X) ' Next End Sub The only thing you need to do is replace my "c:\Temp\TestText.txt" sample path and filename with your own file's path and filename, and put the code you want to use to process each line from the file inside the For-Next loop where indicated. Rick "PcolaITGuy" wrote in message ... I have a simple text file that contains a list of hostnames and each are on their own seperate line. I'm having a great deal of trouble figuring out how code a way to begin reading the file on the first line, pass the value into a variable, perform a command using the variable, then LOOP back to read the next line and so on until EOF. Any help would be greatly appreciated!!! Thanks, Pcola |
#7
Posted to microsoft.public.excel.programming
|
|||
|
|||
Read value from text file line, process, then Loop until EOF
Ooops...my bad...I got it working now :-) Thanks so much! Here is an
example of my code with comments: Public Sub UserForm_Activate() ' The BatchSched form and code is used to remotely schedule tasks on single or multiple servers ' The servernames are obtained from a local file called C:\scripts\selection.txt ' The selection.txt file is created from another macro where users view a spreadsheet and select ' the server(s) they wish to perform the task on ' Upon loading of this module, the user inputs the remote batch file and start time that ' they wish to schedule on the remote server(s) Dim inputcmd As String 'variable that will be populated from the txt_rmtcmd object on form Dim st_hr As String 'Start Hour variable that will be populated from the txt_start_hr object on form Dim st_min As String 'Start Minute variable that will be populated from the txt_start_min object on form cmd_execute.Enabled = False 'disables the EXECUTE button on the form by default until user enables the chk_confirm check box. End Sub Private Sub chk_Confirm_Change() ' This is a failsafe feature to require the user to check the confirmation box before enabling ' the Execute command button If chk_confirm.Value = True Then cmd_execute.Enabled = True Else: cmd_execute.Enabled = False End If End Sub Private Sub cmd_execute_Click() 'cmd_execute is a command button on a form that is clicked to excute the commands assuming ' other inputs have been performed Dim X As Long Dim FileNum As Long Dim TotalFile As String Dim Records() As String Dim cmdstring As String 'This is a variable that I will use to construct my shell command in the loop inputcmd = txt_rmtcmd.Text 'Gets a path and batch filename, from user input, to execute on a remote server 'It uses the txt_rmtcmd object (textbox) on the form st_hr = txt_start_hour.Text 'Gets the hour of the time, from user input, to execute the command in 24hour format 'It uses the txt_start_hour object (textbox) on the form st_min = txt_start_min.Text 'Gets the minute of the time, from user input, to execute the command in 24hour format 'It uses the txt_start_min object (textbox) on the form FileNum = FreeFile Open "c:\scripts\selection.txt" For Binary As #FileNum TotalFile = Space(LOF(FileNum)) Get #FileNum, , TotalFile Close #FileNum ' Now that the entire file is now contained in the TotalFile ' variable, let's split the file into individual lines Records = Split(TotalFile, vbCrLf) ' Okay, now process each line from the file For X = 0 To UBound(Records) cmdstring = "cmd.exe /c AT " + "\\" + Records(X) + " " + st_hr + ":" + st_min + " " + inputcmd 'assembly of a shell command to schedule a remote task for each server (Records(X)) 'for the desired time (st_hr+":"st_min) using the command input by user (inputcmd) Shell (cmdstring), 1 ' The cmdstring is then sent to command shell for processing ' ' Process each line from the file as needed... they are in Records(X) ' Next ' Repeats the process for each line (server) listed in the selection.txt file until the end of file End Sub Private Sub cmd_exit_Click() Unload BatchSched 'closes the form when the Exit button is clicked End Sub "Rick Rothstein (MVP - VB)" wrote: If your text file is less than 20 to 30 Megs in size, than quicker than reading the file in one-line at a time is reading the entire file in all at once, splitting it into individual lines and then process those.... Sub DoSomethingToLinesInFile() Dim X As Long Dim FileNum As Long Dim TotalFile As String Dim Records() As String FileNum = FreeFile Open "c:\Temp\TestText.txt" For Binary As #FileNum TotalFile = Space(LOF(FileNum)) Get #FileNum, , TotalFile Close #FileNum ' Now that the entire file is now contained in the TotalFile ' variable, let's split the file into individual lines Records = Split(TotalFile, vbCrLf) ' Okay, now process each line from the file For X = 0 To UBound(Records) ' ' Process each line from the file as needed... they are in Records(X) ' Next End Sub The only thing you need to do is replace my "c:\Temp\TestText.txt" sample path and filename with your own file's path and filename, and put the code you want to use to process each line from the file inside the For-Next loop where indicated. Rick "PcolaITGuy" wrote in message ... I have a simple text file that contains a list of hostnames and each are on their own seperate line. I'm having a great deal of trouble figuring out how code a way to begin reading the file on the first line, pass the value into a variable, perform a command using the variable, then LOOP back to read the next line and so on until EOF. Any help would be greatly appreciated!!! Thanks, Pcola |
#8
Posted to microsoft.public.excel.programming
|
|||
|
|||
Read value from text file line, process, then Loop until EOF
Sorry about the line length in my posted code...if you would like a better
formatted version to avoid confusion, let me know and I will email it to you. "PcolaITGuy" wrote: Ooops...my bad...I got it working now :-) Thanks so much! Here is an example of my code with comments: Public Sub UserForm_Activate() ' The BatchSched form and code is used to remotely schedule tasks on single or multiple servers ' The servernames are obtained from a local file called C:\scripts\selection.txt ' The selection.txt file is created from another macro where users view a spreadsheet and select ' the server(s) they wish to perform the task on ' Upon loading of this module, the user inputs the remote batch file and start time that ' they wish to schedule on the remote server(s) Dim inputcmd As String 'variable that will be populated from the txt_rmtcmd object on form Dim st_hr As String 'Start Hour variable that will be populated from the txt_start_hr object on form Dim st_min As String 'Start Minute variable that will be populated from the txt_start_min object on form cmd_execute.Enabled = False 'disables the EXECUTE button on the form by default until user enables the chk_confirm check box. End Sub Private Sub chk_Confirm_Change() ' This is a failsafe feature to require the user to check the confirmation box before enabling ' the Execute command button If chk_confirm.Value = True Then cmd_execute.Enabled = True Else: cmd_execute.Enabled = False End If End Sub Private Sub cmd_execute_Click() 'cmd_execute is a command button on a form that is clicked to excute the commands assuming ' other inputs have been performed Dim X As Long Dim FileNum As Long Dim TotalFile As String Dim Records() As String Dim cmdstring As String 'This is a variable that I will use to construct my shell command in the loop inputcmd = txt_rmtcmd.Text 'Gets a path and batch filename, from user input, to execute on a remote server 'It uses the txt_rmtcmd object (textbox) on the form st_hr = txt_start_hour.Text 'Gets the hour of the time, from user input, to execute the command in 24hour format 'It uses the txt_start_hour object (textbox) on the form st_min = txt_start_min.Text 'Gets the minute of the time, from user input, to execute the command in 24hour format 'It uses the txt_start_min object (textbox) on the form FileNum = FreeFile Open "c:\scripts\selection.txt" For Binary As #FileNum TotalFile = Space(LOF(FileNum)) Get #FileNum, , TotalFile Close #FileNum ' Now that the entire file is now contained in the TotalFile ' variable, let's split the file into individual lines Records = Split(TotalFile, vbCrLf) ' Okay, now process each line from the file For X = 0 To UBound(Records) cmdstring = "cmd.exe /c AT " + "\\" + Records(X) + " " + st_hr + ":" + st_min + " " + inputcmd 'assembly of a shell command to schedule a remote task for each server (Records(X)) 'for the desired time (st_hr+":"st_min) using the command input by user (inputcmd) Shell (cmdstring), 1 ' The cmdstring is then sent to command shell for processing ' ' Process each line from the file as needed... they are in Records(X) ' Next ' Repeats the process for each line (server) listed in the selection.txt file until the end of file End Sub Private Sub cmd_exit_Click() Unload BatchSched 'closes the form when the Exit button is clicked End Sub "Rick Rothstein (MVP - VB)" wrote: If your text file is less than 20 to 30 Megs in size, than quicker than reading the file in one-line at a time is reading the entire file in all at once, splitting it into individual lines and then process those.... Sub DoSomethingToLinesInFile() Dim X As Long Dim FileNum As Long Dim TotalFile As String Dim Records() As String FileNum = FreeFile Open "c:\Temp\TestText.txt" For Binary As #FileNum TotalFile = Space(LOF(FileNum)) Get #FileNum, , TotalFile Close #FileNum ' Now that the entire file is now contained in the TotalFile ' variable, let's split the file into individual lines Records = Split(TotalFile, vbCrLf) ' Okay, now process each line from the file For X = 0 To UBound(Records) ' ' Process each line from the file as needed... they are in Records(X) ' Next End Sub The only thing you need to do is replace my "c:\Temp\TestText.txt" sample path and filename with your own file's path and filename, and put the code you want to use to process each line from the file inside the For-Next loop where indicated. Rick "PcolaITGuy" wrote in message ... I have a simple text file that contains a list of hostnames and each are on their own seperate line. I'm having a great deal of trouble figuring out how code a way to begin reading the file on the first line, pass the value into a variable, perform a command using the variable, then LOOP back to read the next line and so on until EOF. Any help would be greatly appreciated!!! Thanks, Pcola |
#9
Posted to microsoft.public.excel.programming
|
|||
|
|||
Read value from text file line, process, then Loop until EOF
Earlier, you wrote "Ooops...my bad...I got it working now"; so I am confused
why you are offering to send the code out. Do you still have a pending question? Rick "PcolaITGuy" wrote in message ... Sorry about the line length in my posted code...if you would like a better formatted version to avoid confusion, let me know and I will email it to you. "PcolaITGuy" wrote: Ooops...my bad...I got it working now :-) Thanks so much! Here is an example of my code with comments: Public Sub UserForm_Activate() ' The BatchSched form and code is used to remotely schedule tasks on single or multiple servers ' The servernames are obtained from a local file called C:\scripts\selection.txt ' The selection.txt file is created from another macro where users view a spreadsheet and select ' the server(s) they wish to perform the task on ' Upon loading of this module, the user inputs the remote batch file and start time that ' they wish to schedule on the remote server(s) Dim inputcmd As String 'variable that will be populated from the txt_rmtcmd object on form Dim st_hr As String 'Start Hour variable that will be populated from the txt_start_hr object on form Dim st_min As String 'Start Minute variable that will be populated from the txt_start_min object on form cmd_execute.Enabled = False 'disables the EXECUTE button on the form by default until user enables the chk_confirm check box. End Sub Private Sub chk_Confirm_Change() ' This is a failsafe feature to require the user to check the confirmation box before enabling ' the Execute command button If chk_confirm.Value = True Then cmd_execute.Enabled = True Else: cmd_execute.Enabled = False End If End Sub Private Sub cmd_execute_Click() 'cmd_execute is a command button on a form that is clicked to excute the commands assuming ' other inputs have been performed Dim X As Long Dim FileNum As Long Dim TotalFile As String Dim Records() As String Dim cmdstring As String 'This is a variable that I will use to construct my shell command in the loop inputcmd = txt_rmtcmd.Text 'Gets a path and batch filename, from user input, to execute on a remote server 'It uses the txt_rmtcmd object (textbox) on the form st_hr = txt_start_hour.Text 'Gets the hour of the time, from user input, to execute the command in 24hour format 'It uses the txt_start_hour object (textbox) on the form st_min = txt_start_min.Text 'Gets the minute of the time, from user input, to execute the command in 24hour format 'It uses the txt_start_min object (textbox) on the form FileNum = FreeFile Open "c:\scripts\selection.txt" For Binary As #FileNum TotalFile = Space(LOF(FileNum)) Get #FileNum, , TotalFile Close #FileNum ' Now that the entire file is now contained in the TotalFile ' variable, let's split the file into individual lines Records = Split(TotalFile, vbCrLf) ' Okay, now process each line from the file For X = 0 To UBound(Records) cmdstring = "cmd.exe /c AT " + "\\" + Records(X) + " " + st_hr + ":" + st_min + " " + inputcmd 'assembly of a shell command to schedule a remote task for each server (Records(X)) 'for the desired time (st_hr+":"st_min) using the command input by user (inputcmd) Shell (cmdstring), 1 ' The cmdstring is then sent to command shell for processing ' ' Process each line from the file as needed... they are in Records(X) ' Next ' Repeats the process for each line (server) listed in the selection.txt file until the end of file End Sub Private Sub cmd_exit_Click() Unload BatchSched 'closes the form when the Exit button is clicked End Sub "Rick Rothstein (MVP - VB)" wrote: If your text file is less than 20 to 30 Megs in size, than quicker than reading the file in one-line at a time is reading the entire file in all at once, splitting it into individual lines and then process those.... Sub DoSomethingToLinesInFile() Dim X As Long Dim FileNum As Long Dim TotalFile As String Dim Records() As String FileNum = FreeFile Open "c:\Temp\TestText.txt" For Binary As #FileNum TotalFile = Space(LOF(FileNum)) Get #FileNum, , TotalFile Close #FileNum ' Now that the entire file is now contained in the TotalFile ' variable, let's split the file into individual lines Records = Split(TotalFile, vbCrLf) ' Okay, now process each line from the file For X = 0 To UBound(Records) ' ' Process each line from the file as needed... they are in Records(X) ' Next End Sub The only thing you need to do is replace my "c:\Temp\TestText.txt" sample path and filename with your own file's path and filename, and put the code you want to use to process each line from the file inside the For-Next loop where indicated. Rick "PcolaITGuy" wrote in message ... I have a simple text file that contains a list of hostnames and each are on their own seperate line. I'm having a great deal of trouble figuring out how code a way to begin reading the file on the first line, pass the value into a variable, perform a command using the variable, then LOOP back to read the next line and so on until EOF. Any help would be greatly appreciated!!! Thanks, Pcola |
#10
Posted to microsoft.public.excel.programming
|
|||
|
|||
Read value from text file line, process, then Loop until EOF
Hi Rick,
I always post the final result in case others find it usefull. When I said "Oops my bad"...I meant that your code example was correct all along and that I had screwed up one of my other methods. Scott "Rick Rothstein (MVP - VB)" wrote: Earlier, you wrote "Ooops...my bad...I got it working now"; so I am confused why you are offering to send the code out. Do you still have a pending question? Rick "PcolaITGuy" wrote in message ... Sorry about the line length in my posted code...if you would like a better formatted version to avoid confusion, let me know and I will email it to you. "PcolaITGuy" wrote: Ooops...my bad...I got it working now :-) Thanks so much! Here is an example of my code with comments: Public Sub UserForm_Activate() ' The BatchSched form and code is used to remotely schedule tasks on single or multiple servers ' The servernames are obtained from a local file called C:\scripts\selection.txt ' The selection.txt file is created from another macro where users view a spreadsheet and select ' the server(s) they wish to perform the task on ' Upon loading of this module, the user inputs the remote batch file and start time that ' they wish to schedule on the remote server(s) Dim inputcmd As String 'variable that will be populated from the txt_rmtcmd object on form Dim st_hr As String 'Start Hour variable that will be populated from the txt_start_hr object on form Dim st_min As String 'Start Minute variable that will be populated from the txt_start_min object on form cmd_execute.Enabled = False 'disables the EXECUTE button on the form by default until user enables the chk_confirm check box. End Sub Private Sub chk_Confirm_Change() ' This is a failsafe feature to require the user to check the confirmation box before enabling ' the Execute command button If chk_confirm.Value = True Then cmd_execute.Enabled = True Else: cmd_execute.Enabled = False End If End Sub Private Sub cmd_execute_Click() 'cmd_execute is a command button on a form that is clicked to excute the commands assuming ' other inputs have been performed Dim X As Long Dim FileNum As Long Dim TotalFile As String Dim Records() As String Dim cmdstring As String 'This is a variable that I will use to construct my shell command in the loop inputcmd = txt_rmtcmd.Text 'Gets a path and batch filename, from user input, to execute on a remote server 'It uses the txt_rmtcmd object (textbox) on the form st_hr = txt_start_hour.Text 'Gets the hour of the time, from user input, to execute the command in 24hour format 'It uses the txt_start_hour object (textbox) on the form st_min = txt_start_min.Text 'Gets the minute of the time, from user input, to execute the command in 24hour format 'It uses the txt_start_min object (textbox) on the form FileNum = FreeFile Open "c:\scripts\selection.txt" For Binary As #FileNum TotalFile = Space(LOF(FileNum)) Get #FileNum, , TotalFile Close #FileNum ' Now that the entire file is now contained in the TotalFile ' variable, let's split the file into individual lines Records = Split(TotalFile, vbCrLf) ' Okay, now process each line from the file For X = 0 To UBound(Records) cmdstring = "cmd.exe /c AT " + "\\" + Records(X) + " " + st_hr + ":" + st_min + " " + inputcmd 'assembly of a shell command to schedule a remote task for each server (Records(X)) 'for the desired time (st_hr+":"st_min) using the command input by user (inputcmd) Shell (cmdstring), 1 ' The cmdstring is then sent to command shell for processing ' ' Process each line from the file as needed... they are in Records(X) ' Next ' Repeats the process for each line (server) listed in the selection.txt file until the end of file End Sub Private Sub cmd_exit_Click() Unload BatchSched 'closes the form when the Exit button is clicked End Sub "Rick Rothstein (MVP - VB)" wrote: If your text file is less than 20 to 30 Megs in size, than quicker than reading the file in one-line at a time is reading the entire file in all at once, splitting it into individual lines and then process those.... Sub DoSomethingToLinesInFile() Dim X As Long Dim FileNum As Long Dim TotalFile As String Dim Records() As String FileNum = FreeFile Open "c:\Temp\TestText.txt" For Binary As #FileNum TotalFile = Space(LOF(FileNum)) Get #FileNum, , TotalFile Close #FileNum ' Now that the entire file is now contained in the TotalFile ' variable, let's split the file into individual lines Records = Split(TotalFile, vbCrLf) ' Okay, now process each line from the file For X = 0 To UBound(Records) ' ' Process each line from the file as needed... they are in Records(X) ' Next End Sub The only thing you need to do is replace my "c:\Temp\TestText.txt" sample path and filename with your own file's path and filename, and put the code you want to use to process each line from the file inside the For-Next loop where indicated. Rick "PcolaITGuy" wrote in message ... I have a simple text file that contains a list of hostnames and each are on their own seperate line. I'm having a great deal of trouble figuring out how code a way to begin reading the file on the first line, pass the value into a variable, perform a command using the variable, then LOOP back to read the next line and so on until EOF. Any help would be greatly appreciated!!! Thanks, Pcola |
#11
Posted to microsoft.public.excel.programming
|
|||
|
|||
Read value from text file line, process, then Loop until EOF
Okay, I see what you meant now. As long as everything is working the way you
want it to, and as long as no more questions are pending, then there is no need to send it to me. Rick "PcolaITGuy" wrote in message ... Hi Rick, I always post the final result in case others find it usefull. When I said "Oops my bad"...I meant that your code example was correct all along and that I had screwed up one of my other methods. Scott "Rick Rothstein (MVP - VB)" wrote: Earlier, you wrote "Ooops...my bad...I got it working now"; so I am confused why you are offering to send the code out. Do you still have a pending question? Rick "PcolaITGuy" wrote in message ... Sorry about the line length in my posted code...if you would like a better formatted version to avoid confusion, let me know and I will email it to you. "PcolaITGuy" wrote: Ooops...my bad...I got it working now :-) Thanks so much! Here is an example of my code with comments: Public Sub UserForm_Activate() ' The BatchSched form and code is used to remotely schedule tasks on single or multiple servers ' The servernames are obtained from a local file called C:\scripts\selection.txt ' The selection.txt file is created from another macro where users view a spreadsheet and select ' the server(s) they wish to perform the task on ' Upon loading of this module, the user inputs the remote batch file and start time that ' they wish to schedule on the remote server(s) Dim inputcmd As String 'variable that will be populated from the txt_rmtcmd object on form Dim st_hr As String 'Start Hour variable that will be populated from the txt_start_hr object on form Dim st_min As String 'Start Minute variable that will be populated from the txt_start_min object on form cmd_execute.Enabled = False 'disables the EXECUTE button on the form by default until user enables the chk_confirm check box. End Sub Private Sub chk_Confirm_Change() ' This is a failsafe feature to require the user to check the confirmation box before enabling ' the Execute command button If chk_confirm.Value = True Then cmd_execute.Enabled = True Else: cmd_execute.Enabled = False End If End Sub Private Sub cmd_execute_Click() 'cmd_execute is a command button on a form that is clicked to excute the commands assuming ' other inputs have been performed Dim X As Long Dim FileNum As Long Dim TotalFile As String Dim Records() As String Dim cmdstring As String 'This is a variable that I will use to construct my shell command in the loop inputcmd = txt_rmtcmd.Text 'Gets a path and batch filename, from user input, to execute on a remote server 'It uses the txt_rmtcmd object (textbox) on the form st_hr = txt_start_hour.Text 'Gets the hour of the time, from user input, to execute the command in 24hour format 'It uses the txt_start_hour object (textbox) on the form st_min = txt_start_min.Text 'Gets the minute of the time, from user input, to execute the command in 24hour format 'It uses the txt_start_min object (textbox) on the form FileNum = FreeFile Open "c:\scripts\selection.txt" For Binary As #FileNum TotalFile = Space(LOF(FileNum)) Get #FileNum, , TotalFile Close #FileNum ' Now that the entire file is now contained in the TotalFile ' variable, let's split the file into individual lines Records = Split(TotalFile, vbCrLf) ' Okay, now process each line from the file For X = 0 To UBound(Records) cmdstring = "cmd.exe /c AT " + "\\" + Records(X) + " " + st_hr + ":" + st_min + " " + inputcmd 'assembly of a shell command to schedule a remote task for each server (Records(X)) 'for the desired time (st_hr+":"st_min) using the command input by user (inputcmd) Shell (cmdstring), 1 ' The cmdstring is then sent to command shell for processing ' ' Process each line from the file as needed... they are in Records(X) ' Next ' Repeats the process for each line (server) listed in the selection.txt file until the end of file End Sub Private Sub cmd_exit_Click() Unload BatchSched 'closes the form when the Exit button is clicked End Sub "Rick Rothstein (MVP - VB)" wrote: If your text file is less than 20 to 30 Megs in size, than quicker than reading the file in one-line at a time is reading the entire file in all at once, splitting it into individual lines and then process those.... Sub DoSomethingToLinesInFile() Dim X As Long Dim FileNum As Long Dim TotalFile As String Dim Records() As String FileNum = FreeFile Open "c:\Temp\TestText.txt" For Binary As #FileNum TotalFile = Space(LOF(FileNum)) Get #FileNum, , TotalFile Close #FileNum ' Now that the entire file is now contained in the TotalFile ' variable, let's split the file into individual lines Records = Split(TotalFile, vbCrLf) ' Okay, now process each line from the file For X = 0 To UBound(Records) ' ' Process each line from the file as needed... they are in Records(X) ' Next End Sub The only thing you need to do is replace my "c:\Temp\TestText.txt" sample path and filename with your own file's path and filename, and put the code you want to use to process each line from the file inside the For-Next loop where indicated. Rick "PcolaITGuy" wrote in message ... I have a simple text file that contains a list of hostnames and each are on their own seperate line. I'm having a great deal of trouble figuring out how code a way to begin reading the file on the first line, pass the value into a variable, perform a command using the variable, then LOOP back to read the next line and so on until EOF. Any help would be greatly appreciated!!! Thanks, Pcola |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Read/Write Process | Excel Discussion (Misc queries) | |||
How can i read Command Line parametres from Excel 2003 file??? | Excel Programming | |||
Textbox-read text line by line | Excel Programming | |||
Process a text file before import - which language to use? | Excel Programming | |||
read last line of a file | Excel Programming |