简体   繁体   中英

How can i capture data using the for command from a text file and set it as a variable (windows command prompt)

Situation

Using Diskpart I can get DETAIL DISK info which looks like this:

Microsoft DiskPart version 10.0.17763.1

Copyright (C) Microsoft Corporation.
On computer: PC

Disk 1 is now the selected disk.

Samsung SSD 960 EVO 500GB
Disk ID: {00000}
Type   : NVMe
Status : Online
Path   : 0
Target : 0
LUN ID : 0
Location Path : PCIROOT(0)#PCI(1D00)#PCI(0000)#NVME(P00T00L00)
Current Read-only State : No
Read-only  : No
Boot Disk  : Yes
Pagefile Disk  : Yes
Hibernation File Disk  : No
Crashdump Disk  : Yes
Clustered Disk  : No

  Volume ###  Ltr  Label        Fs     Type        Size     Status     Info
  ----------  ---  -----------  -----  ----------  -------  ---------  --------

Leaving DiskPart...

The text i want to capture is "Samsung SSD 960 EVO 500GB" Obviously, when i run this on a system with a different drive, this info will change, so it could be a Western Digital or Seagate drive etc instead.

I was thinking of using:

for /f "skip=7 delims=" %g in (disk1.txt) do set disk1=%g

However, this captures everything below and the final bit of data is stored ("Leaving DiskPart...") as the variable. Not the first value. How can i fix this? There must be something simple i haven't thought of.

If you are dead set on running this from the command line, I think this would be your only option.

set "disk1=" & for /f "skip=7 delims=" %g in (disk1.txt) do IF NOT DEFINED disk1 set "disk1=%g"

But since you tagged this question with Batch File then you could use a GOTO command.

for /f "skip=7 delims=" %%g in (disk1.txt) do (set "disk1=%%g" & goto done)
:done

Would you prefer to go directly to the source and skip the text processing?

FOR /F "delims=" %%s IN ('powershell -NoLogo -NoProfile -Command "(Get-CimInstance -ClassName CIM_DiskDrive).Model"') DO (SET "DISK1=%%s")

Good idea, @LotPings.

FOR /F "delims=" %%s IN ('powershell -NoLogo -NoProfile -Command "(Get-Disk).FriendlyName"') DO (SET "DISK1=%%s")

Given that this may return an array, choose which one by using the -Number parameter.

FOR /F "delims=" %%s IN ('powershell -NoLogo -NoProfile -Command "(Get-Disk -Number 0).FriendlyName"') DO (SET "DISK1=%%s")

This is even easier once you start writing PowerShell scripts instead of .bat files. PowerShell is clearly the course Microsoft has set.

$disk1 = (Get-Disk -Number 0).FriendlyName

You could also utilise WMIC to do this, which unlike the DiskPart method does not require to be Run as administrator and does not require the output to be written to a file and read back in to be parsed :

@For /F "Skip=1Delims=" %%A In ('WMIC DiskDrive Where "Index=1" Get Model')Do @For /F Tokens^=* %%B In ("%%A")Do @Set "_=%%B"

To check if it has worked, add the following line below it:

@Set _&Pause

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM