splitting strings

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
batchanonym
Posts: 4
Joined: 08 Sep 2013 04:31

splitting strings

#1 Post by batchanonym » 08 Sep 2013 04:52

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!!!!

aGerman
Expert
Posts: 4743
Joined: 22 Jan 2010 18:01
Location: Germany

Re: splitting strings

#2 Post by aGerman » 08 Sep 2013 05:15

At the end of the FOR help message you'll find the modifiers of the FOR variables.

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

batchanonym
Posts: 4
Joined: 08 Sep 2013 04:31

Re: splitting strings

#3 Post by batchanonym » 08 Sep 2013 05:20

Thx! I will try it! :D
Last edited by batchanonym on 08 Sep 2013 05:37, edited 1 time in total.

batchanonym
Posts: 4
Joined: 08 Sep 2013 04:31

Re: splitting strings

#4 Post by batchanonym » 08 Sep 2013 05:23

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.

aGerman
Expert
Posts: 4743
Joined: 22 Jan 2010 18:01
Location: Germany

Re: splitting strings

#5 Post by aGerman » 08 Sep 2013 05:40

I see. Try:

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

batchanonym
Posts: 4
Joined: 08 Sep 2013 04:31

Re: splitting strings

#6 Post by batchanonym » 08 Sep 2013 05:45

Thx it is exactly what I was looking for!

aGerman
Expert
Posts: 4743
Joined: 22 Jan 2010 18:01
Location: Germany

Re: splitting strings

#7 Post by aGerman » 08 Sep 2013 05:47

You're welcome.

npocmaka_
Posts: 517
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: splitting strings

#8 Post by npocmaka_ » 08 Sep 2013 06:56

Code: Select all

@echo off
set "some_dir=C:\somewhere\on\your\disk\test.txt"
set "dir_parts=%some_dir:\=&echo.%"
echo %dir_parts%


:twisted:

Post Reply