Batch and Text file contents manipulation

I am looking for a simple batch file that could take a text file name
containing one or more users as a parameter then reads the firstname and lastname
from the text file one at a time until EOF then uses the user name to execute
a program like the one below i.e.

app-program -copy -r -o e:\SG2\store9\firstname_lastname.txt e:\SG2\store9\store9.edb -t E:\TEMP "firstname lastname" SERVER1;firstname.lastname $ROOT

The batch file will substitue the firstname and lastname into the command line above
and execute it.

Please let me know if my explanation is not clear enough, thanks.
netimpactAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Lee W, MVPConnect With a Mentor Technology and Business Process AdvisorCommented:
I assumed "app-program" was your program since -copy seems to be a parameter - and I figured app-program was something you ported from a *nix environment since you reference $ROOT and that's not a Windows/DOS thing.

I prefer locating a sleep utility, but the ping trick works too.

If you need to do more commands in the loop, use the code below.
for /f "tokens=1,2" %%a in (usernames.txt) do (
   app-program -copy -r -o e:\SG2\store9\%%a_%%b.txt e:\SG2\store9\store9.edb -t E:\TEMP "%%a %%b" SERVER1;%%a.%%b $ROOT
   ping -n 3 localhost>nul
)

Open in new window

0
 
RobSampsonCommented:
Hi, try this.  Specify your input file name, and you will get a MsgBox telling you the command it will run, so only test on one or two names in the file.

When you want to actually run the command, uncomment the following line:
      'objShell.Run strCommand, 1, True

'=================
strInputFile = "Usernames.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForReading = 1
Set objShell = CreateObject("WScript.Shell")
Set objInputFile = objFSO.OpenTextFile(strInputFile, intForReading, False)
While Not objInputFile.AtEndOfStream
      arrNames = Split(objInputFile.ReadLine, " ")
      strFirst = arrNames(0)
      If UBound(arrNames) > 0 Then
            strLast = arrNames(1)
      Else
            strLast = ""
      End If
      strCommand = "app-program -copy -r -o e:\SG2\store9\" & strFirst & "_" & strLast & ".txt e:\SG2\store9\store9.edb -t E:\TEMP """ & strFirst & " " & strLast & """ SERVER1;" & strFirst & "." & strLast & " $ROOT"
      MsgBox "About to run:" & VbCrLf & strCommand
      'objShell.Run strCommand, 1, True
Wend
objInputFile.Close
Set objInputFile = Nothing
Set objFSO = Nothing
Set objShell = Nothing
MsgBox "Finished."
'=================

Regards,

Rob.
0
 
Lee W, MVPTechnology and Business Process AdvisorCommented:
Yep, that could... of course, I like the KISS principal.

The code below assumes names are entered in a text file called "usernames.txt" in the following sample format:
john doe
jane smith
harry johnson


I also suggest you type for /? for more details on how the for command can work.
for /f "tokens=1,2" %%a in (usernames.txt) do app-program -copy -r -o e:\SG2\store9\%%a_%%b.txt e:\SG2\store9\store9.edb -t E:\TEMP "%%a %%b" SERVER1;%%a.%%b $ROOT

Open in new window

0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
RobSampsonCommented:
Ha ha! Thanks leew.  While I love to keep things simple, as you know, VBS isn't *that* elegant with file parsing, and I'm not Batch expert.....so VBS is my game....

Thanks for the simple one liner.

Regards,

Rob.
0
 
netimpactAuthor Commented:
leew:

Just what I wanted, thnaks. I just thought I will test it. I put it in a bat file called readtextfile2
and when I run it from the DOS prompt it comes up with the OUTPUT below. I know
the error message "'app-program' is not recognized as an internal or external command,
operable program or batch file." is to be ignored, but it echos the raw command from the
batch file then resolved it on the netx line. Should I worry about this or ignore?

Also do we need a delay for each command to run?


F:\MS-DOS>readtextfile2

F:\MS-DOS>for /F "tokens=1,2" %a in (usernames.txt) do app-program -copy -r -o e:\SG2\store9\%a_%b.txt e:\SG2\store9\store9.edb -t E:\TEMP "%a %b" SER
VER1;%a.%b $ROOT

F:\MS-DOS>app-program -copy -r -o e:\SG2\store9\john_doe.txt e:\SG2\store9\store9.edb -t E:\TEMP "john doe" SERVER1;john.doe $ROOT
'app-program' is not recognized as an internal or external command,
operable program or batch file.

F:\MS-DOS>app-program -copy -r -o e:\SG2\store9\jane_smith.txt e:\SG2\store9\store9.edb -t E:\TEMP "jane smith" SERVER1;jane.smith $ROOT
'app-program' is not recognized as an internal or external command,
operable program or batch file.

F:\MS-DOS>app-program -copy -r -o e:\SG2\store9\harry_johnson.txt e:\SG2\store9\store9.edb -t E:\TEMP "harry johnson" SERVER1;harry.johnson $ROOT
'app-program' is not recognized as an internal or external command,
operable program or batch file.
0
 
RobSampsonCommented:
Hi guys,

I can't remember batch exactly, but I think in a batch file, if you have that command, then have this above it:
@echo off

you will not see the echo'ed command, just the output of it (or error, in this case).  Obviously too, change app-program to the EXE path of the required program.

If you need to wait, you could use this command, but I'm not sure how to insert it into the "loop".....

ping -n 3 localhost >nul

Leew, can you help with that?

Regards,

Rob.
0
 
netimpactAuthor Commented:
leew:

Thanks, everything is working the way I expect it to now.
As a matter of interest can you say what this NUL statement: localhost>nul means?
thanks
0
 
RobSampsonCommented:
Mind if I jump in?

That output redirection to NUL just sends the normal text output of the Ping command to no man's land.

If you use
Ping -n 3 localhost

it will output the ping results to screen of three pings (3 seconds)

If you use
Ping -n 3 localhost >NUL

it will not output anything to the screen for 3 seconds....

Regards,

Rob.
0
 
netimpactAuthor Commented:
Thanks for your input RobSampson, much appreciated.
0
 
RobSampsonCommented:
No problem.  Do you have any other issues?  Can you accept an answer to close the question?

Regards,

Rob.
0
 
netimpactAuthor Commented:
Sure, will do, thanks
0
All Courses

From novice to tech pro — start learning today.