Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    New Lounger
    Join Date
    Jan 2009
    Location
    Auburn, California, USA
    Posts
    24
    Thanks
    0
    Thanked 1 Time in 1 Post

    Unhappy Odd batch file error (Vista & XP)

    I'm having a problem with a CMD-style batch file on both Vista and XP machines where the SysVars check out fine (using the SET command at a prompt). PAUSE isn't helping at all, either, in that the CMD window briefly appears, then quickly disappears, when double-clicking the file's icon. Placing the file on my Desktop during testing, I'm having to open a CMD window, change to the Desktop directory and run it directly from the prompt to even see what's going on. Since the referenced ClassicShell.chm file does not exist, I'm expecting to see the echo message.

    File contents:

    @if exist %programfiles%\Classic Shell\ClassicShell.chm goto Folder_OK
    echo CS path different. Email this error message.
    pause
    exit


    Regardless of what I do, this is what is returned:

    C:\Users\Me\Desktop>■@
    '■@' is not recognized as an internal or external command,
    operable program or batch file.

    What gives?

  2. #2
    3 Star Lounger jockmullin's Avatar
    Join Date
    Dec 2009
    Location
    St-Eustache,QC,Canada
    Posts
    239
    Thanks
    10
    Thanked 21 Times in 20 Posts
    It looks like you may have an invalid character in the file before the @-sign.
    It looks like a hex FE.

    Try opening the file in a hex editor.

    Jock

  3. #3
    New Lounger
    Join Date
    Jan 2009
    Location
    Auburn, California, USA
    Posts
    24
    Thanks
    0
    Thanked 1 Time in 1 Post
    Nope. I've created the file from scratch in Notepad several times on both machines. The spurious character is only introduced as the command interpreter attempts to run the file.

    The crazy thing is that this DOES work:

    @echo off
    c:
    cd %programfiles%\Classic Shell
    if not exist ClassicShell.chm echo It doesn't exist.
    Last edited by CaptNemo; 2012-07-30 at 21:58. Reason: Added more details

  4. #4
    Super Moderator BATcher's Avatar
    Join Date
    Feb 2008
    Location
    A cultural area in SW England
    Posts
    3,421
    Thanks
    33
    Thanked 195 Times in 175 Posts
    In a BATch file, any path which includes one or more blanks must be enclosed in double quotes.
    So
    @if exist "%programfiles%\Classic Shell\ClassicShell.chm" goto Folder_OK
    stands a better chance of working.

    The CD command shown in the post above is a relaxation of this requirement which shouldn't really work but does.
    Nobody ever said that COMMAND.COM / CMD.EXE were consistent!
    BATcher

    Time prevents everything happening all at once...

  5. #5
    New Lounger
    Join Date
    Jan 2009
    Location
    Auburn, California, USA
    Posts
    24
    Thanks
    0
    Thanked 1 Time in 1 Post
    This isn't really the answer to my problem. Consider a one-line CMD batch file:

    echo %userprofile% [or any other sysvar]

    This returns:

    C:\Users\Me\Desktop>■e
    '■e' is not recognized as an internal or external command,
    operable program or batch file.

    Now today I'm on yet a third machine and have again created this file from scratch. I'm not copying and pasting. There is no spurious character prefacing the 'e' in echo.

  6. #6
    New Lounger
    Join Date
    Jan 2009
    Location
    Auburn, California, USA
    Posts
    24
    Thanks
    0
    Thanked 1 Time in 1 Post
    My real question is how do I test for the %userprofile% and then be able to CD to that test result?

  7. #7
    Super Moderator BATcher's Avatar
    Join Date
    Feb 2008
    Location
    A cultural area in SW England
    Posts
    3,421
    Thanks
    33
    Thanked 195 Times in 175 Posts
    What do you get when you run SET in a Command Prompt window or in a BATch file? Are there any corrupt characters produced?

    What you are getting is a pure error (once you've corrected the missing double-prompts), since your User Profile variable should contain just
    C:\Users\Me
    Where the Desktop> part comes from, I couldn't guess.

    To test the User Profile environment variable, you simply do something like
    if /i "%userprofile%"=="C:\Users\Me" goto <label>

    When I do echo %userprofile% I get
    D:\BAT> echo %userprofile%
    C:\Users\BATcher

    D:\BAT>
    BATcher

    Time prevents everything happening all at once...

  8. #8
    New Lounger
    Join Date
    Jan 2009
    Location
    Auburn, California, USA
    Posts
    24
    Thanks
    0
    Thanked 1 Time in 1 Post
    Again, I've been testing this on three machines, created the CMD from scratch each time and also testing many versions in an attempt to find something that works. In answer to your question, then, all of the sysvars are kosher; there are no spurious characters introduced, especially in relation to %userprofile%.

    Please note that I'm writing this for another, non-techie user located in another state. I have no idea what they've chosen as their username, hence your "if /i" test doesn't help me at all (I do not know the right side of the string comparison).

    When I echo %userprofile& from a CMD prompt I, too, get the expected result. This also confirms that there is nothing amiss with this sysvar.

    I've tried using many different things on the first line of the batch file, including a basic @echo off. It seems that so long as there is a referenced %userprofile% somewhere in the file, it returns the same error, substituting the first character for @ shown below:

    prompt>■@
    '■@' is not recognized as an internal or external command,
    operable program or batch file.

  9. #9
    Super Moderator BATcher's Avatar
    Join Date
    Feb 2008
    Location
    A cultural area in SW England
    Posts
    3,421
    Thanks
    33
    Thanked 195 Times in 175 Posts
    My only suggestion is that the user name and hence the %userprofile% variable contains somehow an invalid character...

    My second only suggestion is to test "C:\Users\%username%" which should give the same result as "%userprofile%"
    Last edited by BATcher; 2012-08-01 at 15:30.
    BATcher

    Time prevents everything happening all at once...

  10. #10
    WS Lounge VIP
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    8,199
    Thanks
    48
    Thanked 986 Times in 916 Posts
    I have seen that very problem. Windows / Notepad adds a non-printable character at the front of the file and you can't see it in Notepad.
    There are 2 solutions.
    1. echo @if exist>test.cmd
    Now edit test .cmd in Notepad.
    2. Use Notepad++ and turn on view all characters. This is the best solution - of course.

    cheers, Paul

  11. #11
    New Lounger
    Join Date
    Jan 2009
    Location
    Auburn, California, USA
    Posts
    24
    Thanks
    0
    Thanked 1 Time in 1 Post
    I wish it were that simple.

    Paul, Notepad did not add any characters. I've viewed my test file in Notepad++ and engaged Show All Chars, but there is no spurious character added by Notepad.

    And BATcher, this three-line batch file, when engaged from a prompt, yields the same previous result. (Adding quotes on either side of the sysvar matters not.)

    echo %username%
    pause
    exit

    ~~~~~~~~~~~

    C:\Documents and Settings\<me>\My Documents\Downloads>■e
    '■e' is not recognized as an internal or external command,
    operable program or batch file.

  12. #12
    New Lounger
    Join Date
    Jan 2009
    Location
    Auburn, California, USA
    Posts
    24
    Thanks
    0
    Thanked 1 Time in 1 Post
    Problem solved. This works (Win XP):

    set "path1=C:\Documents and Settings\"
    set path2=%username%
    set "path3=\My Documents\Downloads"
    set path4=%path1%%path2%%path3%
    echo %path4%
    pause

  13. #13
    Super Moderator BATcher's Avatar
    Join Date
    Feb 2008
    Location
    A cultural area in SW England
    Posts
    3,421
    Thanks
    33
    Thanked 195 Times in 175 Posts
    I'm pleased you've solved it, but I've never come across this problem in years and years of programming BATch files, quite often with NOTEPAD.

    The old PC-DOS 'E' editor (16-bit) always used to put a (DOS) end-of-file character 0x1A at the end of a file, following the CR/LF pair, but that usually didn't cause any problems.
    BATcher

    Time prevents everything happening all at once...

  14. #14
    New Lounger
    Join Date
    Jan 2009
    Location
    Auburn, California, USA
    Posts
    24
    Thanks
    0
    Thanked 1 Time in 1 Post
    I couldn't agree more. Twenty years ago I was a Zen master of batch file programming (and was even an uncredited technical editor of the MS-DOS 6.2 User Guide). I've never seen anything like it, either. It makes no logical sense and my testing revealed it had nothing to do with using Notepad as the editor, nor corrupt sysvars (as witnessed by the change in coding approach—written in Notepad again—working as expected).

  15. #15
    Lounger
    Join Date
    Dec 2009
    Location
    Gillingham, Dorset, UK
    Posts
    30
    Thanks
    0
    Thanked 13 Times in 11 Posts
    I believe that the problem occurred because the file was initially created on the Vista PC.
    Notepad on Vista/Win7 default to the Unicode format.
    When creating batch files on Vista/Win7, select ANSI from the Encoding drop-down box.

Page 1 of 2 12 LastLast

Posting Permissions

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