locked
If else usage for Windows Batch RRS feed

  • Question

  • @ECHO OFF
    COLOR 3E
    SET A111="Documents and Settings"
    SET A222=Users
    SET /P PC=Enter Computer IP or HostName: 
    
    @echo Opening Remote Folder....
    if exist "" "\\%PC%\C$\"%A111%" (start "" "\\%PC%\C$\"%A111%") else (start \\%PC%\C$\%A222%)
    PAUSE

    I made a batch file to open some remote folders but there is a mistake I didn't notice.

    After entering IP; I want to open, If there is remote "Documents and Settings" folder. But If there is no "Documents and Settings", I want to see "Users" folder.

    Where is the error I make?

    Thank you in advance...


    \_(ツ)_/ twitter.com/serdaruzun

    Wednesday, June 18, 2014 8:45 AM

Answers

  • The first quoted parameter on the start command's command line is assumed to be a console window title.

    If you are not starting a console session, then use an empty double-quoted string on the start command's command line. Same as what Frederik Long posted, but with empty double-quoted strings:


    SET A111=Documents and Settings
    if exist "\\%PC%\C$\%A111%" (start "" "\\%PC%\C$\%A111%") else (start "" "\\%PC%\C$\%A222%")
    


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by Frederik Long Wednesday, June 18, 2014 10:48 PM
    • Marked as answer by Bill_Stewart Tuesday, July 15, 2014 10:00 PM
    Wednesday, June 18, 2014 2:14 PM

All replies

  • my guess is the quotes. You do not need quotes with SET:

    SET A111=Documents and Settings
    


    MCP/MCSA/MCTS/MCITP

    Wednesday, June 18, 2014 9:41 AM
  • Your double quotes are all over the place. You could see this easily by running your batch file in a Console session. Instead of writing

    SET A111="Documents and Settings"
    if exist "" "\\%PC%\C$\"%A111%" (start "" "\\%PC%\C$\"%A111%") else (start \\%PC%\C$\%A222%)
    you should write
    SET A111=Documents and Settings
    if exist "\\%PC%\C$\%A111%" (start "\\%PC%\C$\%A111%") else (start "\\%PC%\C$\%A222%")


    Wednesday, June 18, 2014 10:06 AM
  • Your double quotes are all over the place. You could see this easily by running your batch file in a Console session. Instead of writing

    SET A111="Documents and Settings"
    if exist "" "\\%PC%\C$\"%A111%" (start "" "\\%PC%\C$\"%A111%") else (start \\%PC%\C$\%A222%)
    you should write
    SET A111=Documents and Settings
    if exist "\\%PC%\C$\%A111%" (start "\\%PC%\C$\%A111%") else (start "\\%PC%\C$\%A222%")


    Your code didnt work :(

    \_(ツ)_/ twitter.com/serdaruzun

    Wednesday, June 18, 2014 2:11 PM
  • The first quoted parameter on the start command's command line is assumed to be a console window title.

    If you are not starting a console session, then use an empty double-quoted string on the start command's command line. Same as what Frederik Long posted, but with empty double-quoted strings:


    SET A111=Documents and Settings
    if exist "\\%PC%\C$\%A111%" (start "" "\\%PC%\C$\%A111%") else (start "" "\\%PC%\C$\%A222%")
    


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by Frederik Long Wednesday, June 18, 2014 10:48 PM
    • Marked as answer by Bill_Stewart Tuesday, July 15, 2014 10:00 PM
    Wednesday, June 18, 2014 2:14 PM
  • Your code didnt work :(

    I love feedback like "it did not work". It leaves so much room to the imagination. Useful it ain't . . .
    Wednesday, June 18, 2014 4:24 PM
  • I love feedback like "it did not work". It leaves so much room to the imagination. Useful it ain't . . .

    Correct. When you say that something didn't work, you have to say how it didn't work.

    From that blog post:

    When somebody suggests a troubleshooting step or a workaround, but when you try it and it doesn't work, you need to say how it didn't work. The person who made the suggestion had some expectation that it would work, and just saying that it didn't work will probably just generate an unhelpful response like "Well, try again." Which doesn't help anybody.

    -- Bill Stewart [Bill_Stewart]

    Wednesday, June 18, 2014 4:47 PM
  • Your code didnt work :(

    \_(ツ)_/ twitter.com/serdaruzun

    With Bill's addition it will:

    SET A111="Documents and Settings"
    if exist "" "\\%PC%\C$\"%A111%" (start "" "\\%PC%\C$\"%A111%") else (start \\%PC%\C$\%A222%)
    you should write
    SET A111=Documents and Settings
    if exist "\\%PC%\C$\%A111%" (start "" "\\%PC%\C$\%A111%") else (start "" "\\%PC%\C$\%A222%")

    • Proposed as answer by Frederik Long Wednesday, June 18, 2014 10:48 PM
    Wednesday, June 18, 2014 8:38 PM
  • Your code didnt work :(


    I love feedback like "it did not work". It leaves so much room to the imagination. Useful it ain't . . .
    I always prefer to be honest...

    \_(ツ)_/ twitter.com/serdaruzun

    Thursday, June 19, 2014 6:34 AM
  • I love feedback like "it did not work". It leaves so much room to the imagination. Useful it ain't . . .

    Correct. When you say that something didn't work, you have to say how it didn't work.

    From that blog post:

    When somebody suggests a troubleshooting step or a workaround, but when you try it and it doesn't work, you need to say how it didn't work. The person who made the suggestion had some expectation that it would work, and just saying that it didn't work will probably just generate an unhelpful response like "Well, try again." Which doesn't help anybody.


    -- Bill Stewart [Bill_Stewart]

    It(your code) wasnt able to open any window (users, documents and settings), it closed directly

    \_(ツ)_/ twitter.com/serdaruzun

    Thursday, June 19, 2014 6:35 AM
  • It(your code) wasnt able to open any window (users, documents and settings), it closed directly

    \_(ツ)_/ twitter.com/serdaruzun

    My revised code (which included Bill's suggestion) works perfectly on my machine.

    SET A111=Documents and Settings
    if exist "\\%PC%\C$\%A111%" (start "" "\\%PC%\C$\%A111%") else (start "" "\\%PC%\C$\%A222%")

    Thursday, June 19, 2014 6:48 AM
  • It(your code) wasnt able to open any window (users, documents and settings), it closed directly

    \_(ツ)_/ twitter.com/serdaruzun

    My revised code (which included Bill's suggestion) works perfectly on my machine.

    SET A111=Documents and Settings
    if exist "\\%PC%\C$\%A111%" (start "" "\\%PC%\C$\%A111%") else (start "" "\\%PC%\C$\%A222%")

    How can you test on your machine? I am talking about remote machines! And also, There must not be two different folder (Users Profile Folder) on your machine.

    If the machine is XP; Batch file should open Documents And Settings folder

    If the machine is Win7; Batch file should open Users folder.

    This is all I want :(. But I always failed. Thank all of you for your helps. But I have no any hope :(


    \_(ツ)_/ twitter.com/serdaruzun

    Thursday, June 19, 2014 6:56 AM
  • Since you are accessing the root shares, you need to use network admin level privileges for your batch file to run successfully.  Are you accessing the batch with the elevated privileges?  (Right click batch and select "Run as administrator" and then provide network admin credentials or run it from an elevated command prompt.)

    Tom Lavedas


    Tom Lavedas

    Thursday, June 19, 2014 12:54 PM