Re: Read value from text file line, process, then Loop until EOF

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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" <PcolaITGuy@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:23C3127A-ED13-4F71-8969-586B30347213@xxxxxxxxxxxxxxxx
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


.



Relevant Pages