Simple batch file fails on Windows Server 2012

0

On a Windows 2012 Server, from a logged-in Administrator (local and domain), created a batch file "test.bat" by redirecting console input into a file:

echo Hello

When run from PowerShell cmd prompt as .\test.bat, it echoes:

'■e' is not recognized as an internal or external command,

Batch file is in logged-in user's C:\users\%username% directory.

The file was created from console, thus:

echo "echo Hello" > test.bat

and then edited with Notepad. Looked alright in Notepad... nothing unusual.

Any ideas as to what's going on?

EDITED:

Per suggestion, I modified command line input to remove quotes. Therefore, entered the command like this: echo echo Yowzer > test.bat Ran as: .\test.bat and got the same response as above. Also note that if I edit in Notepad the above test.bat (i.e. created at cmd line) and re-run, it still returns a garbage response. However, if I create/save the file in Notepad from the start, it works correctly.

Can anyone duplicate this?

user340089

Posted 2014-06-30T22:53:49.143

Reputation: 11

"C:\users%username%"?? not in subdir like documents? did you cd into the folder that contains the bat file and then run ".\test bat"? And you are executing this from a poweshell console right? – Logman – 2014-06-30T23:47:04.620

The bat file was both created and run from C:\subdir. Two methods were used to create the bat file: (1) echo stuff > bat.bat from keyboard, and (2) write in Notepad++ and save as bat file. All cmd prompt activity was done from a PoSH console. – user340089 – 2014-07-03T16:48:09.383

-1 for abandoning the question, thus decreasing its value – barlop – 2015-11-08T02:56:04.337

Answers

1

Had the same problem, thought I was crazy.

Make your batch file on another computer. I had the same issue opening Notepad and typing the commands normally. It always gave me the same message. Once I recreated the batch file (do not copy the old file) on my local Win7 device, copied it back to my Win 2012R2 server, worked like a champ.

Mark Creamer

Posted 2014-06-30T22:53:49.143

Reputation: 11

I have no idea why, but this worked for me in a similar situation. – boot13 – 2018-09-02T19:15:22.250

1

It looks like the redirect > has encoded your batch file in unicode, when it should be ascii. In Powershell, use Out-File instead of the redirect like this:

"echo test" | Out-File test.bat -Encoding ascii

Running the batch file after the following encoding will reproduce the error:

"echo test" | Out-File test.bat -Encoding unicode

xXhRQ8sD2L7Z

Posted 2014-06-30T22:53:49.143

Reputation: 238

0

You most likely have some funny character somewhere. I'm pretty sure that if you do xxd -p blah.bat then you will see some funny characters in your batch file.

You could then think about how they got there, and removing them.. And that can be done with xxd and sed. Or perhaps another way, if you know what characters they are.

You can get xxd from downloading VIM 7.X, or from cygwin

Or you could find a hex editor and put the batch file in the hex editor.

You say you are creating the batch file from stdin.. Well, you could elaborate on precisely what you mean by that e.g. do you mean by copy con blah.bat then CTRL-Z? But whatever way you mean, it is putting some funny chars in the file.

added

I see you state that you did it with echo and redirect..

You can easily simplify this for some troubleshooting

Try type blah.bat

if that shows a funny character then try echo H>blah.bat then build it up to what you typed.. and see at what point you get the funny character.

Another thing to experiment with is input and output encoding

try chcp. chcp 850 and chcp 65001 and see if you see any differences. chcp will change both input and output encoding, but chcp will only show you the input encoding.

If you look at my answer here https://stackoverflow.com/questions/30904504/font-is-right-why-cant-i-get-this-unicode-character-to-display-in-this-c-sharp/32418703#32418703 you see a program called chcpa that I wrote a while back, and you can use that to change input and output encodings more flexibly than chcp can. Or you could just use chcp but if you find chcp is limiting you in your troubleshooting then you could try chcpa.

But one of your best tools here in your troubleshooting would be a hex editor or xxd as mentioned. And trying to simplify your line even further, as suggested.

Steps like that will help you isolate where the error is. And comment when you try anything here or further.

barlop

Posted 2014-06-30T22:53:49.143

Reputation: 18 677

-1

Echo doesnt use quotation marks. Take out the double quotes and it will work.

echo echo hello > test.bat

Echo looks for the string after echo and the space, the quotes break the string.

More on echo.

Keltari

Posted 2014-06-30T22:53:49.143

Reputation: 57 019

That did not fix the problem. Inputted: echo echo Yowzer > test.bat Ran as: .\test.bat and still got the same response as above. Note that if I edit the file in Notepad, it still returns a garbage response. However, if I create/save the file in Notepad from the start, all is well. Can anyone duplicate this? – user340089 – 2014-07-02T21:57:51.333

strange. it worked for me. – Keltari – 2014-07-03T03:30:51.497

Were you able to replicate the problem I was having? That is, did you see the weird result reported above when using quote marks? – user340089 – 2014-07-03T16:44:00.993

yes. I removed the quotes and it worked as intended – Keltari – 2014-07-03T18:53:13.787

-1 He said "were you able to replicate the problem I was having" And the problem he was having was was those funny characters. And you replied saying "yes...". And it's clear from his question and even more clear from his reply, that what you speak of "the quotes break the string", is absolutely and obviously not the problem – barlop – 2015-09-05T23:07:30.877