Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts

    Holy smokes, batch file help with concatenation in subroutine!

    I hope someone remembers how to write batch files O.o
    .bat, trying to build a custom command line switch file for setting up relevant switches. Some switches require no second arugment for them, this so far is not a problem, I can read this in without any problem.
    The problem I'm having is building this up into a useful string. I've done two approaches, one is with a subroutine and references:
    Code:
    :CREATE_CMD_FRIENDLY_SWITCH
    SET "%1=%%%1%% -%2"
    IF NOT [%3]==[] (
        SET "%1=%%%1%% %3"
    )
    And the other with just setting in the loop (this code actually shows both the attempt through the subroutine and the concat):
    Code:
    FOR /F "tokens=1,2 delims=^=" %%a IN (jobinformation.txt) DO (
        REM SET "CMD_LINE_SWITCHES=%CMD_LINE_SWITCHES% -%%a"
        REM IF NOT [%%b]==[] (
        REM     SET "CMD_LINE_SWITCHES=%CMD_LINE_SWITCHES% %%b"
        REM )
        CALL:CREATE_CMD_FRIENDLY_SWITCH CMD_LINE_SWITCHES %%a %%b
        REM ECHO.A is %%a, B is %%b
        REM SET "%%a=%%b"
    )
    
    ECHO.Command line switches: %CMD_LINE_SWITCHES%
    The result is simply: Command line switches: %CMD_LINE_SWITCHES% -HOLD (hold is the last value within the jobinformation file).

    I'm not interested in verifying the switches in anyway, they will be fed to an external program that will puke itself if something is wrong. But I'm a little at a loss as to why the CMD_LINE_SWITCHES doesn't appear to be expanding within the... 'concatenation' process. Any insite from someone who hopefully remembers the ancient things? Lol.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #2
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    I'd be remiss if I didn't ask why oh why you are using batch files?
    OracleGuy

  • #3
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,280
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Quote Originally Posted by Fou-Lu View Post
    I hope someone remembers how to write batch files O.o
    .bat, trying to build a custom command line switch file for setting up relevant switches. Some switches require no second arugment for them, this so far is not a problem, I can read this in without any problem.
    The problem I'm having is building this up into a useful string. I've done two approaches, one is with a subroutine and references:
    Code:
    :CREATE_CMD_FRIENDLY_SWITCH
    SET "%1=%%%1%% -%2"
    IF NOT [%3]==[] (
        SET "%1=%%%1%% %3"
    )
    And the other with just setting in the loop (this code actually shows both the attempt through the subroutine and the concat):
    Code:
    FOR /F "tokens=1,2 delims=^=" %%a IN (jobinformation.txt) DO (
        REM SET "CMD_LINE_SWITCHES=%CMD_LINE_SWITCHES% -%%a"
        REM IF NOT [%%b]==[] (
        REM     SET "CMD_LINE_SWITCHES=%CMD_LINE_SWITCHES% %%b"
        REM )
        CALL:CREATE_CMD_FRIENDLY_SWITCH CMD_LINE_SWITCHES %%a %%b
        REM ECHO.A is %%a, B is %%b
        REM SET "%%a=%%b"
    )
     
    ECHO.Command line switches: %CMD_LINE_SWITCHES%
    The result is simply: Command line switches: %CMD_LINE_SWITCHES% -HOLD (hold is the last value within the jobinformation file).

    I'm not interested in verifying the switches in anyway, they will be fed to an external program that will puke itself if something is wrong. But I'm a little at a loss as to why the CMD_LINE_SWITCHES doesn't appear to be expanding within the... 'concatenation' process. Any insite from someone who hopefully remembers the ancient things? Lol.

    How about

    Code:
    @ECHO OFF
    SetLocal EnableDelayedExpansion
    FOR /F "tokens=1,2 delims=," %%i IN (switches.txt) DO (
       SET SWITCHES=!SWITCHES! %%i %%j
    )
    ECHO %SWITCHES%
    PAUSE
    EXIT
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • Users who have thanked Spookster for this post:

    Fou-Lu (09-16-2010)

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by oracleguy View Post
    I'd be remiss if I didn't ask why oh why you are using batch files?
    Hah, yep.
    Its for an automated recovery job creation with our ARCServe software. We found out that doing a recovery by tree takes the information at job creation time, and not runtime, so we couldn't schedule it to run day by day (every day will try to use the same session and tape to recover instead of the newest one to move into recovery domain which is what we need). Instead, we can link .bat scripts into post execute commands for the job we want to do automatic recovery on and create the restore every day. Alternatives do include .exe files, but I'm pretty sure that my department probably won't be happy with compiled programs to run on this (hence why I chose the batch file route instead). A big part of it is because I will be leaving the group I'm in soon; I took a position as an IIS administrator (the Systems guys are a little sad ). I wanted to make sure I wrote something that everyone else can easily modify, with as much room for growth as possible (which is why I'm doing the switches.txt file instead of embedding the commands directly in the batch script). Current format of the switches file:
    Code:
    CAHOST="OURHOST"
    SOURCE="OUR SOURCE AND PATH"
    USERNAME="OURUSERNAME"
    HOLD
    And so forth, and I need it to essentially become:
    Code:
    :: ca_restore is the command line arcserve restore creation application
    ca_restore -cahost "OURHOST" -source "OUR SOURCE AND PATH" -username "OURUSERNAME" -hold
    Quote Originally Posted by Spookster View Post
    How about

    Code:
    @ECHO OFF
    SetLocal EnableDelayedExpansion
    FOR /F "tokens=1,2 delims=," %%i IN (switches.txt) DO (
       SET SWITCHES=!SWITCHES! %%i %%j
    )
    ECHO %SWITCHES%
    PAUSE
    EXIT
    Oh yeah, delayed expansions! I should have noted that my delimiter is actually an = sign, but that isn't really a huge deal. I don't remember the last time I used one of those O.o
    I guess it wouldn't really matter either if the second parameter is empty. All you end up with is an empty space in worst case scenario which (I assume it will be anyway) be ignored by the the arcserve command app. I have an all day conference at work today, but I'll drop by after and try this out to see how it goes - I'll let you know, thanks spooks!

    Edit:
    Thanks a lot spooks, worked like a charm!
    Last edited by Fou-Lu; 09-16-2010 at 12:38 AM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •