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

get rid of the end of a variable

0 views
Skip to first unread message

black...@gmail.com

unread,
Feb 5, 2008, 5:16:10 AM2/5/08
to
Hello
I have an environment variable named something linke
TS_X=d:\folder1\folder2\folder3
I would like to have in another variable d:\folder1\folder2

Is there any way of doing this?
thank you

billious

unread,
Feb 5, 2008, 5:44:48 AM2/5/08
to

<black...@gmail.com> wrote in message
news:6625f541-b0e8-4a9c...@d70g2000hsb.googlegroups.com...


NT4/2K/XP/2K3 (NT+ systems) are discussed in alt.msdos.batch.nt as the
techniques used differ markedly from DOS/9x methods.

You appear to be posting from XP, so this should work for you

----- batch begins -------
[1]@echo off
[2]set ts_x=d:\folder1\folder2\folder3
[3]:: method 1
[4]for /f %%i in ("%ts_x%") do set ts_y=%%~dpi
[5]set ts_y=%ts_y:~0,-1%
[6]set ts
[7]:: method 2
[8]setlocal enabledelayedexpansion
[9]for /f %%i in ("%ts_x%") do set ts_y=%%~dpi&set ts_y=!ts_y:~0,-1!
[10]set ts
------ batch ends --------

Lines start [number] - any lines not starting [number] have been wrapped and
should be rejoined. The [number] that starts the line should be removed

%varname% will be evaluated as the value of VARNAME at the time that the
line is PARSED. The ENABLEDELAYEDEXPANSION option to SETLOCAL causes
!varname! to be evaluated as the CURRENT value of VARNAME - that is, as
modified by the operation of the FOR

[4..5] and [8..9] are different methods to do the same thing.
[6,10] simply show the results

YMMV if you use spaces or other special characters in your names. The trick
is to treat the name as a filename and extract the resultant drive and
directory name using the ~operator; then remove the terminal "\"

see

SETLOCAL /?

FOR /?

SET /?

from the prompt for more complete documentation - or alt.msdos.batch.nt for
examples....


foxidrive

unread,
Feb 5, 2008, 5:52:33 AM2/5/08
to
On Tue, 5 Feb 2008 02:16:10 -0800 (PST), black...@gmail.com wrote:

>Hello
>I have an environment variable named something linke
>TS_X=d:\folder1\folder2\folder3
>I would like to have in another variable d:\folder1\folder2

In NT versions of Windows:

@echo off
set "TS_X=d:\folder1\folder2\folder3"
for %%a in ("%ts_x%") do set "TS_Y=%%~dpa"

Timo Salmi

unread,
Feb 5, 2008, 12:41:37 PM2/5/08
to
black...@gmail.com wrote in alt.msdos.batch:

> I have an environment variable named something linke
> TS_X=d:\folder1\folder2\folder3 I would like to have in another
> variable d:\folder1\folder2 Is there any way of doing this?

Since you seem to post from an NT-system, the more appropriate newsgroup
for this is alt.msdos.batch.nt (included and followups redirected).

As to the problem, adapted from a concomitant
80} How can I extract the last part of a path such as C:\ABC\DEF\GHI\?
202569 Dec 23 2007 ftp://garbo.uwasa.fi/pc/link/tscmd.zip
Http link format http://garbo.uwasa.fi/pub/pc/link/tscmd.zip
tscmd.zip Useful NT/2000/XP script tricks and tips, T.Salmi

@echo off & setlocal enableextensions


set TS_X=d:\folder1\folder2\folder3

echo %TS_X%
for %%a in ("%TS_X%") do echo %%~dpa
endlocal & goto :EOF

Output:
C:\_D\TEST>cmdfaq
d:\folder1\folder2\folder3
d:\folder1\folder2\

For more, see the said item and FAQ.

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 batch files and tricks ftp://garbo.uwasa.fi/pc/link/tsbat.zip

FileGod

unread,
Feb 17, 2008, 2:12:11 AM2/17/08
to
How's this?
cd..
cd>temp.txt
No up to date newer operating system required, this can help get it into the
enviroment, sorry I am not giving a very good example...

black...@gmail.com wrote:
>Hello

0 new messages