[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 880
  • Last Modified:

Batch file Changing lines while executing.

I have a batch file, test.bat, and this is the contents of it:
rem @echo off
ren Mirror.*.Bat "Call Mirror.*.bat"
************************************************
dir /b Mirror.*.bat > tb.bat <<<----  LOOK AT THIS LINE...
************************************************
ren "Call Mirror.*.Bat" Mirror.*.bat
type tb.bat
del tb.bat

Open in new window

When I run it, I get the following weird result...
C:\SARROOT>test.bat

C:\SARROOT>rem @echo off

C:\SARROOT>ren Mirror.*.Bat "Call Mirror.*.bat"
************************************************
C:\SARROOT>dir /b Mirror.*.bat  1>tb.bat <<<--- THIS IS WHAT IT CHANGES INTO
File Not Found
************************************************

C:\SARROOT>ren "Call Mirror.*.Bat" Mirror.*.bat

C:\SARROOT>type tb.bat

C:\SARROOT>del tb.bat

Open in new window

I have NO idea whats happening.  It is changing ".bat > tb" to ".bat  1>tb"  The original text is from using type test.bat, to make sure dos picked it up the way I wrote it, but notepad also reads it correctly.
0
hpdvs2
Asked:
hpdvs2
  • 3
  • 2
  • 2
  • +1
3 Solutions
 
pony10usCommented:
I have seen this before in some of my code.  What I have found works most of the time is to open it in Notepad. Move the cursor to between the "t" and "b" and backspace until you get to the "t" at the end of the word "bat". Then hit the space bar once and the letter "t". Save the file and try to run it again.

There seems to be an "unprintable" character
0
 
Steve KnightIT ConsultancyCommented:
Actually that is normal.

1> means redirect normal output "STDOUT" to the file which is the same as >
2> means redirect error output "STDERR" to the file

Steve
0
 
oBdACommented:
That's perfectly normal; it's the shell parsing the line to execute. "a>b" is basically just short for "a 1>b", because only stdout is redirected. This makes no functional difference.
The "File Not Found" is an error message from "dir" and sent to stderr, which is not redirected, which is why you see it.
If you want to have both stdout and stderr redirected to the same file, you need to redirect stderr to stdout (after the redirect of stdout):
dir /b Mirror.*.bat >tb.bat 2>&1

Open in new window

0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
Steve KnightIT ConsultancyCommented:
So for instance:

dir > output.txt 2>> errors.txt

Sends the output of a successful dir command to the file output.txt and appends any errors to the errors.txt line.

You only see it because you have not got "echo off"

Steve
0
 
hpdvs2Author Commented:
I found the solution, which makes no sense.

@pony10us, I tried that.  actually opened it in notepad did the exact same steps, and also added a second line that did the same thing, but with a file that wasn't there and got the EXACT SAME problem.  

The problem still exists, where the line appears different than the batch.  Weird.

However, the solution was that the dir command was throwing an exception line/debug line, instead of std->out.  Appearantly It changes the text being written in the console when that is the case.  WEIRD.  However, it still ran the command.  

The problem ultimately was in my batch file, where I had just renamed all the Mirror.* to Call Mirror.*, then tried to get a directory listing of Mirror.* and it found nothing.

I fixed it to be: dir /b "Call Mirror.*.bat" > tb.bat.  This appears to be an unlogged Dos issue (windows 7 Ultimate, latest updates) about how the text changes, but it ran just fine, it just doesn't show correctly.

Thanks.
0
 
pony10usCommented:
Thanks Steve,

I rarely see that because I almost always have the echo off. I never really paid that close of attention to the redirect line when I am troublshooting with echo on.

Now that you mention it I agree with what you two are saying.  :)
0
 
Steve KnightIT ConsultancyCommented:
Erm the solution to your question was what we said, but it seems you had a problem with your batch file logic which we I didn't look at of course.  Glad you are sorted though.

Steve
0
 
hpdvs2Author Commented:
Sorry for the lack of clarity in my earlier comment, only the first solution made it in, while I was posting my response, but I didn't see a way to delete it, so I marked the others as the solution.
0

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now