Page 1 of 1

[Question] List for undetermined variables in file.. xP

Posted: 07 Nov 2011 12:40
by Ranguna173
Hi!

Ok, the title is a "little" bit confusing.. (well, so is the question..)

I can't say what my question is, it's better to show you an example:

I have a file named "storebox.txt"
It contains the following lines:
C
M
D
--End of the file (this isn't part of the "storebox.txt")--

Now, there is a program that is always adding line to the storebox..
I need a code that would determinate how many lines there are in the text file and I need that code to copy all of them, I've been using this:

Code: Select all

> storebox.txt (
  set /p line1=
  set /p line2=
  set /p line3=
  )


But has I said, there is a program that adds line to the file, so there are more then 3 line in the file after the program modifies it.

Please help and thanks!

Re: [Question] List for undetermined variables in file.. xP

Posted: 07 Nov 2011 14:42
by Ranguna173
No one?
Please..
I realy need this

Re: [Question] List for undetermined variables in file.. xP

Posted: 07 Nov 2011 15:03
by Ranguna173
Thank you, but after some long hours googleing/googling I found the code, here is the code for those who want it:

Code: Select all

findstr /R /N "^" storebox.txt | find /C ":"


If you want to save the output in a variable then just save it in a file and load it:

Code: Select all

findstr /R /N "^" storebox.txt | find /C ":">count
< count (
  set /p line1=
  )


Now the variable "line1" contains the number of lines.

To copy the lines:

Code: Select all

findstr /R /N "^" storebox.txt | find /C ":">count
< count (
  set /p line1=
  )

if %line1%==3 goto copy3
if %line1%==4 goto copy4
if %line1%==5 goto copy5
if %line1%==6 goto copy6
if %line1%==7 goto copy7
if %line1%==8 goto copy8
if %line1%==9 goto copy9

:copy3
< storebox.txt (
  set /p line1=
  set /p line2=
  set /p line3=
  )

:copy4
< storebox.txt (
  set /p line1=
  set /p line2=
  set /p line3=
  set /p line4=
  )

:copy5
< storebox.txt (
  set /p line1=
  set /p line2=
  set /p line3=
  set /p line4=
  set /p line5=
  )

:copy6
< storebox.txt (
  set /p line1=
  set /p line2=
  set /p line3=
  set /p line4=
  set /p line5=
  set /p line6=
  )

:copy7
< storebox.txt (
  set /p line1=
  set /p line2=
  set /p line3=
  set /p line4=
  set /p line5=
  set /p line6=
  set /p line7=
  )

:copy8
< storebox.txt (
  set /p line1=
  set /p line2=
  set /p line3=
  set /p line4=
  set /p line5=
  set /p line6=
  set /p line7=
  set /p line8=
  )

:copy9
< storebox.txt (
  set /p line1=
  set /p line2=
  set /p line3=
  set /p line4=
  set /p line5=
  set /p line6=
  set /p line7=
  set /p line8=
  set /p line9=
  )

Re: [Question] List for undetermined variables in file.. xP

Posted: 07 Nov 2011 15:34
by aGerman
Try

Code: Select all

@echo off &setlocal
for /f "tokens=2 delims=:" %%i in ('find /c /v "" "storebox.txt"') do (
  for /l %%j in (1 1 %%i) do (
    set /p "line%%j="
  )
)<"storebox.txt"

:: show the variables
set line

pause

But why do you need to store the lines in variables? Doesn't it make more sense to process the lines directly inside of the loop?

Regards
aGerman

Re: [Question] List for undetermined variables in file.. xP

Posted: 07 Nov 2011 15:51
by Ranguna173
aGerman wrote:Try

Code: Select all

@echo off &setlocal
for /f "tokens=2 delims=:" %%i in ('find /c /v "" "storebox.txt"') do (
  for /l %%j in (1 1 %%i) do (
    set /p "line%%j="
  )
)<"storebox.txt"

:: show the variables
set line

pause

But why do you need to store the lines in variables? Doesn't it make more sense to process the lines directly inside of the loop?

Regards
aGerman


Sorry, forgot to mention, I'm trying to not use the "for" command..
But thanks that worked, much smaller than mine, counts and saves...
Thank! I might use that! (in another batch)