• Odd batch file error (Vista & XP)

    Home » Forums » Developers, developers, developers » DevOps Lounge » Odd batch file error (Vista & XP)

    Author
    Topic
    #484607

    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 ShellClassicShell.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:UsersMeDesktop>■@
    ‘■@’ is not recognized as an internal or external command,
    operable program or batch file.

    What gives?[/FONT]

    Viewing 13 reply threads
    Author
    Replies
    • #1343163

      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

    • #1343164

      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.

    • #1343211

      In a BATch file, any path which includes one or more blanks must be enclosed in double quotes.
      So
      @if exist %programfiles%Classic ShellClassicShell.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

      Plethora means a lot to me.

    • #1343344

      This isn’t really the answer to my problem. Consider a one-line CMD batch file:

      echo %userprofile% [I]or any other sysvar[/I]

      This returns:

      C:UsersMeDesktop>■e[/FONT]
      ‘■e’ is not recognized as an internal or external command,[/FONT]
      operable program or batch file.

      [/FONT]

      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.[/FONT][/FONT]
      • #1343347

        My real question is how do I test for the %userprofile% and then be able to CD to that test result?

    • #1343365

      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:UsersMe
      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:UsersMe” goto

      When I do echo %userprofile% I get
      D:BAT> echo %userprofile%
      C:UsersBATcher

      D:BAT>

      BATcher

      Plethora means a lot to me.

    • #1343425

      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>■@[/FONT]
      ‘■@’ is not recognized as an internal or external command,[/FONT]
      operable program or batch file.[/FONT]

    • #1343433

      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%”

      BATcher

      Plethora means a lot to me.

    • #1343434

      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

      • #1343442

        Problem solved. This works (Win XP):

        set “path1=C:Documents and Settings”
        set path2=%username%
        set “path3=My DocumentsDownloads”
        set path4=%path1%%path2%%path3%
        echo %path4%
        pause

    • #1343436

      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 SettingsMy DocumentsDownloads>■e
      ‘■e’ is not recognized as an internal or external command,
      operable program or batch file.

      • #1346953

        I’ve viewed my test file in Notepad++ and engaged Show All Chars, but there is no spurious character added by Notepad.[/quote]
        It’s not that simple. The Show All Chars in Notepad++ will not show you the unicode byte order marker characters that are probably at the start of the file. file. Instead, look at the file encoding – Notepad++ shows that in the right side of the status bar at the bottom of the window. You can change the encoding of the file in Notepad++ to ANSI (cant’t recall which menu that is in) and then save the file and it should get rid of the unicode markers.

        • #1359779

          Hey guy’s,
          Just found this thread. Wanted to share what happened to me. I have to restart a service every 30 mins do to some crappy MS programing. I wrote a batch file on one computer(2008) that worked, net service start and stop nothing compicated. I then right click copied and pasted to another machine inside the RDP manager(the whole file not the text). During the copy process some sort of encoding happened.

          Upon calling the batch from the command promtp on the second machine like:
          c:Usersrexdocumentsnds.bat

          the following is returned

          C:UsersrexDocuments>
          ‘■’ is not recognized as an internal or external command,
          operable program or batch file.

          During the copy from one machine to another over RDP, A single charater was placed in front of the text in the batch file. I can replicate this. I open a new blank notepad file on the second machine and copy/paste in the text from the “copied” file and save it. It will then work! Pulled all my freaking hair out trying to figure out what I was doing wrong…then it hit me…..I’m using windows….it’s not me. 🙂 Moral to the story, using the command “type” at the prompt will display the file and all chars in it.

          c:type nds.bat

          ■* p o w e r s h e l l r e s t a r t – s e r v i c e – d i s p l a y n a m e ” N o t i f i c a t i o n
          D e l i v e r y S e r v i c e ”
          i i s r e s e t
          p o w e r s h e l l s t a r t – s l e e p – s 5
          p o w e r s h e l l s t a r t – s e r v i c e – d i s p l a y n a m e ” N o t i f i c a t i o n D e l
          i v e r y S e r v i c e ”

          See the crap at the begining…….Good luck all!

          Chris

    • #1343533

      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

      Plethora means a lot to me.

    • #1343623

      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).

    • #1345344

      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.

    • #1345362

      Perhaps the problem is that CMD.EXE did not like the missing label “Folder_OK”.

      Alternatively that label could have existed in part of the script which was not posted,
      and it is quite possible there was a problem with a script error we could not see.

      • #1345364

        If a label is missing, then the command processor issues an error message when the corresponding GOTO attempts to execute.

        BATcher

        Plethora means a lot to me.

    • #1346932

      I think Cliff.H identified the cause. Your editor is probably creating the batch files as Unicode. Here is a way to test. Create a small batch file with “@echo hi”. Using EditPad Pro, this file is normally 10 characters long including the CRLF. If I save it as unicode-8, it’s 13 characters long.

    Viewing 13 reply threads
    Reply To: Odd batch file error (Vista & XP)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: