I was wondering if SET can be filled by the output of a command?
I tried this, without success:
Code: Select all
SET count=myapp.exe input.pdf
echo %count
Moderator: DosItHelp
Code: Select all
SET count=myapp.exe input.pdf
echo %count
Code: Select all
for /f "tokens=* delims=" %%# in ('myapp.exe input.pdf') do set "count=%%#"
echo %count%
Code: Select all
>temp.txt echo Example command output
<temp.txt set /p var=
del temp.txt
echo var=%var%
Code: Select all
echo This does NOT work|set /p var=
Code: Select all
myapp.exe file1.png file2.png etc.
Code: Select all
FOR /F "delims=" %%a IN ('dir *.png') DO set "name=%%a" & echo %name%
Code: Select all
setlocal enabledelayedexpansion
for %%x in (%__CD__%*) do set LIST=!LIST! "%%x"
echo LIST = %LIST%
Code: Select all
for %%x in (%__dir__%*) do set LIST=!LIST! "%%x"
Code: Select all
for %%x in (%__dir *.pdf__%*) do set LIST=!LIST! "%%x"
Code: Select all
for %%# in (*.pdf) do echo %%# >> list.txt
Code: Select all
SETLOCAL EnableDelayedExpansion
cd /D %~dp0
set _filelist=
for /f "delims=|" %%f in ('dir /b *.pdf') do (
set "_filelist=!_filelist!%%f "
)
set _filelist=%_filelist:,,=%
echo %_filelist%
Isn't the newly discovered way in use?dbenham wrote: ↑29 Apr 2020 11:17Yes, FOR /F is generally how it is done.
The only other option is to redirect the output of your command to a temp file, and then read the result with SET /P using redirected input.You might think you could pipe the output of your command into SET /P like so:Code: Select all
>temp.txt echo Example command output <temp.txt set /p var= del temp.txt echo var=%var%
But the pipe doesn't do any good because each side of the pipe is executed in its own temporary cmd.exe process. So although the variable gets set in the child process, the variable disappears as soon as the pipe terminates - there is no way to bring the value back to the parent batch process.Code: Select all
echo This does NOT work|set /p var=
Dave Benham
Code: Select all
cmd /c "exit 0" 0>&3 4>&3 6>&1 | break
break 0>&6 3>&6
:# This leaves behind: &0=Orig&0, &1=Orig&1, &2=Orig&2, &3=PipeIn, &4=PipeOut, &6=Orig&0
>&4 echo From another child &:# Write to pipe
<&3 set /p "READ=" &:# Read value from pipe
echo Read on pipe: %READ%