KlaymenDK
KlaymenDK

Reputation: 724

My batch files (.cmd) sometimes execute partial lines

I've a strange problem that I have never seen before in my ~30 years of working with batch files. Given a dummy CMD file such as this:

@echo off
:somelabel
echo Testing something
dir /b
echo All is well.
:end

This mostly runs as expected, but sometimes I get output such as 'ing something' is not recognized as an internal or external command, operable program or batch file.

That's clearly an occurrence where it has chopped off a bit of a line and attempted to execute the rest of it. When this happens, it's always a 'random' fraction of a 'random' line; it's not always line X, or losing Y characters, or occurring where I have a particular character combination. Nor does it affect only echo statements, it could just as well try to execute abel or ir /b.

My system is a fully updated Win2008 R2, running in VirtualBox 5.0.2, running in a fully upgraded Linux Mint, running on a Lenovo ThinkPad. The scripts are all UTF-8 encoded.

... what's going on? How can I avoid this?

Upvotes: 5

Views: 2003

Answers (4)

e_l_m_t_7
e_l_m_t_7

Reputation: 81

Recently I run into the same problem, although the cause might be different. In my case there was an REM command with text which have special characters in other languange. After I erased the REM command, the problem was solved. For disclosure the problem did not happened in all computers the code was executed, and still dont know why will special characters cause the batch file to execute partial lines.

Upvotes: 0

Jonathan J. Pecany
Jonathan J. Pecany

Reputation: 313

I know this is a couple years late but I actually found the most accurate answer.

I actually did some testing on why it could register only partial code and got the answer. If you are editing the code and run batch while still open, it has a partial chance of only registering partial code. This is actually correct, nothing with encoding although that might be part of it except still does it to me with ANSI but with some testing, I realize that isn't it and that it is because batch cannot catch up with you when editing the code.

I did type a comment on the answer marked as best and said what I think but after further testing on the question I realized the pattern is when editing the code and running the batch file while still open, almost right away it seems to only read partial code.

When you said it seems to work when changing encoding to ANSI, did you just run it or did you edit it and run it at the same time. The main reason I did want to answer this is because my encoding method on a batch file I am working on is in ANSI and still causes the error of partially reading the code. Once I read Aacini answer, it gave me a thought of it and that he is partially correct to KlaymenDK situation.

Do prevent the error of partially reading the code modify and wait a bit than run and if that doesn't work than restart it. If you are planning on publicly posting it, you don't really have to worry about the problem because it would most likely only occur if you edit the code while running it. Nothing that could really be a problem with your code as I know.

Upvotes: 1

KlaymenDK
KlaymenDK

Reputation: 724

Credit goes to @SomethingDark for the comment:

The command line doesn't play nice with UTF-8. Use ANSI instead.

This seems to have resolved the issue (as far as I can be sure with an intermittent problem).

Upvotes: 1

Aacini
Aacini

Reputation: 67216

Well, in my experience there is no way that a normal Batch file may present this behavior. The only way this can happen is if the Batch file is modified while it is running, so the cmd.exe processor continue reading the "next line" of the Batch file, but at that position in the modified file there is a part of another line. The example below show this behavior:

@echo off
:somelabel
(for /F "skip=1 delims=" %%a in (%~NX0) do echo %%a) > temp.tmp
del "%~NX0" & ren temp.tmp "%~NX0"
echo   Testing something
echo All is well.
:end

In this example after the del ... & ren ... line is executed, the next line to execute will be read at the beginning of original position of echo Testing something line, but now at that point there is its ing something part because the first @echo off line was deleted. See this:

@echo offRL
echo   Testing something

In previous scheme the RL letters exemplify the CR+LF control characters, so the next position is at the "ing".

Upvotes: 1

Related Questions