Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hello,
I'm trying to parce a file with 100s of "tokens" separated by ";" The current format is somthing like the following
Data One; Data Two; Data Three;I need to extract the lines and remove the ";" while putting them in a vertical list like this
Data One
Data Two
Data ThreeI'm coding the following:
For /F "tokens=* delims=;" %i IN (Myfile.txt) DO (echo %i >> NewFile.txt)
This writes the new file but all it pretty much does is copy everything over the NewFile.txt. I want to be able to remove the quotes and insert a break line after each Data Strint.
Many Thanks.
@echo off > NewFile.txt
for /F "delims=" %%i in (MyFile.txt) do (
for %%j in (%%i) do echo.%%j>> NewFile.txt
)
:: [End_Of_Batch]
Warning: the above script works if the strings delimited by ";" do not have embedded blanks, otherwise the code needs to be slightly modified replacing blanks with a char that never exists in the string (select that, please)
Report Offensive Follow Up For Removal
Thank you very much IVO.
This works great but your warning came true. The strings do have a blank space between them, i'm trying to extract names out of the files so they are like this John Doe; Jane Doe; Peter Paul; etc.
Right now your code produces the following in the outpout
I'm actually trying to getJohn Doe
Jane Doe
Peter PaulAlso this code blows my mind 'casue i've never seen nested for commands. I can do a simple for but much of what your code I can't "translate" to english. I'm going to attempt that below and would you please correct me wherever possible.
Your Code:
@echo off > NewNamesFile.txt --> Create new file at the begining? is it not the same to just redirect it at the end? is it just a preference?for /F "delims; =" %%i in (NamesFile.txt) -->You are not specifing tokens but you're saying delims= or basically default space and tab. I experimented by putting a space and all I get out is the same as the original file
for %%j in (%%i) do echo.%%j>> NewNamesFile.txt)--> Here is wher everything just goes bananas for me so anything you can do to tell me what you're doing on this one is appreciated.
Thank you very much again.
Report Offensive Follow Up For Removal
Ola Jimenez,
later I will explain my batch line by line, for now the following script should work for yoir job,
@echo off & setlocal EnableDelayedExpansion > NewFile.txt
for /F "delims=" %%i in (MyFile.txt) do (
set line=%%i
set line=!line: =?!
for %%j in (!line!) do (
set row=%%j
set row=!row:?= !
echo.!row!>> NewFile.txt
)
)
:: [End_Of_Batch]
Report Offensive Follow Up For Removal
Ola Jimenez,
I'm back at the end of a busy day to answer your questions and give you the script to do your job.
First of all here the correct version of the code in the previous post as that was written on the fly and has some bugs.
@echo off & setlocal EnableDelayedExpansion > NewFile.txt
for /F "delims=" %%i in (MyFile.txt) do (
set line=%%i
set line=!line: =#!
for %%j in (!line!) do (
set row=%%j
set row=!row:#= !
if "!row:~0,1!"==" " (echo !row:~1!>> NewFile.txt
) else (
echo !row!>> NewFile.txt
)
)
)
:: [End_Of_Batch]
Now let us examine it step by step
@echo off & setlocal EnableDelayedExpansion > NewFile.txtSets off the echoing of commands as usual for any well behaved script and enables the delayed expansion of environment variables, a facility needed to set and then refer variables inside FOR loops. Otherwise variables are frozen at their value outside the loop preventing their dynamical usage. Dynamic variables are marked by "!" instead of the conventional "%".
The output of SETLOCAL is redirected to the output file to create an empty file to be populated later by adding lines via ">>". That is a safety rule in case of running the script multiple times. A more usual format is@echo off
setlocal enabledelayedexpansion
type nul>newfile.txtfor /F "delims=" %%i in (MyFile.txt) do (
just reads each line of the input file as is; one token (omitted) and no delimiters as the option "delims=" means to discard even the default one (space).
set line=%%i
set line=!line: =#!assigns each line from the input file to the dynamic variable "line" and replaces spaces with # i.e. a character unlucky to be found in names (so John Doe becomes John#Doe).
for %%j in (!line!) do (
uses a nested plain (no switches) FOR to parse the line. A string used as clause in a plain FOR is processed item by item where separators are the usual command tail separators i.e. "space", ",", ";". Here the reason to replace spaces with #.
set row=%%j
set row=!row:#= !the opposite of the previous process to restore the original item before output.
if "!row:~0,1!"==" " (echo !row:~1!
removes the space at the beginning of the item if that exists to normalize the name.
What stated above is just to outline the meaning of the script. In the code you find nested FOR (why not?), dynamic variables, plain FOR to parse an unknown sequence of tokens (how many?), substring manipulation and IF (then) ELSE statement.
To know more about the issues outlined type
Set /?
For /?
If /?NT batch scripting offers amazing moments if you can touch it lovely.
Report Offensive Follow Up For Removal
![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |