Put command output into a variable

前端 未结 4 1590
耶瑟儿~
耶瑟儿~ 2021-01-24 03:27

Given a directory with the following files

image1.txt
image2.txt
image3.txt

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

相关标签:
4条回答
  • 2021-01-24 03:40

    There isn't a direct way, the FOR-Loop is one way or the other way is set /p with a temporary file.

    dir /b /od c:\test\image?.txt | findstr ^1 > oldest.tmp
    < oldest.tmp set /p myVar=
    
    0 讨论(0)
  • 2021-01-24 03:42

    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 讨论(0)
  • 2021-01-24 03:58

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

    note: this is untested. Source:

    Google

    0 讨论(0)
  • 2021-01-24 04:00
    For /F %%A in ('"dir /b /od C:\test\image*.txt|findstr ^1"') do set myVar=%%A
    

    You could do it through For loop, try that in command line, I just tested it and it works fine

    Output:

    set myVar=image1.txt
    

    On executing Set on command line you can see:

    myVar=image1.txt
    NUMBER_OF_PROCESSORS=2
    
    0 讨论(0)
提交回复
热议问题