0

Here is a for loop to save each line of a text file to its own variable:

@echo off 
setlocal enableextensions enableDelayedExpansion 

set count=0
for /f "tokens=*" %%a in (file.txt) do (
    set /a count=!count! + 1
    set var_!count!=%%a
)

endlocal

I would like to save the content of each variable to a separate new text file. How can I do this? I tried the following. But it does not work, because !var_!count!! is a variable inside a variable.

@echo off 
setlocal enableextensions enableDelayedExpansion 

set count=0
for /f "tokens=*" %%a in (file.txt) do (
    set /a count=!count! + 1
    set var_!count!=%%a
    echo !var_!count!!>file_!count!.txt
)

endlocal

1 Answer 1

2
@echo off 
setlocal enableextensions enableDelayedExpansion 

set count=0
for /f "tokens=*" %%a in (file.txt) do (
    set /a "count+=1"
    set "var_!count!=%%a"
    for %%b in (!count!) do >file_!count!.txt echo !var_%%b!
)

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

1 Comment

that was fast! thanks a lot, works great! what a funny workaround :)

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.