Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch - Write output of DIR to a variable

I have to store the ouput of DIR in a variable.

This was asked before e.g. here or here or here.

They all provide an answer more or less similar to what I'm using right now:

%= Find the *.sln file and write its path to the file temp.temp =%
DIR /s/b *.sln > temp.temp

%= Read out the content of temp.temp again to a variable =%
SET /p texte=< temp.temp

%= Remove the temp.temp file =%
DEL temp.temp

%= Get only the filename without path =%
FOR %%A IN ("%texte%") DO (
    SET Name=%%~nxA
)

But in my case I'm sure that the ouput of DIR /s/b *.sln will allways be a one-liner. To me it looks a bit ugly to have to
a) store the ouput in an external file and
b) run a FOR loop over it though I already know it will only have one line.

Is there any direct/simpler way to do this?

like image 964
derHugo Avatar asked Nov 23 '17 08:11

derHugo


People also ask

How do I write the output of a batch file?

Output: The > operator is used to overwrite any file that already exists with new content. The >> operator is used to append to the text file (add to), instead of overwriting it.

What does %1 mean in a batch file?

When used in a command line, script, or batch file, %1 is used to represent a variable or matched string. For example, in a Microsoft batch file, %1 can print what is entered after the batch file name.

What does && do in batch file?

&& runs the second command on the line when the first command comes back successfully (i.e. errorlevel == 0 ). The opposite of && is || , which runs the second command when the first command is unsuccessful (i.e. errorlevel != 0 ).

How to store result of command direct into a variable?

If you don't want to output to a temp file and then read into a variable, this code stores result of command direct into a variable: If you want to store this code in a batch file, add an extra % symbol: A useful example to count the number of files in a directory & store in a variable: (illustrates piping)

Where does batch CMD store the output of a command?

Notes 'bout the code: the output from the command executed via call :EXEC command is stored in %output%. Batch cmd doesn't cares about scopes so %output% will be also available in the main script. the @ the beginning is just decoration and there to suppress echoing the command line.

What is the difference between% and% in a batch file?

The only difference between the two answers above is that on the command line, you use a single % in your variable. In a batch file, you have to double up on the percentage signs (%%).

Is there a way to redirect the output of a command?

Boils down to: You can also redirect the output of a command to a temporary file, and then put the contents of that temporary file into your variable, likesuchashereby. It doesn't work with multiline input though.


1 Answers

for /f "delims=" %%a in ('dir /s /b *.sln') do set "name=%%a"

indeed is the most efficient method (you can process the output of a command directly, no need for a temporary file).
%name% will contain the full qualified file name of your file (or the last of the files, if there are more than one)

like image 96
Stephan Avatar answered Sep 20 '22 10:09

Stephan