9

on Windows 7 in VS2012, I have setup an External Tool to call a bat file (let’s call it BatA.bat). In the external tool, I am passing in $(ItemDir) as the one and only Argument and I have the Initial Directory to $(ItemDir) as well.

Within BatA.bat, a call is made to run BatB.bat like this:

call "%1BatB.bat"

This command calling BatB.bat results in the following in the output window:

'rem' is not recognized as an internal or external command, operable program or batch file.

hello

BatB.bat has the following code:

rem ***Bat B***
@echo off
echo.
echo hello

The odd thing is that in spite of the error message, BatB.bat does run and executes perfectly!

I just can’t figure out why I’m getting the error message on the call to BatB.bat in the first place...

BatA.bat and BatB.bat are both in the same directory. I’ve tried hard coding the path in the call to BatB.bat as well as doing a cd to the bat directory before calling BatB.bat but I still receive the same error.

Scott Software
  • 499
  • 2
  • 4
  • 17
  • Dude... so I created a text file within visual studio (BatB.txt) and then renamed it in the solution explorer to BatB.bat. The fact that it was a txt file first before becoming a bat file is causing the error detailed above (I'm able to replicate this behavior). If I create the bat file and name it BatB.bat initially - bingo bango - problem solved. So to sum up... if a .txt file is converted to a .bat file, the first line of the bat file will fail but all subsequent lines will execute. – Scott Software Mar 04 '14 at 18:34
  • 2
    It's because it's saving a byte order marker (BOM) to identify the encoding. Use the File menu's Advanced Save Options while editing the file, and choose 'Western European (Windows) - Codepage 1252' and then save. (That's the default codepage VS gives me when opening an existing batch file on Win7.) – Ken White Mar 04 '14 at 18:42
  • duplicates: other duplicates: [Batch file not running due to weird symbols at the start of the line](https://superuser.com/q/1084067/241386), [Weird characters (´╗┐) at the start of a batch file](https://superuser.com/q/769601/241386), [How do I resolve '@echo' is not a recognized command](http://stackoverflow.com/q/4337445/995714), [Not recognized command everytime in the first line of a code](http://stackoverflow.com/q/28836113/995714)... – phuclv Mar 25 '17 at 04:21
  • Possible duplicate of [Visual Studio inserts invalid characters in batch files](http://stackoverflow.com/questions/854360/visual-studio-inserts-invalid-characters-in-batch-files) – phuclv Mar 25 '17 at 04:21

1 Answers1

22

Easiest way to fix this for me was to open the file in notepad++ and under the Encoding tab, make sure to use an encoding that has no BOM at the end.

It is the BOM that causes 'rem' is not recognized as an internal or external command.......

ebo
  • 2,717
  • 1
  • 27
  • 22
Steven
  • 221
  • 2
  • 5
  • 2
    I've encountered this as well. Visual Studio 2012 likes to encode all code files in UTF-8, so if you need to edit bat files that are part of a Visual Studio solution, you might want to use the "Open Folder in File Explorer" command provided by the power tools extension and use Notepad++ for all batch file work. –  Oct 06 '14 at 20:03