Jump to content

Unable to parse variable length string separated by delimiter


Recommended Posts

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

Link to comment
Share on other sites

I have got the solution. Here it is:

-----------------------------------

type nul>output.txt

FOR /F "usebackq tokens=*" %%x in ("orig.txt") do (

set LINE=%%x

call set LINE=%%LINE:\=//%%

call echo:%%LINE%%

) >output.txt

-----------------------------------

Regards,

Technext

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. Privacy Policy