Trailing blanks in batch file

R

Ragnar Midtskogen

Hello,

I have a batch file where I am trying to insert the short name of the
weekday in a file path.

I create the day variable as follows:
REM Get day of week
for /f %%a in ('date /t') do set DAY=%%a

The following line shoul copy the Registry backup file to the weekday folder
on the E drive:
XCOPY %BackUpDir%\Registry_Settings\%ServerName%-Registry-Backup.bkf
E:\SQLServerBackupsQA\%DAY%\ /Y
But, this string comes out with two blanks between the three letter day and
the following backslash. The XCOPY requires the backslash.

Any ideas on how to get rid of the blanks?
I am not to good about batch files, so any help would be appreciated.

Ragnar
 
F

foxidrive

I have a batch file where I am trying to insert the short name of the
weekday in a file path.

I create the day variable as follows:
REM Get day of week
for /f %%a in ('date /t') do set DAY=%%a

Look for blank spaces at the end of the line above, in your batch file.
If there are none then post again.
 
A

Al Dunbar [MS-MVP]

foxidrive said:
Look for blank spaces at the end of the line above, in your batch file.
If there are none then post again.

Easiest way to avoid this kind of a problem is to parenthesize the command,
i.e.:

(for /f %%a in ('date /t') do set DAY=%%a)

If that is not the cause, then blanks can be stripped from the DAY variable
with:

(set day=%day: =%)

At least I think that works on w2k in addition to 2k3 and xp

/Al
 
R

Ragnar Midtskogen

Thank you Foxidrive,

Of course they are there, I was asking how to get rid of them.
In another post, Al Dunbar said to put the line "for /f %%a in ('date /t')
do set DAY=%%a" in parentheses and that fixed the problem

Ragnar
 
R

Ragnar Midtskogen

Thank you Al,

(for /f %%a in ('date /t') do set DAY=%%a) gets rid of the blanks,
Batch file programming seems a bit strange, at least for the uninitiated
:)-)).

Ragnar
 
T

Todd Vargo

Ragnar Midtskogen said:
Thank you Al,

(for /f %%a in ('date /t') do set DAY=%%a) gets rid of the blanks,
Batch file programming seems a bit strange, at least for the uninitiated
:)-)).

That is part of the learning curve.
 
P

Phil Robyn

Ragnar said:
Thank you Al,

(for /f %%a in ('date /t') do set DAY=%%a) gets rid of the blanks,
Batch file programming seems a bit strange, at least for the uninitiated
:)-)).

Ragnar

You could also do

... set "DAY=%%a"

to get rid of the trailing blank(s), which are perhaps being placed at the
end of each line of the batch file by the particular text editor that you
are using.
 
F

foxidrive

Thank you Foxidrive,

Of course they are there, I was asking how to get rid of them.

Place your cursor at the end of the line (with the end key) and press
backspace, then save.
 
A

Alexander Suhovey

Phil Robyn said:
You could also do

... set "DAY=%%a"

Yet another one:

set DAY=%%a&

This is probably ugliest one but hey, it's a single character :)
 
A

Al Dunbar [MS-MVP]

Alexander Suhovey said:
Yet another one:

set DAY=%%a&

This is probably ugliest one but hey, it's a single character :)

Hey, it's so ugly it will probably remain a single character for the rest of
its life!

This would be marginally better:

set DAY=%%a&rem :: DO NOT add whitespace between the "%%a" and the
"&"

but both are bad because the main function of "&" is to separate two
commands on a line, not terminate one, something it does incidentally.

Of these two:

(set DAY=%%a)

and:

set "DAY=%%a"

my personal favourite is the one with parenthesis. IMHO, they stand out a
little more, and consider this one: (set DAY="%%a"). Doing that with the
double-quoted method requires a bit more care.

/Al
 
A

Al Dunbar [MS-MVP]

foxidrive said:
Place your cursor at the end of the line (with the end key) and press
backspace, then save.

then load the file back into your editor to display it and visually check to
see if there are blank characters. Wait a minute, it looks exactly the same
as before I fixed it!".

/Al
 
A

Alexander Suhovey

Al Dunbar said:
Hey, it's so ugly it will probably remain a single character for the rest
of
its life!

Haha, that was a good one! Love it!

set DAY=%%a&rem :: DO NOT add whitespace between the "%%a" and the "&"

Same applies to parentheses and quotes, isnt it? :) The problem here is not
somebody putting extra trailing spaces by accident, it's some text editors
that can automatically do that for whatever reason they have. Also, you
probably know that some forum engines have this problem too: when you post a
code snipplet using
Code:
tags, every line inside gets an extra
trailing space which can very well result in broken batch code if you
copy-paste it to your text editor.

but both are bad because the main function of "&" is to separate two
commands on a line, not terminate one, something it does incidentally.

Sure, but I don't see how it's that bad other than it looks ugly. There are
other things in batch scripting that weren't meant to be used in the way
they are used by scripters today.

Like "set /p" command which purpose is to provide interactive user input
facility but somebody figured it also can be used to append the output text
to the same line.

Or ping as a way to add pauses to the script.

@echo off
set /p foo="Working ..."<nul
for /l %%i in (1,1,40) do (
ping -n 1 127.0.0.1>nul 2>&1
set /p foo="."<nul
)
echo Done.
 
A

Al Dunbar [MS-MVP]

Interesting comments, see my responses in-line...

Alexander Suhovey said:
Haha, that was a good one! Love it!

I can't seem to buy a good straight line these days, so when one comes along
for free, I go for it!
Same applies to parentheses and quotes, isnt it? :)

Yes and no. Yes, the prevention of a trailing blank isn't the primary reason
for them, but no: the parentheses and quotes at least surround the command
they are there to protect, whereas the ampersand simply follows it, a fact
that I contend makes its meaning at least somewhat less obvious. Also, the
quotes are normally understood to differentiate what is in a string from
what is not in a string, and the parenthesis' function is to group one or
more commands, thereby similarly differentiating what is in the command(s)
from what is not. If what is not happens to be a space, well that is still
simply these things doing their normal function, when this poor
(non-existent) space is given short shrift.

We could argue endlessly, or we could do a test with a hundred batch
programmers and ask them to modify this script to make it more readable, but
still yielding the same result:

set DAY=%%1&(set TIME=%%2)

I bet that the "&" would either be replaced with an endline character or
preceded and followed with a blank MORE often than would the final ")" be
preceded by a blank.

In my mind, then, it is not really which method is most aligned with the
original intent of the feature as I initially suggested, but which will
result in a more intuitive understanding of what it is doing in this
context.
The problem here is not
somebody putting extra trailing spaces by accident, it's some text editors
that can automatically do that for whatever reason they have.

That may indeed be the cause of the majority of cases, however, I see the
problem as there *being* a trailing space whose presence creates a problem.
And that problem is further exascerbated by the difficulty of knowing the
blank is there when it is followed by more whitespace in the form of an
endline character.

If someone exclusively used an editor that displayed blanks as grey dots,
then it might be less important for them to do this (unless, of course, this
is one of the editors that indiscriminately pads lines with blanks), but one
can never be sure that one's script will never be edited by someone else
with a different editor.
Also, you
probably know that some forum engines have this problem too: when you post a
code snipplet using
Code:
tags, every line inside gets an extra
trailing space which can very well result in broken batch code if you
copy-paste it to your text editor.

Which is one reason for the many posts over the years recommending the
explicit use of parentheses, or, sometimes, double-quotes.
Sure, but I don't see how it's that bad other than it looks ugly. There are
other things in batch scripting that weren't meant to be used in the way
they are used by scripters today.

I agree, like 80%! Ugliness alone may not be a bad thing; ugliness that
interferes with one's ability to detect subtle nuances about what is really
going on in a script, now *that* is a bad thing.
Like "set /p" command which purpose is to provide interactive user input
facility but somebody figured it also can be used to append the output text
to the same line.

Or ping as a way to add pauses to the script.

@echo off
set /p foo="Working ..."<nul
for /l %%i in (1,1,40) do (
ping -n 1 127.0.0.1>nul 2>&1
set /p foo="."<nul
)
echo Done.

Sure, I use those techniques, and more... That is because the side effects
in question are sometimes almost important as the obvious intent of such a
feature, and the side effect is not really available otherwise without
writing an executable to do it. But, ideally, one should never use code for
its side effects unless the meaning is documented, or at least in general
use in the batch scripting community.

/Al
 
A

Alexander Suhovey

[snip]
We could argue endlessly

Yes we could though I wouldn't call it arguing, rather an interesting
conversation. I could say that to me

(set DAY=%%1)
(set TIME=%%2)

doesn't look much better than

set DAY=%%1&
set TIME=%%2&

and intent of "&" isn't less apparent here as that of parentheses. But the
thing is, I actually agree with you. By calling it ugly in my initial post I
was trying to say that it's just another way to address the issue though
least preferable one.
 
T

Timo Salmi

Alexander Suhovey said:
Yes we could though I wouldn't call it arguing, rather an interesting
conversation. I could say that to me
(set DAY=%%1)

Back to the question posed in the subject. The solutions so far work for
a single word, but insidiously not directly beyond that. Here is a small
demo

@echo off & setlocal enableextensions
set S= This is a test
echo %S%.
(for /f "tokens=*" %%a in ('echo %S%') do set T=%%a)
echo %T%.
endlocal & goto :EOF

The output
C:\_D\BAS>cmdfaq
This is a test .
This is a test .

Well, e.g. sed can be used to get rid of the remaining trailing blank.

However, should one drop the "tokens=*" then one gets:
This.

All the best, Timo
 
T

Timo Salmi

Timo Salmi said:
Well, e.g. sed can be used to get rid of the remaining trailing blank.

Taken from
182662 Jan 13 2007 ftp://garbo.uwasa.fi/pc/link/tscmd.zip
tscmd.zip Useful NT/2000/XP script tricks and tips, T.Salmi

@echo off & setlocal enableextensions
set S= This is a test
echo %S%.
echo %S%|sed -e "s/^ *//" -e "s/ *$//" -e "s/^/@set
S=&/">%temp%\tmp$$$.cmd
for %%c in (call del) do %%c %temp%\tmp$$$.cmd
echo %S%.
endlocal & goto :EOF

The output will correctly drop both the leading AND the trailing blanks.
D:\TEST>cmdfaq
This is a test .
This is a test.

All the best, Timo
 
T

Todd Vargo

Al said:
That may indeed be the cause of the majority of cases, however, I see the
problem as there *being* a trailing space whose presence creates a problem.
And that problem is further exascerbated by the difficulty of knowing the
blank is there when it is followed by more whitespace in the form of an
endline character.

If someone exclusively used an editor that displayed blanks as grey dots,
then it might be less important for them to do this (unless, of course, this
is one of the editors that indiscriminately pads lines with blanks), but one
can never be sure that one's script will never be edited by someone else
with a different editor.

If an Editor is indeed appending the trailing space, it would be worthwhile
to know which editor does this so as to warn others to watch out for this
behavior.
 
A

Al Dunbar [MS-MVP]

Alexander Suhovey said:
[snip]
We could argue endlessly

Yes we could though I wouldn't call it arguing, rather an interesting
conversation.

I disagree. A conversation, interesting or otherwise, can be classified as
an argument if there is a disagreement of opinion, and/or if one or either
side is attempting to get the other side to change their opinion on a
matter. Unfortunately, the word "argue" seems to have taken on the more
negative aspect where there also is enmity involved. The lawyers who argue
cases against each other are often the best of friends.
I could say that to me

(set DAY=%%1)
(set TIME=%%2)

doesn't look much better than

set DAY=%%1&
set TIME=%%2&

And you would be quite right to say that that is your opinion. To
extrapolate your opinion to the level of absolute truth, now that would be
wrong. You don't seem to do that, but there are some that do.

Now, I'm not necessarily saying that I like the looks of my code when I see
set commands in parentheses - but I certainly do appreciate how they tend to
reduce simple syntactical errors.
and intent of "&" isn't less apparent here as that of parentheses.

I see what you mean, as using the command separator to terminate one command
without a second following is obviously a use different from its more common
use. Of course, the uninitiated would probably wonder why you would be
appending an ampersand to a variable containing date or time information,
whereas he'd be less likely (imho) to assume that in the examples with
parentheses the intent was to append a ")", as he would see the matching
"(".

as an aside, I just did some testing and found that null statements seem to
generate error messages, for example:

()
&echo/testing

Odd, then, that this doesn't:

echo/testing&
But the
thing is, I actually agree with you. By calling it ugly in my initial post I
was trying to say that it's just another way to address the issue though
least preferable one.

And, of course, I generally agree with you as well. Interesting that agree ~
argee ~ argue...

/Al
 
A

Al Dunbar [MS-MVP]

Timo Salmi said:
Back to the question posed in the subject. The solutions so far work for
a single word, but insidiously not directly beyond that.

I disagree.
Here is a small
demo

@echo off & setlocal enableextensions
set S= This is a test
echo %S%.
(for /f "tokens=*" %%a in ('echo %S%') do set T=%%a)
echo %T%.
endlocal & goto :EOF

The output
C:\_D\BAS>cmdfaq
This is a test .
This is a test .

It is not that the final ")" in the for statement failed in its task, but
that the variable itself, "%%a", contained a trailing blank. I would
consider it an error on the part of the command processor for the ")" to
have the ability to edit the content of a variable, as that is not noted
anywhere in the documentation as its purpose.

Contrariwise, I maintain that the ")" succeeded in its task of preventing
the inadvertent appending of additional blank characters beyond the
characters contained in the variable. The ")" doesn't strip trailing blanks,
but is simply used to explicitly show what characters are included in the
command. Consider this sequence, for example:

(set blanky= trim this )
echo/.%blanky%.

What would you expect the output to appear as ".trim this.", ". trim this
..", or ". trim this ."?
Well, e.g. sed can be used to get rid of the remaining trailing blank.

But the potential issue of trailing blanks appearing in a variable is a
slightly different issue than whether or not we knowingly put them there.

/Al
 
L

leew

I don't usually see problems with spaces... but for this instance, I
would do it differently. Instead of setting the DAY variable through a
for command, why not just reference the %date% variable.

%date:~0,3% (assuming US-EN format).

So,

SET DAY=%date:~0,3%

(which basically says, start at the 0 character of %date% and take the
first 3 only).

-Lee
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top