Output command output to a variable

The directory with the following files is specified

image1.txt
image2.txt
image3.txt

I want to get the oldest file (let the files be sorted by data, first the old date):

dir /b /od c:\test\image?.txt | findstr ^1

This works great when entering manually in cmd.exe. Now (in a script package) I want to put the output of this command in a variable. How can i do this? Thank!

Update: I wonder if there is a direct path without using a loop?

+1
source share
4 answers
For /F %%A in ('"dir /b /od C:\test\image*.txt|findstr ^1"') do set myVar=%%A

You can do it through a For loop, try this on the command line, I just tested it and it works fine

Conclusion:

set myVar=image1.txt

When you execute the Set command on the command line, you can see:

myVar=image1.txt
NUMBER_OF_PROCESSORS=2
+2
source

, FOR-Loop - /p .

dir /b /od c:\test\image?.txt | findstr ^1 > oldest.tmp
< oldest.tmp set /p myVar=
+1

Example:

wmic path Win32_VideoController get CurrentHorizontalResolution | FINDSTR [0-9] > X.txt 'Output in a file
wmic path Win32_VideoController get CurrentVerticalResolution | FINDSTR [0-9] > Y.txt
wmic path Win32_VideoController get CurrentRefreshRate | FINDSTR [0-9] > Hz.txt
wmic path Win32_VideoController get CurrentBitsPerPixel | FINDSTR [0-9] > Bits.txt
set /p X= < X.txt 'Input from a file
set /p Y= < Y.txt
set /p Hz= < Hz.txt
set /p Bits= < Bits.txt
set X=%X: =% 'Remove the spaces
set Y=%Y: =%
set Hz=%Hz: =%
set Bits=%Bits: =%
DEL /q X.txt 'Delete file created
DEL /q Y.txt
DEL /q Hz.txt
DEL /q Bits.txt

Four steps.

0
source

set variableName = dir / b / od C; \ test \ image? .txt | findstr ^ 1

note: this is not verified. Source:

Google

-2
source

All Articles