Ajay Asthana
Ajay Asthana

Reputation: 19

how to add space in for /f "tokens=*"

this is my myfile.txt I want to add space in second column as see the sample

ARK,LAR    SNE,QNE,898,ILO,SNE,SNE,LAR,LAR,545
AUS,MNY    P08,TTL,7776,STO,STL,STL,MNY,MNY,567
BOS,MTZ    TNK,SDK,444,PPO,TNK,TNK,MTZ,MTZ,456

this is the code I am using

for /f  "tokens=* " %%i in (myfile.txt) do call :echo2 %%i %%J %%K %%L %%M %%N %%O %%P %%Q %%R %%S
goto :EOF
:echo2
echo insert ('%1','%2','%3','%4','%5','%6','%7','%8','%9','%10'); >>myfile1.txt
goto :EOF

its displaying results , where it should have taken space what I am missing any help is appreciated

Upvotes: 1

Views: 842

Answers (4)

pathe3
pathe3

Reputation: 348

You can't exceed 9 variables so your script won't work after the 9th. You can use for /f to copy each line exactly as the original file like so:

for /f  "tokens=* " %%i in (myfile.txt) do echo %%i >>myfile1.txt
goto :EOF

Upvotes: 0

Magoo
Magoo

Reputation: 80113

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
(
FOR /f "delims=" %%i IN (myfile.txt) DO (
 SET "dataline=%%i"
 SET "outline="
 CALL :adddata
)
)>myfile1.txt
GOTO :EOF

:adddata
FOR /f "tokens=1*delims=," %%p IN ("%dataline%"
    ) DO SET outline=%outline%'%%p',&SET "dataline=%%q"
IF DEFINED dataline GOTO adddata
ECHO insert (%outline:~0,-1%);
GOTO :eof

This should do the job with no practical limit on columns - provided of course that the comma is reliably an end-of-column delimiter.

  • For each line in the source file, assign the entire line to dataline and clear outline
  • then take the first token, delimited by comma, from dataline, quote it,add a comma and append it to outline; then set dataline to the remainder of the line after the first comma.
  • repeat until there is nothing left in dataline
  • output the text insert ( + all but the last character of outline (which will be a comma) + );

Upvotes: 1

Endoro
Endoro

Reputation: 37589

Try this:

@echo off & setlocal
(for /f  "delims=" %%i in (myfile.txt) do (
    set "line='%%i'"
    setlocal enabledelayedexpansion
    set "line=!line:,=','!"
    set "line=!line:    =    ','!"
    echo(insert (!line!^);
    endlocal
))>myfile1.txt

Upvotes: 0

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200373

If I understand you correctly, you want to preserve the spaces in the text between the 1st and 2nd comma, correct? Try this:

@echo off

for /f "tokens=1-10 delims=," %%a in (myfile.txt) do (
  >>myfile1.txt echo.insert ('%%a','%%b','%%c','%%d','%%e','%%f','%%g','%%h','%%i','%%j'^);
)

Upvotes: 0

Related Questions