首页 > 解决方案 > 如何从批处理输出文件中删除额外的文本和空格

问题描述

我正在尝试从工作中的多个设备收集 ICCID。

我能够收集和输出 ICCID,但无法删除 device.txt 内容左侧的所有字符,只留下 15 位数字。

到目前为止,我能够创建一个批处理文件来收集 ICCID 并输出到 device.txt 文件。

@echo off

(
%windir%\sysnative\cmd.exe /c netsh.exe mbn show interfaces | find "Device Id"

) > C:\Users\Test\Pictures\device.txt

这是输出文件中显示的内容:
Device Id: 990001234567890

我需要它只输出 device.txt 文件中的数字: 990001234567890

标签: batch-file

解决方案


您应该能够netsh.exe直接执行 - 不需要cmd.exe /c.

用于FOR /F解析命令的输出,如下所示:

for /f "delims=: tokens=2" %%A in (
  'netsh.exe mbn show interfaces ^| find "Device Id"'
) do >"C:\Users\Test\Pictures\device.txt" echo %%A

delims=:指定在每个:. tokens=2指定获取第二个令牌。

输出将有前导空格。为了消除空格,加入一个额外简单的 FOR 循环:

for /f "delims=: tokens=2" %%A in (
  'netsh.exe mbn show interfaces ^| find "Device Id"'
) do for %%B in (%%A) do >"C:\Users\Test\Pictures\device.txt" echo %%B

推荐阅读