DOS FOR循环 - 我可以使用整个单词作为分隔符(多字符分隔符) [英] DOS FOR loop - Can I use an entire word as a delimiter (multi-character-delimiter)

查看:199
本文介绍了DOS FOR循环 - 我可以使用整个单词作为分隔符(多字符分隔符)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 名称SerialNumber 
\\。 \PHYSICALDRIVE1 000000002027612
\\\\\ PHYSICALDRIVE0 000000002027476
\\\.\\PHYSICALDRIVE2 00000000202746E
\\\.\\PHYSICALDRIVE3 00000000202760E


$使用FOR循环在dos中,我需要能够解析出与每个PHYSICALDRIVE条目相关的号码,文件。例如:1,0,2和3)

从我收集的角度来看,delims =一次只能查看一个角色。
因为我不能说delims = PHYSICALDRIVE 并让它作为单个分隔符处理。
任何人都可以给出一个例子,如何解析只字符串的数字?



在重要的情况下正在使用延迟扩展。 / p>

谢谢。

解决方案

我认为任何解决方案都需要先解析出完整的列值,然后使用SET搜索和替换或子串来解析出最后的数字。

我假定Name列的值永远不会有空格。因此,默认的FOR / F分隔符将很容易解析出第一列。

如果驱动器号总是小于10,那么 (yourFileName.txt)do(
setdrive = %% A
setdrive =
pre $ !drive:〜-1!
echo!drive!

否则,如果它可以是10或更大,那么

  for / f %% A(yourFileName.txt)do(
setdrive = %%〜A
setdrive =!drive:* physicaldrive =!
echo!drive!


I have a source file that would look something like this:

Name                SerialNumber          
\\.\PHYSICALDRIVE1  000000002027612  
\\.\PHYSICALDRIVE0  000000002027476 
\\.\PHYSICALDRIVE2  00000000202746E  
\\.\PHYSICALDRIVE3  00000000202760E  

Using FOR loops in dos I need to be able to parse out just the number associated with each PHYSICALDRIVE entry to be used later in the bat file. eg: 1,0,2 and 3)

From what I gather the delims= only looks at one character at a time. Since I can't say delims=PHYSICALDRIVE and have it treat that as a single delimiter. Can anyone give an example on how to parse out only the numbers at the end of the string?

In case it matters Delayed expansion is being used.

Thanks.

解决方案

I think any solution will require first parsing out the full column value, and then using SET search and replace or substring to parse out the number at the end.

I'm assuming the Name column value can never have a space within it. So the default FOR /F delimiters will parse out the 1st column easily enough.

If the drive number will always be less than 10 then

for /f %%A in (yourFileName.txt) do (
  set "drive=%%A"
  set "drive=!drive:~-1!"
  echo !drive!
)

Else if it can be 10 or greater then

for /f %%A in (yourFileName.txt) do (
  set "drive=%%~A"
  set "drive=!drive:*physicaldrive=!"
  echo !drive!
)

这篇关于DOS FOR循环 - 我可以使用整个单词作为分隔符(多字符分隔符)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆