DOS FOR loop - Can I use an entire word as a delimiter (multi-character-delimiter)

不问归期 提交于 2019-12-13 01:29:54

问题


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.


回答1:


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!
)


来源:https://stackoverflow.com/questions/11768035/dos-for-loop-can-i-use-an-entire-word-as-a-delimiter-multi-character-delimite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!