Jeff B
Jeff B

Reputation: 3

How to remove additional text and spaces from batch output file

I'm trying to collect the ICCID from multiple devices at my work.

I'm able to collect and output the ICCID but not able to remove all character to the left of the device.txt contents, leaving only the 15 digit number.

So far I was able to create a batch file to collect the ICCID and output to device.txt file.

@echo off

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

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

This is what shows in the output file:
Device Id: 990001234567890

I need it to only output the number in the device.txt file: 990001234567890

Upvotes: 0

Views: 469

Answers (1)

dbenham
dbenham

Reputation: 130819

You should be able to execute netsh.exe directly - no need for cmd.exe /c.

Use FOR /F to parse the output of the command as follows:

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=: specifies to break the line into tokens at every :. The tokens=2 specifies to take the 2nd token.

The output will have leading whitespace. To eliminate the whitespace, throw in an extra simple FOR loop:

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

Upvotes: 1

Related Questions