JustAGuy
JustAGuy

Reputation: 23

I want to run more than 1 command with the FOR /F

I'm trying to make a script to: send a request to an url, then append (>>) the %%a variable i set in the FOR /F command IF i get a certain response from the command i run with FOR /F. I tried with 2 scripts that are the followings:

FOR /F %%a in (usernames.txt) do (
IF "curl -k -d name=%%a https://club.pokemon.com/api/signup/verify-username" EQU "{"valid":true,"suggestions":[],"inuse":false}" %%a >> usernames1.txt

and

set /p VAR = < tmpFile
FOR /F %%a in (usernames.txt) do (
curl -k -d name=%%a https://club.pokemon.com/api/signup/verify-username 
> tmpFile
IF VAR={"valid":true,"suggestions":[],"inuse":false}
%%a >> usernames1.txt)

EDIT: good enough with that script, thanks guys. but i've got another thing: can i add more than 1 variable to the script? I mean a variable like %%a, that takes every line from another txt file

Upvotes: 1

Views: 95

Answers (1)

Jean-Fran&#231;ois Fabre
Jean-Fran&#231;ois Fabre

Reputation: 140168

First, you set VAR once, whereas your temporary file does not exist. Then you test with = instead of == and without the ! chars.

And don't put too many spaces like if you were using a real shell like bash :)

As Magoo noted, I also had to fix set /p VAR = < tmpFile to remove the extra spaces. Batch has a tendency to take them literaly.

(another example: echo foo > file: file now contains "foo ".

(needless to say that without enabledelayedexpansion it wouldn't work either because lots of things happen inside the FOR loop)

The fixed code (also had to protect the test string with extra quotes or it wouldn't work):

setlocal enabledelayedexpansion

del usernames1.txt >NUL 2>NUL
FOR /F %%a in (usernames.txt) do (
curl -k -d name=%%a https://club.pokemon.com/api/signup/verify-username > tmpFile 2>NUL
set /p VAR=<tmpFile
IF "!VAR!"=="{"valid":true,"suggestions":[],"inuse":false}" echo %%a >> usernames1.txt
)

I tested it with a random list of user names and changing "valid":true by "valid":false and the names were issued.

Upvotes: 4

Related Questions