Page 1 of 1

Manipulating directory string

Posted: 10 Aug 2011 16:52
by A_Bobby
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

Re: Manipulating directory string

Posted: 10 Aug 2011 18:17
by Ed Dyreen
'
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
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.

Code: Select all

%@forU% ( 'DestPath, "\", 1' ) %@TrimRight%

Re: Manipulating directory string

Posted: 10 Aug 2011 22:44
by ceciliaxywatkins
Google trying to give good results to end users so if you do manipulating your site must be penalize.
NoNo Hair

Re: Manipulating directory string

Posted: 10 Aug 2011 23:31
by dbenham
This works:

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

Posted: 11 Aug 2011 11:30
by allal
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

Code: Select all

for %%G in ("%DestPath%") do set "parentdir=%%~dpG"


nothing is simple than that

Re: Manipulating directory string

Posted: 11 Aug 2011 11:56
by Acy Forsythe
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
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

Posted: 11 Aug 2011 13:38
by Ed Dyreen
'
@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.

Re: Manipulating directory string

Posted: 11 Aug 2011 22:17
by michellexy
Google trying to give good results to end users so if you do manipulating your site must be penalize.
NoNo Hair