Extract specific line from text script

2019-09-11 07:22发布

I am trying to get a script working to extract a specific line out of multiple text files at once. I have the script that will extract the line out of the text but I cannot get it to do this to multiple files.

What I have is an inventory file for each computer at my facility. I'm trying to extract like 16 which has the user's name so each file will have a different value on line 16 but it's always line 16.

Example file I'm trying to extract from:

<HTML>
<BODY>
<center>
<table border=1>
<tr>
    <th>Full Name</th>
    <th>UserID</th>
    <th>IP Address</th>
    <th>Make</th>
    <th>Model</th>
    <th>Serial Number</th>
    <th>Default Printer</th>
</tr>
<tr>
<td>John Doe</td>
<td>USERNAME</td>
<td> 1. 1. 1.1
</td>
<td>Hewlett-Packard
</td>
<td>HP Compaq 8000 Elite SFF PC
</td>
<td>SERIALNUMBERX
</td>
<td>Microsoft XPS Document Writer
</td>
</tr>
</table></font>
<br />
Inventory taken on 2015/11/24 at 07:43:14. </center>
</BODY>
</HTML>

I'm wanting the script to extract the information to a text file so I can put it into a spreadsheet basically.

Here's my script so far. (The toop part I believe is the issue - the FOR command specifically) Sorry for my scripting ignorance. Trying to learn as I go.

@echo off &setlocal
setlocal EnableDelayedExpansion
set filename=\\computername\c$\users\username\desktop\pcs.txt
for /F "tokens=*" %%a in ('type %filename%') do (
set file=%%a.html
)
set /a from=16
set /a till=16

for /f %%i in ('type "%file"^|find /v /c ""') do if %till% gtr %%i set /a till=%%i
set /a skip=from-1

setlocal EnableDelayedExpansion
<"!file!" (
for /l %%i in (1 1 %skip%) do set /p "="
for /l %%i in (%from% 1 %till%) do (
set "ln="
set /p "ln="
echo(!ln! >> c:\users\username\desktop\extracted.txt
 )
)
endlocal
pause

My pcs.txt file is setup like this:

pc1
pc2
pc3

2条回答
看我几分像从前
2楼-- · 2019-09-11 07:49

I ended up using psexec to run the file on each computer instead.

@echo off &setlocal
setlocal EnableDelayedExpansion
set "file=\\server\public\is\kixtartlogs\%computername%.html"
set /a from=16
set /a till=16

for /f %%i in ('type "%file%"^|find /v /c ""') do if %till% gtr %%i set /a till=%%i
set /a skip=from-1

setlocal EnableDelayedExpansion
<"!file!" (
for /l %%i in (1 1 %skip%) do set /p "="
for /l %%i in (%from% 1 %till%) do (
set "ln="
set /p "ln="
echo(%computername% !ln! >> \\computername\c$\users\username\desktop\extracted.txt
)
)
endlocal

PSEXEC command: psexec @\\server\c$\pcs.txt -u domain\username -p password cmd /c \\server\c$\extract.bat

查看更多
干净又极端
3楼-- · 2019-09-11 08:10

Always line 16?

@echo off
setlocal 
set count=0
for %%a in (*.txt) do call :getname %%a
set name
goto :eof

:getname
set /a count+=1
for /f "tokens=2 delims=<>" %%i in ('more +15 %1') do (set "name%count%=%%i" & goto :eof)
查看更多
登录 后发表回答