4

I have this following code.

@echo off
setlocal EnableDelayedExpansion
set holdingline=,Measure,,+ X,,,0,0
FOR /F "tokens=1,2,3,4,5,6,7,8 delims=," %%a IN ("%holdingline%") DO (
    echo %%a
    echo %%b
    echo %%c
    echo %%d
    echo %%e
    echo %%f
    echo %%g
    echo %%h
    echo %holdingline%
)
pause

Output displayed is as below:

Measure
+ X
0
0
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
,Measure,,+ X,,,0,0

The empty strings are pushed to the end and I wonder why. I am expecting them in order, say something like:

ECHO is off.
Measure
ECHO is off.
+ X
ECHO is off.
ECHO is off.
0
0
,Measure,,+ X,,,0,0

This would enable me to assign them to the correct variables. I tried searching but did not find much help.

2 Answers 2

2
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
set holdingline=,Measure,,+ X,,,0,0
FOR /F "tokens=1-8 delims=," %%a IN (""%holdingline:,=","%"") DO (
    ECHO(%%~a
    ECHO(%%~b
    ECHO(%%~c
    ECHO(%%~d
    ECHO(%%~e
    ECHO(%%~f
    ECHO(%%~g
    ECHO(%%~h
    echo %holdingline%
)

GOTO :EOF

This should solve your problem


To process a file, producing a new file

@ECHO OFF
SETLOCAL
(
FOR /f "delims=" %%x IN (q29639243.txt) DO (
 set "holdingline=%%x"
 CALL :process
)
)>u:\new.txt

GOTO :EOF

:process
FOR /F "tokens=1-8 delims=," %%a IN (""%holdingline:,=","%"") DO (
    ECHO(%%~a
    ECHO(%%~b
    ECHO(%%~c
    ECHO(%%~d
    ECHO(%%~e
    ECHO(%%~f
    ECHO(%%~g
    ECHO(%%~h
    echo %holdingline%
)

GOTO :EOF

I used a file named q29639243.txt containing similar data for my testing. Produces u:\new.txt

Sign up to request clarification or add additional context in comments.

1 Comment

Can the same be done using a file. I mean instead of using set command to enter value holdingline is there a way to read the file record into holdingline variable and do the same task?
2

This should do what you want it to:

@echo off
setlocal EnableDelayedExpansion
set holdingline=,Measure,,+ X,,,0,0
FOR /F "tokens=1,2,3,4,5,6,7,8 delims=," %%a IN ("%holdingline:,,=, ,%") DO (
    echo %%a
    echo %%b
    echo %%c
    echo %%d
    echo %%e
    echo %%f
    echo %%g
    echo %%h
    echo %holdingline%
)
pause

Replacing ,, with , , will cause it to set the middle variables to (space) and hence treat them like nothing. If you don't have anything between two , it will skip them and hence only 4 variables exist, leaving e-h blank

1 Comment

Thanks. Your solution gets me closer. But it still doesn't treat the first null at the begining of the string and the 2 consecutive nulls as expected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.