Otiel
Otiel

Reputation: 18743

Save command output to variable

In a batch file, I am trying to fetch the output of a command and save it to a variable.

The goal of my command is to count the number of folders in a certain folder.

So I tried to do the following:

Am I on the right path to retrieve the output in a variable? What should I do to resolve the error message?
Or maybe there is a simpler way to set a command output in a variable?

Upvotes: 1

Views: 1904

Answers (1)

Joey
Joey

Reputation: 354864

You can use the answer you first mentioned. You don't have to cd there, but you can use pushd which will allocate a temporary drive letter for UNC paths which will be released when you do a popd.

So in essence:

pushd \\server\path
set count=0
for /d %%x in (*) do set /a count+=1
popd

Upvotes: 4

Related Questions