Manipulating directory string

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
A_Bobby
Posts: 38
Joined: 21 Oct 2010 12:48

Manipulating directory string

#1 Post by A_Bobby » 10 Aug 2011 16:52

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

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Manipulating directory string

#2 Post by Ed Dyreen » 10 Aug 2011 18:17

'
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%
Last edited by Ed Dyreen on 11 Aug 2011 13:28, edited 1 time in total.

ceciliaxywatkins
Posts: 1
Joined: 10 Aug 2011 22:10

Re: Manipulating directory string

#3 Post by ceciliaxywatkins » 10 Aug 2011 22:44

Google trying to give good results to end users so if you do manipulating your site must be penalize.
NoNo Hair

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Manipulating directory string

#4 Post by dbenham » 10 Aug 2011 23:31

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

allal
Posts: 34
Joined: 04 Jun 2011 05:49

Re: Manipulating directory string

#5 Post by allal » 11 Aug 2011 11:30

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

Acy Forsythe
Posts: 126
Joined: 10 Jun 2011 10:30

Re: Manipulating directory string

#6 Post by Acy Forsythe » 11 Aug 2011 11:56

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.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Manipulating directory string

#7 Post by Ed Dyreen » 11 Aug 2011 13:38

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

michellexy
Posts: 1
Joined: 11 Aug 2011 22:08

Re: Manipulating directory string

#8 Post by michellexy » 11 Aug 2011 22:17

Google trying to give good results to end users so if you do manipulating your site must be penalize.
NoNo Hair

Post Reply