I am writing a batch script that copies files from a my windows directory to a WSL distro. Part of this is choosing which distro to copy the files to. If I use the command wsl --list -q
if gives me the following output:
Ubuntu-22.04
Ubuntu-18.04
I am trying to use the FOR loop process described in the answer here to assign the variables the distro names: How to set commands output as a variable in a batch file
However, the first variable is always just the first letter of the first line (U) and the second variable is empty.
This is what I have been trying:
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET count=1
FOR /F "tokens=* USEBACKQ" %%F IN (`wsl --list -q`) DO (
SET "var!count!=%%F"
SET /a count=!count!+1
)
ECHO %var1%
ECHO %var2%
ENDLOCAL
Output:
U
ECHO is off.
I have tried removing USEBACKQ, adding delims=
, and removing tokens=*
. I have also tried using the single variable version in the posted answer. I still only get the U. If I write the wsl --list -q
to a .txt file it writes it properly. I'm trying to avoid writing these to a file just to read them and delete the file. I'm wondering if the wsl --list
command outputs differently than other commands. The variables properly populated if I used a different command, such as dir
.