1. You are viewing our forum as a guest. For full access please Register. WindowsBBS.com is completely free, paid for by advertisers and donations.

MS-DOS Batch Files

Discussion in 'Other PC Software' started by alanrf, 2008/12/26.

  1. 2008/12/26
    alanrf

    alanrf Well-Known Member Thread Starter

    Joined:
    2004/03/05
    Messages:
    282
    Likes Received:
    8
    Apologies if this is not the appropriate forum for this topic.

    I'm trying to modify an old, but very useful, Batch file.

    The file currently uses several parameters, labelled 1%, 2%, ... passed as the file is opened, for example: batchfilename [%1] [%2] ...

    Now, I want to introduce an extra parameter that can, optionally, be set by the invocation of the file, or, if left blank, be automatically changed to a default value. Assuming, for the sake of simplicity, that I am attempting to pass a drive letter, and label the parameter as %4 for convenience, the process might look like this:

    batchfilename [%4]

    If I enter appropriate content, then the information is passed and the batch file executes as expected. If I omit the parameter, no action is taken and the execution fails.

    Currently, to catch the error I am using if "%4 "==" " goto errormsg which works and posts a warning.

    However I would like to refine it so that the check becomes, logically, something like

    if "%4 "==" " then "%4 "== "K "

    Obviously the then tag is not available but I have been trying various combinations of set without success.

    Is it possible to emulate an if ... then condition in this way or am I expecting too much?
     
  2. 2008/12/26
    TonyT

    TonyT SuperGeek Staff

    Joined:
    2002/01/18
    Messages:
    9,068
    Likes Received:
    396

  3. to hide this advert.

  4. 2008/12/26
    Bilb0

    Bilb0 Inactive

    Joined:
    2007/07/23
    Messages:
    203
    Likes Received:
    6
    if "%4 "==" " goto
    Instead of the error mesage place the desired true steps at the destination.

    Then after that step (assuming false in the previous step) use a goto to skip the goto for the "True" option.
     
  5. 2008/12/27
    wildfire

    wildfire Getting Old

    Joined:
    2008/04/21
    Messages:
    4,649
    Likes Received:
    124
    alanrf

    I think I see the problem and it does depend a little on your OS.

    If you are meaning genuine DOS batch files I don't think you have an option other than use temp batch files...

    A very simple (untested) example below.
    Code:
    IF  "%1 "==" " GOTO SetDefault
    ECHO SET DRIVE=%1 > TEMP.BAT
    GOTO CONT
    :SETDEFAULT
    ECHO SET DRIVE=K > TEMP.BAT
    :CONT
    CALL TEMP.BAT
    
    If the above is used without a parameter then %DRIVE% should be "K" otherwise the first parameter will be in %DRIVE%. Other tricks may be available for later OS's but the above should work with any MS OS from Dos 3.3 to Windows Vista
     
    alanrf likes this.
  6. 2008/12/29
    alanrf

    alanrf Well-Known Member Thread Starter

    Joined:
    2004/03/05
    Messages:
    282
    Likes Received:
    8
    Thanks for the input everybody.

    The last post comes near to what I am trying to achieve, so I'll carry on experimenting.
     
  7. 2008/12/29
    wildfire

    wildfire Getting Old

    Joined:
    2008/04/21
    Messages:
    4,649
    Likes Received:
    124
    Alan,

    If you're happy to post the existing batch I'll amend it for you. However I completly understand if you wish to give it a go yourself.

    Edit: Actually this should work, put the following at the start of your batch file

    Code:
    @ECHO OFF
    REM Make sure there is at least 3 parameters
    IF  "%3" == " " GOTO DISPUSE
    REM Now check for 4th parameter
    IF NOT  "%4 "==" " GOTO NORMAL
    REM No 4th parameter so call again with a default value
    %0 %1 %2 %3 [B]whatever you want for %4[/B]
    GOTO END
    REM Normal batch process continues
    :NORMAL
    
    Change the bold for whatever you want and add this as the end of your bat.

    Code:
    :DISPUSE
    REM Display batch file useage
    ECHO %0 [B]1 2 3 [4][/B]
    :END
    
    Again changing to bold part to suit and more ECHO messages if required before the :END label.

    BTW %0 is the name of your batch file so even if it's renamed the batch will still recall itself and DISPUSE will still show the correct name.

    The above is not ideal but it should work and is generic enough so no changes other than the above should be required in the batch.

    Hope this helps

    Let me know how you get on.
     
    Last edited: 2008/12/29
  8. 2009/01/19
    alanrf

    alanrf Well-Known Member Thread Starter

    Joined:
    2004/03/05
    Messages:
    282
    Likes Received:
    8
    Thanks for your advice, and apologies for not responding earlier. Blame two weeks in bed with the accursed 'flu!


    I think I have now resolved my issue to my satisfaction.

    Thanks again for your interest and input.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.