Results 1 to 4 of 4
  1. #1
    Star Lounger GiddyUpGo's Avatar
    Join Date
    Nov 2009
    Location
    Clarksville, Arkansas USA
    Posts
    99
    Thanks
    44
    Thanked 4 Times in 3 Posts

    Batch file Switches...at the first or at the last of the Batch file Command

    Which is the proper placement of the switches in a .bat file? Should the switches be placed at the first or last of the command?

    Example: xcopy /e /h /k /v /y "C:\soure directory" "C:\designated directory"
    or
    xcopy "C:\source directory" "C:\designated directory" /e /h /k /v /y

    I have written a .bat file that takes care of several cleanup computer commands. It seems to work either way...but I would like to know the proper placement of the switches. In searching the internet, I see the command switches placed both ways.

    The xcopy here is used only as an example...Would like to know about the other batch commands that can use switches also.

    What I would like to know, does it make any difference?

    Are there still any old dos batch file programers around?
    Thank you for reading this. I hope someone that knows about the old dos .bat commands on switches can comment on this.

    GiddyUpGo
    Last edited by GiddyUpGo; 2011-05-10 at 22:02. Reason: Make Readable, no comments from anyone.

  2. #2
    WS Lounge VIP
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    8,168
    Thanks
    47
    Thanked 978 Times in 908 Posts
    We lurv batch files (BATcher and me) and are happy to help.

    Switches are generally happy anywhere on the command line. I try and put them in a logical sequence to make the command readable, but anything that works is good.
    Using environment variables to hold source and destination files/directories makes it easy to re-use the command.
    "setlocal" allows you to use lots of environment variables without cluttering up the master environment.
    I like to put "echo" in front of commands during testing.

    cheers, Paul

  3. #3
    Super Moderator BATcher's Avatar
    Join Date
    Feb 2008
    Location
    A cultural area in SW England
    Posts
    3,413
    Thanks
    33
    Thanked 195 Times in 175 Posts
    It all depends on whether or not the command requires positional parameters to precede the switches.

    As Paul says, most commands do not have this requirement usually because all switches are preceded by a slash / or a hyphen -, and so can be distinguished from other parameters.
    For example, the following are all equivalent:
    ping -n 6 127.0.0.1 > nul
    ping 127.0.0.1 -n 6 > nul
    and even
    > nul ping 127.0.0.1 -n 6

    whereas RoboCopy requires the source path and the destination path to be the first two parameters:
    ROBOCOPY source destination [file [file]...] [options]

    The usual way is to check the syntax of the command using <command> /? or otherwise from documentation, and then experiment!
    BATcher

    Time prevents everything happening all at once...

  4. #4
    Star Lounger GiddyUpGo's Avatar
    Join Date
    Nov 2009
    Location
    Clarksville, Arkansas USA
    Posts
    99
    Thanks
    44
    Thanked 4 Times in 3 Posts

    Smile Thank You! Paul T and Batcher

    Paul T:
    This has answered my question, which I could not find an answer from searching the internet.
    I have not tried the "setlocal" But will.
    Batcher:
    Your answer has me thinking about the many different ways of batch files.
    I will now modify my .bat file to make it look the best and operate better.

    I used to have to manually run my "Clean-Up Computer Programs". Now I just start my batch file and let it do the work for me.

    Thank you both.

Posting Permissions

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