0

I have this really nice line in my batch file that tells me how many lines are in a file:

 find /v /c "" C:\Users\c1921\mypath\myfolder\!$Unit!.txt

This is nice and gives me 31 for the particular file I'm working with. My problem is the file looks something like this:

 Microsoft (R) Windows Script Host Version 5.8
 Copyright (C) Microsoft Corporation. All rights reserved.

 my_handled
 219278
 check
 219276
 control
 219274

I want to be able to skip the first three lines entirely and then save the first value and then use the second value in my next command etc.

How do I save the number (e.g. 31) into a variable in my batch file?

On a hunch I tried setting a variable like so but it wasn't effective:

set "$testVar="""
echo !$testVar! 

2 Answers 2

2

This command allows you to "save the number (e.g. 31) into a variable in my batch file":

for /F %%a in ('find /v /c "" ^< C:\Users\c1921\mypath\myfolder\!$Unit!.txt') do set numLines=%%a

This command allows you "to skip the first three lines entirely" and process the rest:

for /F "skip=3 delims=" %%a in (C:\Users\c1921\mypath\myfolder\!$Unit!.txt) do echo Processing: "%%a"

However, in my opinion this problem could be entirely avoided if the three first lines in the text file are supressed from the very beginning. I think this file is generated via a VBScript of JScript program that is executed this way:

cscript progname.vbs > C:\Users\c1921\mypath\myfolder\!$Unit!.txt

The first three lines in the text file may be avoided adding //nologo switch this way:

cscript //nologo progname.vbs > C:\Users\c1921\mypath\myfolder\!$Unit!.txt
Sign up to request clarification or add additional context in comments.

1 Comment

I've never heard of //nologo before that solved that problem. I was going to count the lines and then just skip the first three and then starting on the fourth line, but this works so much better thank you.
1
@ECHO OFF
SETLOCAL
SET /a count=3
SET "first="
FOR /f "skip=3delims=" %%a IN (q25089468.txt) DO (
 IF DEFINED first (CALL :show %%a) ELSE (SET "first=%%a")
)

ECHO count=%count%

GOTO :EOF

:show
ECHO first=%first% second=%1
SET /a count+=2
SET "first="
GOTO :eof

I used a file named q25089468.txt containing your data for my testing.

You appear to be asking two entirely different things - how to count the lines and how to skip the first 3, then deliver each succeeding pair to another process.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.