Manipulating directory string
Moderator: DosItHelp
Manipulating directory string
Hi,
So I have
set /p DestPath=
in my batch file to which if the user enters "C:\Program Files\etc" (and this could be any path. This is just one e.g.)
I want to capture "C:\Program Files\" as DestPath which is one one less directory.
How do I do this?
Thanks,
Bobby
So I have
set /p DestPath=
in my batch file to which if the user enters "C:\Program Files\etc" (and this could be any path. This is just one e.g.)
I want to capture "C:\Program Files\" as DestPath which is one one less directory.
How do I do this?
Thanks,
Bobby
Re: Manipulating directory string
'
MODIFIED on 11 Aug 2011 14:27 set "$trim=%%~!"
Next code gets parent path without checking if the path actually exists, which I thought necessary in your case.
WARNING: UNTESTED !
Depending of the size of the script and level of abstraction, you will eventually may want to start using :functions or @macros to accomplish your goals but I'm getting sarcastic.
MODIFIED on 11 Aug 2011 14:27 set "$trim=%%~!"
Next code gets parent path without checking if the path actually exists, which I thought necessary in your case.
Code: Select all
@echo off &SetLocal EnableExtensions EnableDelayedExpansion
::(
set /p DestPath=
set "$=!DestPath!"
set "$=!$!\###"
set "$=!$:\\###=!"
set "$=!$:\###=!"
set "$=!$:\=" "!"
set "$="!$!""
for %%! in ( !$! ) do set "$trim=%%~!"
set "$=!$!###"
set "DestPath=!DestPath:%$trim%###=!"
set "DestPath"
::)
EndLocal &pause &exit /b
Depending of the size of the script and level of abstraction, you will eventually may want to start using :functions or @macros to accomplish your goals but I'm getting sarcastic.
Code: Select all
%@forU% ( 'DestPath, "\", 1' ) %@TrimRight%
Last edited by Ed Dyreen on 11 Aug 2011 13:28, edited 1 time in total.
-
- Posts: 1
- Joined: 10 Aug 2011 22:10
Re: Manipulating directory string
Google trying to give good results to end users so if you do manipulating your site must be penalize.
NoNo Hair
NoNo Hair
Re: Manipulating directory string
This works:
Nice features:
- validates the path - sets errorlevel and gives error message if invalid
- destination path is not set if input path is invalid
- reports the full path of the parent, even if the input path is relative
Additional Feature:
- destination path will equal input path if input path is the root (has no parent)
Limitation:
- user must have access rights to both input and parent paths
Dave Benham
Code: Select all
@echo off
setlocal
set /p DestPath=Enter a valid path:
set DestPath
call :getParent %DestPath% DestParent
set DestParent
exit /b
:getParent path
pushd "%~1" || exit /b
cd..
set "%2=%cd%"
popd
exit /b
Nice features:
- validates the path - sets errorlevel and gives error message if invalid
- destination path is not set if input path is invalid
- reports the full path of the parent, even if the input path is relative
Additional Feature:
- destination path will equal input path if input path is the root (has no parent)
Limitation:
- user must have access rights to both input and parent paths
Dave Benham
Re: Manipulating directory string
please stop complicating things especialy Ed Dyreen who forever try to make things complex and worse instead of solving the issue
i call Ed Dyreen codes a MAGICAL TALISMAN
what about this
nothing is simple than that
i call Ed Dyreen codes a MAGICAL TALISMAN
what about this
Code: Select all
for %%G in ("%DestPath%") do set "parentdir=%%~dpG"
nothing is simple than that
-
- Posts: 126
- Joined: 10 Jun 2011 10:30
Re: Manipulating directory string
I'll agree that Ed's code is usually more complex than needs to be to solve the task...
However... I don't see Dave's code as any more complex than yours. Condensing something down to 1 line != simplifying.
You're code will not work if they include a \ at the end of the directory listing, while push/pop and set = %CD% will
EDIT:
And
EDIT2:
@ED,
That's an interesting way of breaking down the path, but in the end, you don't strip off the last directory... I see how it's supposed to work, but this line:
set "DestPath=!DestPath:%$trim%###=!" doesn't do what it needs to do. I think it's not expanding correctly inside the code block.
However... I don't see Dave's code as any more complex than yours. Condensing something down to 1 line != simplifying.
You're code will not work if they include a \ at the end of the directory listing, while push/pop and set = %CD% will
EDIT:
And
Nice features:
- validates the path - sets errorlevel and gives error message if invalid
- destination path is not set if input path is invalid
- reports the full path of the parent, even if the input path is relative
Additional Feature:
- destination path will equal input path if input path is the root (has no parent)
EDIT2:
@ED,
That's an interesting way of breaking down the path, but in the end, you don't strip off the last directory... I see how it's supposed to work, but this line:
set "DestPath=!DestPath:%$trim%###=!" doesn't do what it needs to do. I think it's not expanding correctly inside the code block.
Re: Manipulating directory string
'
@Acy, I modified the code, I made a mistake at line set "$trim=%%!" corrected to set "$trim=%%~!"
All solutions are good in my opinion, it gives people a choice:
-My code is written with the idea that we can't check the path if it don't exist yet.
-Dave's code depends on this due to the push &pop features.
-Allal's code will work if used correctly.
@Acy, I modified the code, I made a mistake at line set "$trim=%%!" corrected to set "$trim=%%~!"
All solutions are good in my opinion, it gives people a choice:
-My code is written with the idea that we can't check the path if it don't exist yet.
-Dave's code depends on this due to the push &pop features.
-Allal's code will work if used correctly.
-
- Posts: 1
- Joined: 11 Aug 2011 22:08
Re: Manipulating directory string
Google trying to give good results to end users so if you do manipulating your site must be penalize.
NoNo Hair
NoNo Hair