Solved type command errorlevel catch

June 18, 2012 at 06:46:50
Specs: Windows XP
Hi guys,

I need to catch the error if the command:

type a.txt >> d:\b.txt

does not work (for example if b.txt is already an open file what can't be written). If it succeeds to append the file to b.txt i want to delete a.txt.
THe odd thing is when i get the %ERRORLEVEL% it gives always 0 because thats probably the errorlevel of the type command instead of the append command after the type.
What is a good solution for this problem?


See More: type command errorlevel catch

Report •

✔ Best Answer
June 19, 2012 at 07:06:05
Because anything after && only executes if the previous command succeeded. Anything after || executes only if the previous command failed.

Tony



#1
June 18, 2012 at 11:04:50
I didn't test this but it's something to try:

type a.txt >> d:\b.txt || del a.txt

Edit:

Just tried it, and I don't think it's possible. From the couple tests I did, it didn't matter whether the file is already open or being used, the type command still succeeded.

Tony


Report •

#2
June 18, 2012 at 19:41:19
variation on Tony's approach:
copy d:b.txt + a.txt && echo DEL A.TXT backup successful

(didn't know that syntax for copy would work, Lol! learn sump'n ever'day!)
try this with dry runs before committing!
another method would be to compare the size of b.txt before and after the operation
with the size of a.txt to see if they add up: if b.txt were 2000 bytes and a.txt was
100 bytes the new b.txt should be 2100 bytes. yet another method would be to use
findstr /g a.txt b.txt, which would look for every line in a.txt to be found in b.txt. If you want help coding either of these latter two, holler at me.


Report •

#3
June 19, 2012 at 00:04:07
Hi Tony, i tried this with big files (then you know for sure it takes a while) but it doesn't work. It always deletes the file, even if you get the error that the file b.txt is used by another proces (second type command to the same b.txt file).

However this works:

type a.txt >> d:\b.txt && del a.txt

any clue why?


Report •

Related Solutions

#4
June 19, 2012 at 07:06:05
✔ Best Answer
Because anything after && only executes if the previous command succeeded. Anything after || executes only if the previous command failed.

Tony


Report •

#5
June 19, 2012 at 09:33:37
Ok thnx for the solution tony.

Report •

#6
June 20, 2012 at 14:30:06
But then the && functionality is using a different method of checking, since the errorlevel is always set to zero (see opening post), but && knows when the redirection worked or not.

So, && is evaluating the whole command, and the the errorlevel result is only from the command excluding redirection ?!


Report •

#7
June 20, 2012 at 19:52:02
Lol! this is one of those threads that keeps on threading!
If in doubt, I believe that parentheses can override lower levels
(command1 | command2 >> xfiles) && echo ok || echo NOT
If I'm wrong, I hope to be corrected...

Report •

#8
June 21, 2012 at 02:59:13
Very nice generic solution, it works as well.
How can we suppress the error? Is this a nice trick:

(command1 | command2 >> xfiles) 2>null && echo ok || echo NOT


Report •

#9
June 21, 2012 at 20:42:41
No reason I can see that won't work. Piping the error to null should suppress, and that won't interfere with the AND / OR error handling in the pipestream.

Report •

Ask Question