Unable to parse variable length string separated by delimite

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Technext
Posts: 13
Joined: 24 May 2010 07:42

Unable to parse variable length string separated by delimite

#1 Post by Technext » 15 Jun 2010 10:58

Hi,


I have a problem with parsing a string, which consists only of directory path. For ex.

My input string is
---------------------------------
Abc\Program Files\sample\
---------------------------------

My output should be
---------------------------------
Abc//Program Files//sample
---------------------------------

The script should work for input path of any length i.e., it can contain any no. of subdirectories. (For ex., abc\temp\sample\folder\joe\)

I have looked for help in many links but to no avail. Looks like FOR command extracts only one whole line or a string (when we use ‘token’ keyword in FOR syntax) but my problem is that I am not aware of the input path length and hence, the no. of tokens.

My idea was to use \ as a delimiter and then extract each word before and after it (\), and put the words to an output file along with // till we reach the end of the string.

I tried implementing the following but it did not work:
-----------------------------------------------------------
@echo off

FOR /F "delims=\" %%x in (orig.txt) do (
IF NOT %%x == "" echo.%%x//>output.txt
)
-----------------------------------------------------------

The file orig.txt contains only one line i.e, Abc\Program Files\sample\

The output that I get contains only: Abc//
The above output contains blank spaces as well after ‘Abc//’

My desired output should be: Abc//program Files//sample//


Can anyone please help me with this?


Regards,
Technext

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

Re: Unable to parse variable length string separated by deli

#2 Post by aGerman » 15 Jun 2010 11:36

You could simply exchange the back slashes by double slashes:

Code: Select all

>output.txt type nul
for /f "delims=" %%a in (orig.txt) do (
  set "line=%%a"
  >>output.txt call echo.%%line:\=//%%
)


Regards
aGerman

Technext
Posts: 13
Joined: 24 May 2010 07:42

Re: Unable to parse variable length string separated by deli

#3 Post by Technext » 16 Jun 2010 03:18

Thanks a lot aGerman! It was really short & to the point.

I only have one query: Why do we use 'call' in the line below:
>>output.txt call echo.%%line:\=//%%

I know that removing it doesn't work but I don't know the WHY behind it? Can you please guide me?


Thanks again,
Technext

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

Re: Unable to parse variable length string separated by deli

#4 Post by aGerman » 16 Jun 2010 06:36

All 4 lines of the FOR loop are parsed as one command line by cmd.exe. That means into the same command line a regular variable doesn't change.

There are 3 possibilities to handle it:

-1st Use the call trick (with %% instead of %). That's what I used for the example above.

-2nd Use SETLOCAL ENABLEDELAYEDEXPANSION (with ! instead of %).

Code: Select all

setlocal EnableDelayedExpansion
>output.txt type nul
for /f "delims=" %%a in (orig.txt) do (
  set "line=%%a"
  >>output.txt echo.!line:\=//!
)


-3rd Call a subroutine for each line

Code: Select all

>output.txt type nul
for /f "delims=" %%a in (orig.txt) do (
  set "line=%%a"
  call :proc
)
goto :eof

:proc
>>output.txt echo.%line:\=//%
goto :eof


Regards
aGerman

[EDIT: useless "call" removed in the last two examples /]

Technext
Posts: 13
Joined: 24 May 2010 07:42

Re: Unable to parse variable length string separated by deli

#5 Post by Technext » 17 Jun 2010 03:35

Thank You for the detailed explanation aGerman! I also got confused when I saw your earlier reply (unedited one). Thanks for the clarification.


Regards,
Technext

Post Reply