简体   繁体   English

如何使用for命令从文本文件捕获数据并将其设置为变量(Windows命令提示符)

[英]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: 使用Diskpart,我可以获得详细的磁盘信息,如下所示:

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. 我要捕获的文本是“ Samsung SSD 960 EVO 500GB”。显然,当我在具有不同驱动器的系统上运行此信息时,此信息将更改,因此它可能是Western Digital或Seagate驱动器等。

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. 但是,这捕获了下面的所有内容,最后的数据位(“ Leave DiskPart ...”)作为变量存储。 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. 但是,由于您用批处理文件标记了此问题,因此可以使用GOTO命令。

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. 好主意,@ 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. 鉴于这可能返回一个数组,请使用-Number参数选择哪个数组。

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脚本而不是.bat文件,这将变得更加容易。 PowerShell is clearly the course Microsoft has set. 显然,PowerShell是Microsoft设置的过程。

$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 : 您还可以利用WMIC来执行此操作,与DiskPart方法不同,该方法不需要以管理员身份运行,也不需要将输出写入文件并重新读回以进行解析

@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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 windows 命令提示符中设置具有多行文件内容的环境变量 - How to set an env variable with multiline file content in windows command prompt 如何从.bat文件在命令提示符下运行此命令? - How can I run this command in command prompt from a .bat file? 如何随时捕获Windows中命令提示符的显示输出? - How to capture display output from a command prompt in Windows at any moment? Windows命令提示符批处理文件-未设置变量 - Windows Command Prompt Batch File - Variable Not Getting Set 在Windows命令提示符下使用命令写文件? - writing in a file using command from windows command prompt? 如何使用set命令将json文件传递到命令行变量 - how can I pass a json file into a command line variable using the set command 在Windows命令提示符下从文本文件执行命令 - Execute commands on windows command prompt from a text file Windows命令提示符延迟打印文本文件 - Windows Command Prompt print text file with delay 获取 Windows 命令提示符内容到文本文件 - Getting a Windows command prompt contents to a text file 如何在 Windows 命令提示符下从批处理文件执行多个命令,同时保持命令提示符上下文打开? - How to execute multiple commands from a batch file, in windows command prompt, while keeping the command prompt context open?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM