Go Back   CodingForums.com > :: Computing & Sciences > Computer Programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-14-2010, 11:40 PM   PM User | #1
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 09-15-2010, 12:14 AM   PM User | #2
oracleguy
Rockstar Coder


 
Join Date: Jun 2002
Location: USA
Posts: 9,043
Thanks: 1
Thanked 322 Times in 318 Posts
oracleguy is a jewel in the roughoracleguy is a jewel in the roughoracleguy is a jewel in the rough
I'd be remiss if I didn't ask why oh why you are using batch files?
__________________
OracleGuy
oracleguy is offline   Reply With Quote
Old 09-15-2010, 02:14 AM   PM User | #3
Spookster
Supreme Overlord


 
Spookster's Avatar
 
Join Date: May 2002
Location: Marion, IA USA
Posts: 6,220
Thanks: 4
Thanked 80 Times in 79 Posts
Spookster will become famous soon enough
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
Who gave you that Ugging infraction? Yeah that's right it was me!
Spookster is offline   Reply With Quote
Users who have thanked Spookster for this post:
Fou-Lu (09-15-2010)
Old 09-15-2010, 02:09 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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!
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php

Last edited by Fou-Lu; 09-15-2010 at 11:38 PM..
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:15 AM.


Advertisement
Log in to turn off these ads.