Can SET be filled by a command?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Shohreh
Posts: 13
Joined: 26 Feb 2020 08:05

Can SET be filled by a command?

#1 Post by Shohreh » 29 Apr 2020 06:46

Hello,

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
Thank you.
Last edited by Shohreh on 29 Apr 2020 14:06, edited 1 time in total.

npocmaka_
Posts: 493
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: Can SET be filled by a command?

#2 Post by npocmaka_ » 29 Apr 2020 07:41

the common way is to use for /f:

Code: Select all

for /f "tokens=* delims=" %%# in ('myapp.exe input.pdf') do set "count=%%#"
echo %count%

dbenham
Expert
Posts: 2369
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Can SET be filled by a command?

#3 Post by dbenham » 29 Apr 2020 11:17

Yes, 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.

Code: Select all

>temp.txt echo Example command output
<temp.txt set /p var=
del temp.txt
echo var=%var%
You might think you could pipe the output of your command into SET /P like so:

Code: Select all

echo This does NOT work|set /p 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.


Dave Benham

Shohreh
Posts: 13
Joined: 26 Feb 2020 08:05

Re: Can SET be filled by a command?

#4 Post by Shohreh » 29 Apr 2020 14:05

Thank you!

Shohreh
Posts: 13
Joined: 26 Feb 2020 08:05

Re: Can SET be filled by a command?

#5 Post by Shohreh » 29 Apr 2020 15:01

Actually, in the same vein, I also need to call a command that expects a list of files at the end:

Code: Select all

myapp.exe file1.png file2.png etc.
I tried the following, but it's not obviously full of data I don't need.

Code: Select all

FOR /F "delims=" %%a IN ('dir *.png') DO set "name=%%a" & echo %name%
What's a good way to build a list of files?

Eureka!
Posts: 89
Joined: 25 Jul 2019 18:25

Re: Can SET be filled by a command?

#6 Post by Eureka! » 29 Apr 2020 19:04

Something like this? (not tetsed)

Code: Select all

setlocal enabledelayedexpansion
for %%x in (%__CD__%*) do set LIST=!LIST! "%%x"

echo LIST = %LIST%

Shohreh
Posts: 13
Joined: 26 Feb 2020 08:05

Re: Can SET be filled by a command?

#7 Post by Shohreh » 30 Apr 2020 08:36

Thanks, it worked.

But it displays 1) the fully qualified filenames (I don't need the path) and 2) it starts by displaying "!LIST! "%__DIR"".

The following gets rid of the path:

Code: Select all

for %%x in (%__dir__%*) do set LIST=!LIST! "%%x"
The following doesn't work, although I need to filter files:

Code: Select all

for %%x in (%__dir *.pdf__%*) do set LIST=!LIST! "%%x"
Do I need to use a for loop, append the output to a text file, and use findstr to read them out?

Code: Select all

for %%# in (*.pdf) do echo %%# >> list.txt
----
Edit: Found this elsewhere:

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%

Post Reply