Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Add backslash to end of path

2,168 views
Skip to first unread message

JJ

unread,
Dec 2, 2010, 5:31:12 PM12/2/10
to
I have a batch file that requires passing in a path as a command line
argument. Then full file paths are built around that argument. How
can I append a backslash to the string only when it doesn't already
end in a backslash?

foxidrive

unread,
Dec 2, 2010, 6:03:40 PM12/2/10
to


set "newpath=%~1.\"


this will resolve to (for EG:)

c:\windows.\
or
c:\windows\.\

and both of those are equivalent to

c:\windows

--
Regards,
Mic

billious

unread,
Dec 2, 2010, 10:23:50 PM12/2/10
to

"foxidrive" <foxi...@gotcha.woohoo.invalid> wrote in message
news:gBVJo.50988$3f.2...@newsfe12.iad...

Now that's a clever method.

It is however targeted to the particular problem - not that there's anything
wrong with that...

There is however a hidden trap. If there is no argument defined, then
NEWPATH will be set to ".\" which may or may not be what's intended.

In a more general sense, I'd use

set var=%~1
if defined var if not "%var:~-1%"=="\" set var=%var%\

where VAR contains the string in question, noting the '-length' syntax of
the substring was introduced in XP, IIRC and the length could be varied if
required - for looking for appended switches, for instance.

So one could envisage a 'safe' routine along the lines of

set var=%~1
if not defined var echo MAYDAY! MAYDAY! VAR undefined&pause&exit
if not "%var:~-1%"=="\" set var=%var%\


I'm also intrigued by the way syntax seems to be exploited.

I'd suggest that in "c:\windows.\" the "." means "here be the end of the
NAME, there is no [further] extension" whereas in "c:\windows\.\" it means
"the current directory relative to the aforementioned directory."


JJ

unread,
Dec 3, 2010, 12:37:45 AM12/3/10
to
On Dec 2, 8:23 pm, "billious" <billious_1...@hotmail.com> wrote:
>
> In a more general sense, I'd use
>
> set var=%~1
> if defined var if not "%var:~-1%"=="\" set var=%var%\

Explain the '%var:~-1%' part to me. It looks like it just returns the
last character of the string and it works when var is set to %~1, but
when I try

set var=D:\folder


if defined var if not "%var:~-1%"=="\" set var=%var%\

I get a syntax error.

foxidrive

unread,
Dec 3, 2010, 12:52:15 AM12/3/10
to


I don't think you do. Try this batch file.

@echo off


set var=D:\folder
if defined var if not "%var:~-1%"=="\" set var=%var%\

echo %var%
pause


--
Regards,
Mic

billious

unread,
Dec 3, 2010, 7:03:31 AM12/3/10
to

You certainly shouldn't get an error if that's EXACTLY what batch is seeing.
It's perfectly valid syntax for XP and later.

see

SET /?

from the prompt for an incomplete analysis of the substringing options. In
short,

%var:~m,n% is the substring of n characters starting at position m in the
string (positions start from ZERO)
",n" is optional. If not specified, from position m to the end of the string
m or n negative means count of characters from the END of the string

hence %var:~-1% mas m=-1 and n omitted, so FROM 1 character away from the
end of the string TO the end of the string, which is the last character
alone.

Since spaces, commas, semicolons and tabs are separators, if VAR contains
any of these, any substring may equally contain them. If the substring is
surrounded by quotes, the separators are interpreted as regular characters,
hence quoting both sides protects against this potential minefield.

So - as to the cause of the syntax error - I'll presume you've checked your
listing twice (since that's quite the tradition, given the season)

I'd suggest you may be using Notepad. Please refrain. Use EDIT (my
preference) since Notepad has the reindeeresque habit of playing games with
the text, convinced that everything is some variety of WP file.


I'm_HERE

unread,
Dec 3, 2010, 7:12:02 AM12/3/10
to
another solution:

-------------------------------------------8<---------------------------------

@Echo oFF

Call :Test-backslash "C:\"
Set Test-path

Call :Test-backslash "%Windir%\System32\Drivers"
set test-path

Call :Test-backslash "C:\Documents and Settings\\\\\\"
set test-path

Call :Test-backslash "D:"
set test-path

pause & exit /b

:Test-backslash
Setlocal EnableExtensions
For /f "Tokens=1*" %%T In ('
cipher /Q "%~1\*"
') Do If Not Defined tp Set tp=%%U
Endlocal & (
Set "test-path=%tp%" & goto :eof
)

----- OR
--------------------------------8<------------------------------

Set "Var=yourPath"
2>Nul >Nul (Set Var && (
Set Var|FindStr "\\\>"||Set Var=%Var%\
))
Set Var

--------------------------------------8<-------------------------------

01MDM

unread,
Dec 3, 2010, 11:05:44 AM12/3/10
to

It's very easy. Try:

@echo off
setlocal

set "parampath=%~f1\"
set "parampath=%parampath:\\=\%"

echo %parampath%

foxidrive

unread,
Dec 3, 2010, 11:23:59 AM12/3/10
to

It's interesting to see the variations in techniques.


I've just tested a copy command in XP Pro and this works - I don't know
when this behaviour was implemented.

copy a.txt "d:\backup\abc\\def jhi\\"


So you can use double backslashes in XP at least.

--
Regards,
Mic

01MDM

unread,
Dec 3, 2010, 11:25:16 AM12/3/10
to

And you can add checking:

if "%~1"=="" goto:eof

JJ

unread,
Dec 3, 2010, 1:08:55 PM12/3/10
to
On Dec 3, 5:03 am, "billious" <billious_1...@hotmail.com> wrote:
>
> You certainly shouldn't get an error if that's EXACTLY what batch is seeing.
>

Had double quotes around the string. Fixed. Thanks.

Frank Westlake

unread,
Dec 3, 2010, 1:42:58 PM12/3/10
to
foxidrive wrote:
> It's interesting to see the variations in techniques.

I'm on a handheld so I'm shooting in the dark, but how about something
with FOR/D:

FOR /D %%d in (%path%) do SET "dir=%%fd"

Frank

foxidrive

unread,
Dec 3, 2010, 6:06:10 PM12/3/10
to
On 4/12/2010 05:42, Frank Westlake wrote:
> foxidrive wrote:
>> It's interesting to see the variations in techniques.
>
> I'm on a handheld so I'm shooting in the dark, but how about something
> with FOR/D:
>

Did you mean something like this?

FOR /D %%d in (%folder%) do SET "dir=%%~fd"


I find that it merely provides the literal folder that is in the
environment variable, Frank, no slashes added or removed.

--
Regards,
Mic

Frank Westlake

unread,
Dec 4, 2010, 9:44:18 AM12/4/10
to
foxidrive wrote:

> Did you mean something like this?

Something like.

> I find that it merely provides the literal folder that is in the
> environment variable, Frank, no slashes added or removed.

I only break out Windows once a week for a few minutes now and I
haven't used a Windows console in over a month. I expect that my
knowledge of CMD will fade to nothing in another month or two and I'll
have to abandon this group. Until then I'll continue to appear
confused and incompetent.

Frank

foxidrive

unread,
Dec 4, 2010, 10:02:49 AM12/4/10
to
On 5/12/2010 01:44, Frank Westlake wrote:

> I only break out Windows once a week for a few minutes now and I
> haven't used a Windows console in over a month. I expect that my
> knowledge of CMD will fade to nothing in another month or two

It happens doesn't it. Stop using some knowledge and before long it's
hard to remember what's what.

Do you write scripts in Linux to pass the time?


--
Regards,
Mic

Frank Westlake

unread,
Dec 4, 2010, 1:37:42 PM12/4/10
to
foxidrive wrote:
> Do you write scripts in Linux to pass the time?

It is normal that most of what my computer does was written by me. My
binary programs number over 100, and that does not include programs
for MSDOS and other operating systems. I'm guessing the total is well
over 200. Since Windows NT I have also done many things in CMD script,
and I think those number between 100 and 200 -- closer to 200.

A few months ago I had to replace my Palm TX and I bought a Archos 5
Internet Tablet -- a 5" diagonal handheld running Android 1.6, the
Linux OS. There is a scripting host for it (SL4A) which hosts about
six languages (JavaScript, Python, Ruby, PHP, Beanshell, PERL, ...) so
I have been very busy making it do things with JavaScript. This
JavaScript runs in the Rhino shell and has access to a large part of
the Java library, so it's very capable.

I don't recommend the Archos 5. It experiences the same problem my TX
did: the touchscreen alignment gets so bad that the system cannot
recalibrate it.

To pass the time? I don't know. I guess I have nothing else to do so
perhaps that is why.

Frank

foxidrive

unread,
Dec 4, 2010, 10:18:17 PM12/4/10
to
On 5/12/2010 05:37, Frank Westlake wrote:
> foxidrive wrote:
>> Do you write scripts in Linux to pass the time?
>
> It is normal that most of what my computer does was written by me. My
> binary programs number over 100, and that does not include programs
> for MSDOS and other operating systems. I'm guessing the total is well
> over 200. Since Windows NT I have also done many things in CMD script,
> and I think those number between 100 and 200 -- closer to 200.
>
> A few months ago I had to replace my Palm TX and I bought a Archos 5
> Internet Tablet -- a 5" diagonal handheld running Android 1.6, the
> Linux OS. There is a scripting host for it (SL4A) which hosts about
> six languages (JavaScript, Python, Ruby, PHP, Beanshell, PERL, ...) so
> I have been very busy making it do things with JavaScript. This
> JavaScript runs in the Rhino shell and has access to a large part of
> the Java library, so it's very capable.

It sounds like you enjoy it, and coding.

Shame about the dodgy touchscreen.


--
Regards,
Mic

Frank P. Westlake

unread,
Dec 5, 2010, 10:19:34 AM12/5/10
to
"JJ"
news:d95c88c2-0f04-48d5...@n32g2000pre.googlegroups.com...

> How can I append a backslash to the string only when it doesn't
> already
> end in a backslash?


One more method:

SET "directory=D:\humbug"
:: Prints directory=D:\humbug
SET directory

FOR %%a in (%directory%\.) do Set "directory=%%~fa\"
:: Prints directory=D:\humbug\
SET directory


FOR %%a in (%directory%\.) do Set "directory=%%~fa\"
:: Prints directory=D:\humbug\
SET directory

Also works with FOR/D.


Frank


foxidrive

unread,
Dec 5, 2010, 10:43:00 AM12/5/10
to
On 6/12/2010 02:19, Frank P. Westlake wrote:
> "JJ"
> news:d95c88c2-0f04-48d5...@n32g2000pre.googlegroups.com...
>> How can I append a backslash to the string only when it doesn't already
>> end in a backslash?
>
> One more method:
>

@echo off
SET "directory=D:\humbug\bonus folder"
SET directory

FOR %%a in ("%directory%\.") do Set "directory=%%~fa\"
SET directory

FOR %%a in ("%directory%\.") do Set "directory=%%~fa\"

SET directory
pause

A small amendment to make it function with long path names, Frank.


--
Regards,
Mic

0 new messages