splitting strings
Moderator: DosItHelp
-
- Posts: 4
- Joined: 08 Sep 2013 04:31
splitting strings
Here is my code:
@SETLOCAL enableextensions enabledelayedexpansion
@ECHO off
for /f "tokens=1,* delims=\" %%a in ("D:\Blabla") do (set dossier=%%a echo %dossier%)
I'm trying to split the path of a given file/folder in order to separate the path's elements. The problem is that the programme doesn't loop. I have tried removing the first two lines as well as removing tokens=1,* and one of the previous versions had only echo %%a in its loop. So far, I only managed to have the loop run once instead of twice (I have always tested with the same path).
I started programming in batch a few days ago, so I'm clueless about its syntax. Can someone, please, help me? Thank you!!!!
@SETLOCAL enableextensions enabledelayedexpansion
@ECHO off
for /f "tokens=1,* delims=\" %%a in ("D:\Blabla") do (set dossier=%%a echo %dossier%)
I'm trying to split the path of a given file/folder in order to separate the path's elements. The problem is that the programme doesn't loop. I have tried removing the first two lines as well as removing tokens=1,* and one of the previous versions had only echo %%a in its loop. So far, I only managed to have the loop run once instead of twice (I have always tested with the same path).
I started programming in batch a few days ago, so I'm clueless about its syntax. Can someone, please, help me? Thank you!!!!
Re: splitting strings
At the end of the FOR help message you'll find the modifiers of the FOR variables.
Regards
aGerman
Code: Select all
@echo off &setlocal
for %%I in ("C:\somewhere\on\your\disk\test.txt") do (
set "fDrive=%%~dI"
set "fPath=%%~pI"
set "fName=%%~nxI"
set "fBaseName=%%~nI"
set "fExtension=%%~xI"
)
echo %fDrive%
echo %fPath%
echo %fName%
echo %fBaseName%
echo %fExtension%
pause
Regards
aGerman
-
- Posts: 4
- Joined: 08 Sep 2013 04:31
Re: splitting strings
Thx! I will try it! 

Last edited by batchanonym on 08 Sep 2013 05:37, edited 1 time in total.
-
- Posts: 4
- Joined: 08 Sep 2013 04:31
Re: splitting strings
I wasn't really looking for that. I tested your code and I got the folowing results:
C:
\somewhere\on\your\disk\
test.txt
test
.txt
and what I'm trying to get is the following:
C:
somewhere
on
your
disk
test.txt
Anyway, thx for the help.
C:
\somewhere\on\your\disk\
test.txt
test
.txt
and what I'm trying to get is the following:
C:
somewhere
on
your
disk
test.txt
Anyway, thx for the help.
Re: splitting strings
I see. Try:
"%fFullName:\=" "%" means that each of the backslashes is replaced by " " (including the quotation marks). The resulting string is
"C:" "somewhere" "on" "your" "disk" "test.txt"
See also: SET.
A simple FOR loop iterates over the string and assignes every substring to variable %%I step by step. The tilde character (~) removes the enclosing quotation marks.
Regards
aGerman
Code: Select all
@echo off &setlocal
set "fFullName=C:\somewhere\on\your\disk\test.txt"
for %%I in ("%fFullName:\=" "%") do (
if "%%~I" neq "" echo(%%~I
)
pause
"%fFullName:\=" "%" means that each of the backslashes is replaced by " " (including the quotation marks). The resulting string is
"C:" "somewhere" "on" "your" "disk" "test.txt"
See also: SET.
A simple FOR loop iterates over the string and assignes every substring to variable %%I step by step. The tilde character (~) removes the enclosing quotation marks.
Regards
aGerman
-
- Posts: 4
- Joined: 08 Sep 2013 04:31
Re: splitting strings
Thx it is exactly what I was looking for!
Re: splitting strings
You're welcome.
Re: splitting strings
Code: Select all
@echo off
set "some_dir=C:\somewhere\on\your\disk\test.txt"
set "dir_parts=%some_dir:\=&echo.%"
echo %dir_parts%
