Cynosure.X International LLC

:

Add Comment | Related Links | TrackBack
Related Content

Pop Quiz: The syntax of the command is incorrect.

Had a hair pulling ten minutes just now. A simple batch script code fails to run. And I had a difficult time figuring it out. The following is a much simplified brain teaser for you. Can you figure out what's wrong?

@echo off

set f=%SystemDrive%\test\file.txt"

echo 1
if exist "%f%" (
echo 2
copy "%f%" "%SystemDrive%\file.txt"
)
echo 3

The code above produces the following output:

1
The syntax of the command is incorrect.

Why? What's wrong with the if statement? Let see if you know the answer. I'll post the lesson learned in a few days.

Mel
Thu, 07 Nov 2013 02:52:56 +0300

The error actually occurred before the if statement, which was why I was pulling my hair out. In the set variable statement, before the echo statement, there was an extra double-quote at the end of the statement. That caused the failure in the if statement.

Lesson learned in this case is that DOS/Windows batch interpreter is quite rudimentary in its error messages. So, just looking at the line that produces the error message may not be sufficient. You have to look at surrounding statements. In more complex batch scripts, digging deep into the variable value could be a speedier way of debugging.

Mel
Fri, 22 Nov 2013 03:07:16 +0300

Add Comment | Related Links | TrackBack
Related Content

Did your message disappear? Read the Forums FAQ.

Add Comment

Spam Control | * indicates required field
Your Name: *
E-mail:
Remember Me!
Comment: *
File attachment is optional. Please do not attach a file to your submission unless it is relevent.
Attach File:
(20 MB Max)
Spam Protection: * Answer of 9 + 0?
Click button only once, please!

TrackBack

TrackBack only accepted from WebSite-X Suite web sites. Do not submit TrackBacks from other sites.

Send Ping | TrackBack URL | Spam Control

No TrackBacks yet. TrackBack can be used to link this thread to your weblog, or link your weblog to this thread. In addition, TrackBack can be used as a form of remote commenting. Rather than posting the comment directly on this thread, you can posts it on your own weblog. Then have your weblog sends a TrackBack ping to the TrackBack URL, so that your post would show up here.

Messages, files, and images copyright by respective owners.

Products | Services
Forums | Latest | RSS
Library | Search | Wiki
Help | Licenses

Login | Register

125 Users Online

Hacking Digital Cameras
Fun for Photographers



Amazon Associate

Copyright © 1996 - 2024. All Rights Reserved.