In a project with a batchfile I am trying to capture the last word of a
directory-line like this:
C:\school\source\linetest\LineTest\src\linetest
-----------
where I want to get the string "linetest" and assign it to a variable - How
can this be done using a batch file ?
Reason:
I need to execute a Lego Robot Java command : lejos.exe linetest.Main in the
directory \src but this is a minor part of the problem - isolating the last
word is the main trouble.
Thanks in advance,
Lars Lindgren
save this batchfile, and name it CommandExtensions.bat
@echo off
echo %1: a %~a1
echo %1: d %~d1
echo %1: f %~f1
echo %1: n %~n1
echo %1: p %~p1
echo %1: s %~s1
echo %1: t %~t1
echo %1: x %~x1
echo %1: z %~z1
on my system its stored in D:\UTIL\CommandExtensions.bat
executing:
d:\util\CommandExtensions.bat d:\util\CommandExtenstions.bat
gives:
D:\>d:\util\CommandExtensions.bat d:\util\CommandExtensions.bat
d:\util\CommandExtensions.bat: a --a------
d:\util\CommandExtensions.bat: d d:
d:\util\CommandExtensions.bat: f d:\UTIL\CommandExtensions.bat
d:\util\CommandExtensions.bat: n CommandExtensions
d:\util\CommandExtensions.bat: p \UTIL\
d:\util\CommandExtensions.bat: s d:\UTIL\COMMAN~1.BAT
d:\util\CommandExtensions.bat: t 02-02-2008 13:15
d:\util\CommandExtensions.bat: x .bat
d:\util\CommandExtensions.bat: z 164
--
Luuk
>In a project with a batchfile I am trying to capture the last word of a
>directory-line like this:
>
>C:\school\source\linetest\LineTest\src\linetest
> --------
>
>where I want to get the string "linetest" and assign it to a variable - How
>can this be done using a batch file ?
>
>Reason:
>I need to execute a Lego Robot Java command : lejos.exe linetest.Main in the
>directory \src but this is a minor part of the problem - isolating the last
>word is the main trouble.
For W2K/XP etc
@echo off
for /f "delims=" %%a in (
"C:\school\source\linetest\LineTest\src\linetest"
) do set var=%%~na
echo %var%
pause
I forgot to mention that (most) of these options are mentioned in the
help-screen when typing:
FOR /?
(when someone needs this for reference)
--
Luuk
> In a project with a batchfile I am trying to capture the last word of a
> directory-line like this:
>
> C:\school\source\linetest\LineTest\src\linetest
> --------
>
> where I want to get the string "linetest" and assign it to a variable - How
> can this be done using a batch file ?
*** DOS users may incorporate XSET for this. One would use "FDIR",
along with the "/WORD" and "/SEPARATOR" functions to get the directory and
then to select the last word by designating the `/' as the word separator.
Richard Bonner
http://www.chebucto.ca/~ak621/DOS/
OK, thanks for all the good sugestions everybody - Got a solution working as
a result of your answers :-) .. Thanks.
Lars Lindgren
The delims & the switch /f is are needed, this should even work in DOS I
guess...
@echo off
rem for /f "delims=" %%a in (
for %%a in (
"C:\school\source\linetest\LineTest\src\linetest"
) do set var=%%~na
echo %var%
pause
>this should even work in DOS I guess...
Yes, it works fine. Won't work in DOS or Win9x/ME though.
>@echo off