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

T

thinktwice

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?
 
F

foxidrive

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 \
 
D

Dean Wells \(MVP\)

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
 
E

Esra Sdrawkcab

Dean said:
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
?
 
D

Dean Wells \(MVP\)

That's a relative path, I'd assumed it was clear I was supplying a means
of identifying an absolute path.
 
T

thinktwice

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?
 
D

Dean Wells \(MVP\)

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:\
 
T

thinktwice

but how could i analyze the path character by character? for loop
seems could only process token by token
 
D

Dean Wells \(MVP\)

.... 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) said:
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 said:
but how could i analyze the path character by character? for loop
seems could only process token by token
 
T

thinktwice

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


How about
..\uponefolder\siblingfolder
?

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

thinktwice

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 :).


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


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?
 
T

thinktwice

by the way, you remind me that file:///c:\somefolder might be another
case :)

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


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?
 
D

Dean Wells \(MVP\)

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 said:
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 :).


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


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?
 
D

Dean Wells \(MVP\)

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 said:
by the way, you remind me that file:///c:\somefolder might be another
case :)

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


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?
 
D

Dean Wells \(MVP\)

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 said:
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


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
?

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

Tom Lavedas

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

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

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 ...

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

Timo Salmi

thinktwice said:
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
 
D

Dean Wells \(MVP\)

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 said:
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
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
?

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

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 ...

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

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