Question

The task is to iterate through each line in a file named alts.txt. Then I grab the line and split it at the semicolon and print out the text before the semicolon and after the semicolon.

My file looks something like this...

username:password
username2:password2
username3:

My current code is this:

setlocal ENABLEDELAYEDEXPANSION
set file=alts.txt
for /f "tokens=*" %%A in (%file%) do (
    set str=%%A
    set "username=%str::="^&REM #%
    set "pass=%str:*:=%"
    echo username=%username% pass=%pass%
)
pause

If someone would be kind enough to show me my error and EXACTLY how to fix the error it would be greatly appreciated.

Was it helpful?

Solution

@echo off
setlocal ENABLEDELAYEDEXPANSION
set file=alts.txt
for /f "tokens=1,2 delims=:" %%A in (%file%) do (
    set "$user=%%A"
    set "$pass=%%B"
    echo username=!$user! pass=!$pass!
)
pause

Be careful using %username%. It's a system variable. You can test writing echo %username% in the CMD prompt. You better choose another name for the Variable like i did.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top