Jump to content

BatchRun Type Application


jlandes
 Share

Recommended Posts

Hello all,

I've been tasked at work with writing a small application that can call a so-called command file and execute the commands in the file. The first problem that I'm having is that I want to have the ability to add comments and have those comments omitted from the commands to be run. For example, I might setup a file like this:

; This is a comment
C:\TEST\COMMAND.EXE  ; This is another comment
C:\TEST\COMMAND2.EXE ; This is a third comment

; This is a forth comment
C:\TEST\COMMAND3.EXE ; This is a fifth comment

As you can see, the ; will be used to designate a comment and that can appear at the beginning of a line or anywhere in the middle. The only thing my program should then see is:

C:\TEST\COMMAND.EXE
C:\TEST\COMMAND2.EXE
C:\TEST\COMMAND3.EXE

Heres the code that Ive come up with so far:

$hFile = FileOpen("test.txt", 0)

; Check if file opened for reading OK
If $hFile = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

; Read in lines of text until the EOF is reached
While 1
    $sLine = FileReadLine($hFile)
    If @error = -1 Then ExitLoop
    $iLocation = StringInStr($sLine, ";")

    If $iLocation > 0 Then
        $sLine = StringLeft($sLine, $iLocation - 1)
    EndIf

    If $sLine <> "" Then
        MsgBox(0, "Line read:", $sLine)
    EndIf
Wend

FileClose($hFile)

Whats the best way to do this?

The second thing I'd like to do is expand all the AutoIt macros, so that I can type in the command file @ProgramFilesDir or @WindowsDir and that will resolve to the actual directory. How can this be done?

Thanks for any help.

Sincerely yours,Jeremy Landesjlandes@landeserve.com

Link to comment
Share on other sites

One way is to set your command file up as an INI file and use Valik's IniReadSection.

This function will ignore comment lines but I am not sure about comments on valid Key/Value lines. Perhaps Valik can confirm whether these comments are also ignored.

Anyway, back to your command file, here's an example:

[Commands]
; This is a comment
Command1=C:\TEST\COMMAND.EXE ; This is another comment
Command2=C:\TEST\COMMAND2.EXE; This is a third comment

; This is a forth comment
Command3=C:\TEST\COMMAND3.EXE; This is a fifth comment
Link to comment
Share on other sites

Comments are not part of the "standard" INI file as far as I know. Besides, an INI file can't treat ; on a line as a comment start, otherwise, lines containing a semi-colon would be truncated. Then there would have to be an escape sequence. Thats just a little more complicated than an INI file should be. Keep in mind, its not just that comment lines are skipped, any line not matching the "key=value" format is ignored. These are equivalent "comments":

[Main]
Key1=Value2
; This is a comment
Key2=Value2
This is a comment

Jeremy, have a look here for something you can either use for reference. It shows one way to expand "variables" in an INI file which may help you with your second problem. I say use it for reference because its very old (Pre-ReDim). Its also not very elegant and clunky to use. However, how/if you use it is up to you.

Link to comment
Share on other sites

Why not make it easy?

: Save as commands.cmd. You can use ":" or "REM" at the start of the line to mark a comment.
C:\Test\Command1.exe
C:\Test\Command2.exe
And the AutoIt script:
RunWait('commands.cmd')
Well, the AutoIt script isn't really needed...

And for replacing AutoIt macros you just have to read the file into a string and use StringReplace. Then write the file to the tempdir and run it.

Edited by sugi
Link to comment
Share on other sites

As you can see, the ; will be used to designate a comment and that can appear at the beginning of a line or anywhere in the middle.  The only thing my program should then see is:

Whats the best way to do this?

Actually, you might want to use StringSplit. StringSplit will take a line of text and will split it on the delimiter you specify and will store the results in an array.

eg.

$myString = "C:\Directory\File.exe ; this is a comment"

$cmds = StringSplit ($myString, ";")

This will yield:

$cmds[1] = "C:\Directory\File.exe"

$cmds[2] = " this is a comment"

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...