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

how to judge whether a path is relative path or absolute path

4 views
Skip to first unread message

thinktwice

unread,
May 6, 2008, 11:26:27 AM5/6/08
to
is there any system available command to do that? or i have to analyze
the path character by character?
seems it's not that easy to judge a path is valid ,right?

Tom Lavedas

unread,
May 6, 2008, 11:49:21 AM5/6/08
to

It's commonly done using an IF EXIST test.

Tom Lavedas
===========
http://members.cox.net/tglbatch/wsh/

foxidrive

unread,
May 6, 2008, 12:56:07 PM5/6/08
to
On Tue, 6 May 2008 08:26:27 -0700 (PDT), thinktwice <memor...@gmail.com>
wrote:

>is there any system available command to do that? or i have to analyze
>the path character by character?
>seems it's not that easy to judge a path is valid ,right?

Relative or absolute?

An absolute path will contain a : character or start with a \

Dean Wells (MVP)

unread,
May 6, 2008, 2:33:03 PM5/6/08
to
For validity, use an 'if exist'. To determine whether a supplied path
is relative or absolute, verify that one of the following is true -

1. the second and third characters are a colon and backslash
respectively
2. the first character is a backslash

--
Dean Wells [MVP / Directory Services]
MSEtechnology
[[ Please respond to the Newsgroup only regarding posts ]]
R e m o v e t h e m a s k t o s e n d e m a i l


"thinktwice" <memor...@gmail.com> wrote in message
news:313f430d-6da8-4865...@w5g2000prd.googlegroups.com...

Esra Sdrawkcab

unread,
May 6, 2008, 3:46:56 PM5/6/08
to
Dean Wells (MVP) wrote:
> For validity, use an 'if exist'. To determine whether a supplied path
> is relative or absolute, verify that one of the following is true -
>
> 1. the second and third characters are a colon and backslash
> respectively
> 2. the first character is a backslash
>
How about
..\uponefolder\siblingfolder
?

Dean Wells (MVP)

unread,
May 6, 2008, 6:29:13 PM5/6/08
to
That's a relative path, I'd assumed it was clear I was supplying a means
of identifying an absolute path.

--
Dean Wells [MVP / Directory Services]
MSEtechnology
[[ Please respond to the Newsgroup only regarding posts ]]
R e m o v e t h e m a s k t o s e n d e m a i l


"Esra Sdrawkcab" <ad...@127.0.0.1> wrote in message
news:QA2Uj.109012$Ff4....@newsfe5-win.ntli.net...

thinktwice

unread,
May 6, 2008, 9:09:15 PM5/6/08
to
i think these are all the possibile forms to be an absolute path
x:\somefolder
x:\somefolder\..\anotherfolder
\\ip\somefolder

possibile forms of being a relative path
//relative path
..\somefolder
.\somefolder
..\somefolder\..\anotherfolder

do i miss any cases here?

Dean Wells (MVP)

unread,
May 6, 2008, 9:19:28 PM5/6/08
to
The UNC path is a great addition and extends this scenario to
non-locally mapped media; good point. I'd also add (albeit in
hindsight) the edge-case examples below -

\\.\c:\

... or

\\?\c:\

--
Dean Wells [MVP / Directory Services]
MSEtechnology
[[ Please respond to the Newsgroup only regarding posts ]]
R e m o v e t h e m a s k t o s e n d e m a i l


"thinktwice" <memor...@gmail.com> wrote in message

news:59fce814-30cc-4794...@h1g2000prh.googlegroups.com...

thinktwice

unread,
May 6, 2008, 9:26:54 PM5/6/08
to
but how could i analyze the path character by character? for loop
seems could only process token by token

Dean Wells (MVP)

unread,
May 6, 2008, 9:54:49 PM5/6/08
to
e.g. -

%VAR:~1,1%

... 1,1: starting char + chars

--
Dean Wells [MVP / Directory Services]
MSEtechnology
[[ Please respond to the Newsgroup only regarding posts ]]
R e m o v e t h e m a s k t o s e n d e m a i l


"thinktwice" <memor...@gmail.com> wrote in message

news:88489b3a-c424-478b...@l17g2000pri.googlegroups.com...

Dean Wells (MVP)

unread,
May 6, 2008, 10:01:08 PM5/6/08
to
... note the first value specifies an offset or should be treated as
zero-based.

--
Dean Wells [MVP / Directory Services]
MSEtechnology
[[ Please respond to the Newsgroup only regarding posts ]]
R e m o v e t h e m a s k t o s e n d e m a i l


"Dean Wells (MVP)" <dwe...@maskmsetechnology.com> wrote in message
news:uGJuWV%23rIH...@TK2MSFTNGP02.phx.gbl...

thinktwice

unread,
May 6, 2008, 10:14:04 PM5/6/08
to
On May 7, 6:29 am, "Dean Wells \(MVP\)" <dwe...@maskmsetechnology.com>
wrote:

by the way , could i analyze a literal string char by char ? seems for
loop could only process token by token

thinktwice

unread,
May 6, 2008, 11:00:59 PM5/6/08
to
thianks ,Dean.

i didn't know \\.\c:\, \\?\c:\ could be valid path beofre. i have
tried these on my windows system, but seems the system doesn't
recognize these paths. are they linux related? by the way, i guess
dealing with popular forms is enough, if someone want to use the
special cases as u gave, i might tell them that's not supported :).


On May 7, 9:19 am, "Dean Wells \(MVP\)" <dwe...@maskmsetechnology.com>
wrote:


> The UNC path is a great addition and extends this scenario to
> non-locally mapped media; good point. I'd also add (albeit in
> hindsight) the edge-case examples below -
>
> \\.\c:\
>
> ... or
>
> \\?\c:\
>
> --
> Dean Wells [MVP / Directory Services]
> MSEtechnology
> [[ Please respond to the Newsgroup only regarding posts ]]
> R e m o v e t h e m a s k t o s e n d e m a i l
>

> "thinktwice" <memorial...@gmail.com> wrote in message

thinktwice

unread,
May 6, 2008, 11:05:43 PM5/6/08
to
by the way, you remind me that file:///c:\somefolder might be another
case :)

On May 7, 9:19 am, "Dean Wells \(MVP\)" <dwe...@maskmsetechnology.com>
wrote:

> The UNC path is a great addition and extends this scenario to
> non-locally mapped media; good point. I'd also add (albeit in
> hindsight) the edge-case examples below -
>
> \\.\c:\
>
> ... or
>
> \\?\c:\
>
> --
> Dean Wells [MVP / Directory Services]
> MSEtechnology
> [[ Please respond to the Newsgroup only regarding posts ]]
> R e m o v e t h e m a s k t o s e n d e m a i l
>

> "thinktwice" <memorial...@gmail.com> wrote in message

Dean Wells (MVP)

unread,
May 7, 2008, 8:47:25 AM5/7/08
to
Their support is limited; off the top of my head both 'dir' and 'del'
support them (especially useful for deleting files with poisened
filenames.)

--
Dean Wells [MVP / Directory Services]
MSEtechnology
[[ Please respond to the Newsgroup only regarding posts ]]
R e m o v e t h e m a s k t o s e n d e m a i l

"thinktwice" <memor...@gmail.com> wrote in message

news:182aa84d-53cc-46ad...@q1g2000prf.googlegroups.com...

Dean Wells (MVP)

unread,
May 7, 2008, 8:49:44 AM5/7/08
to
Although that particular type of URI exploits the 'file' handler, to my
mind at least, it's beyond scope here.

--
Dean Wells [MVP / Directory Services]
MSEtechnology
[[ Please respond to the Newsgroup only regarding posts ]]
R e m o v e t h e m a s k t o s e n d e m a i l

"thinktwice" <memor...@gmail.com> wrote in message

news:f02dba08-5c20-439c...@b9g2000prh.googlegroups.com...

Dean Wells (MVP)

unread,
May 7, 2008, 8:55:15 AM5/7/08
to
Here's an example that you can build on -

set sPOS=%1
call echo %%PATH:~%sPOS%,1%%

--
Dean Wells [MVP / Directory Services]
MSEtechnology
[[ Please respond to the Newsgroup only regarding posts ]]
R e m o v e t h e m a s k t o s e n d e m a i l

"thinktwice" <memor...@gmail.com> wrote in message

news:f8560018-29c2-4ef9...@i36g2000prf.googlegroups.com...

Tom Lavedas

unread,
May 7, 2008, 9:24:13 AM5/7/08
to

Here's one way I can think of to parse a string ...

@echo off
set "string=%~1"
set "n="
:loop
if not defined string goto :EOF
set /a n+=1
set "char=%string:~0,1%"
set "string=%string:~1%"
echo %n% %char% %string%
goto loop

I thought if I knew the length of the string, it could also be done in
a FOR /L statement, but I couldn't find a good solution to the 'find
the length' problem in a google search. So, I constructed this
one ...

@echo off
echo.|set /p "x=%1" > %temp%\tmp.txt
for %%a in (%temp%\tmp.txt) do set Length=%%~za
del %temp%\tmp.txt
echo %Length%

So with the length the FOR parsing goes something like this ...

@echo off
echo.|set /p "x=%1" > %temp%\tmp.txt
for %%a in (%temp%\tmp.txt) do set /a c=%%~za-1
del %temp%\tmp.txt
set "string=%~1"
for /l %%a in (0,1,%c%) do call echo %%a %%string:~%%a,1%%

The looping solution seems a tiny bit more useful to me, but ...

Timo Salmi

unread,
May 7, 2008, 9:36:12 AM5/7/08
to
thinktwice <memor...@gmail.com> wrote:
> by the way , could i analyze a literal string char by char ? seems for
> loop could only process token by token

set s=whatever
for /l %%c in (0,1,255) do (
set si=!s:~%%c!
WhateverYouWishToDoWithSi
)

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FI-65101, Finland
Useful CMD script tricks http://www.netikka.net/tsneti/info/tscmd.htm

Dean Wells (MVP)

unread,
May 7, 2008, 9:44:35 AM5/7/08
to
Nod, this one is oddly tough; here's two solutions I've used over the
years (word-wrap almost certainly going to be a problem below) -

:: 2 approaches below

:: ------------------------

:: FIRST

:: Example script that demonstrates how to count characters by piping
UNICODE output through
:: non-UNICODE console filters

@echo off
setlocal ENABLEDELAYEDEXPANSION

:: Space char. substitution below (I used a period) was necessary as
when %* or %VAR% is expanded by
:: the for-in-do below, it concatenated any number of sequential spaces
to 1 space destroying an
:: accurate count
set VAR=%*
set VAR=%VAR: =.%

if "%VAR%"=="" (
set CNT=0
) else (
for /f "delims=[] tokens=1" %%l in ('cmd /u /c echo %VAR%^| find /v /n
""') do (
set /a CNT=%%l-3
)
)

echo %CNT%

goto :EOF

:: -------------------------

:: SECOND

:: Uses byte size within a temporary file

:getLEN
set LENGTH=0
set /p=%1>%tempFILE%<nul
for /f %%l in ("%tempFILE%") do set LENGTH=%%~zl
goto :EOF


--
Dean Wells [MVP / Directory Services]
MSEtechnology
[[ Please respond to the Newsgroup only regarding posts ]]
R e m o v e t h e m a s k t o s e n d e m a i l


"Tom Lavedas" <tglb...@cox.net> wrote in message
news:f233d609-b206-46a0...@d1g2000hsg.googlegroups.com...

foxidrive

unread,
May 7, 2008, 6:06:21 PM5/7/08
to
On Tue, 6 May 2008 18:26:54 -0700 (PDT), thinktwice <memor...@gmail.com>
wrote:

>but how could i analyze the path character by character? for loop


>seems could only process token by token

@echo off
set var=%1
echo %var%|find ":">nul&& echo path is absolute
echo %var%|findstr /r "^\\.*" >nul&& echo path is absolute

0 new messages