Computer Hope

Microsoft => Microsoft DOS => Topic started by: Helpmeh on January 17, 2009, 07:16:06 PM

Title: New Line In File
Post by: Helpmeh on January 17, 2009, 07:16:06 PM
OK, I need your help again :-\


In priorities:
1. I'm trying to get the contents, and set each line as a variable.
2. If the linecount exceeds a certain amount (30 for example) delete the first line and move everything up a line.


The FOR command might work here, but I don't know how to set the delimiter as a new line...and the eol thing doesn't do anything either.
Title: Re: New Line In File
Post by: Dias de verano on January 18, 2009, 02:16:13 AM
1. I'm trying to get the contents, and set each line as a variable.

Could you explain this a bit more ... do you mean you want to get each line into a variable, do something with that variable, then move on to the next line and repeat until all lines are done?

Quote
2. If the linecount exceeds a certain amount (30 for example) delete the first line and move everything up a line.

Understood that 30 is an example. Do you mean that if the file has 36 lines, trim it down to 30 lines, discarding the top 6 lines? Or will it be monitored so if it ever gets over 30 it is trimmed at once? So that 31 is the biggest it ever gets?

And you are enquiring about making the FOR delimiters the beginning and end of a line? That is, take the whole line? Can do that now. It's "delims="
Title: Re: New Line In File
Post by: Helpmeh on January 18, 2009, 09:05:18 AM
1. I'm trying to get the contents, and set each line as a variable.

Could you explain this a bit more ... do you mean you want to get each line into a variable, do something with that variable, then move on to the next line and repeat until all lines are done?

Quote
2. If the linecount exceeds a certain amount (30 for example) delete the first line and move everything up a line.

Understood that 30 is an example. Do you mean that if the file has 36 lines, trim it down to 30 lines, discarding the top 6 lines? Or will it be monitored so if it ever gets over 30 it is trimmed at once? So that 31 is the biggest it ever gets?

And you are enquiring about making the FOR delimiters the beginning and end of a line? That is, take the whole line? Can do that now. It's "delims="

1. I'm trying to get the contents, and set each line as a variable.

Could you explain this a bit more ... do you mean you want to get each line into a variable, do something with that variable, then move on to the next line and repeat until all lines are done?

Quote
2. If the linecount exceeds a certain amount (30 for example) delete the first line and move everything up a line.

Understood that 30 is an example. Do you mean that if the file has 36 lines, trim it down to 30 lines, discarding the top 6 lines? Or will it be monitored so if it ever gets over 30 it is trimmed at once? So that 31 is the biggest it ever gets?

And you are enquiring about making the FOR delimiters the beginning and end of a line? That is, take the whole line? Can do that now. It's "delims="


1. That's correct. My program refreshes every 2 seconds, but when the linecount is over 30, it get's really jumpy. I just want to remove this problem without having to delete the whole file, then start again.

2. I was thinking about that solution, but wasn't sure if it would work or not. And just a question, what would happen if there are more than 26 of them??? Like, I start at %a , but what happens after I get to %z and there's still more lines to go?
Title: Re: New Line In File
Post by: Dias de verano on January 18, 2009, 12:12:20 PM

1. That's correct. My program refreshes every 2 seconds, but when the linecount is over 30, it get's really jumpy. I just want to remove this problem without having to delete the whole file, then start again.

You would have to count the lines in the file, and if there were more than 30, copy the last 30 lines of the file into another file, delete the original file, then rename the second file to the original filename. This might not be possible if the first file was in use by another program.

Quote
And just a question, what would happen if there are more than 26 of them??? Like, I start at %a , but what happens after I get to %z and there's still more lines to go?

I don't get this at all. Why do you want ALL of the lines in the file existing as variables all at once? Why would you need to do that?
Title: Re: New Line In File
Post by: Sidewinder on January 18, 2009, 04:33:57 PM
Quote
I don't get this at all. Why do you want ALL of the lines in the file existing as variables all at once? Why would you need to do that?

I might be wrong, but I suspect it's related to this (http://www.computerhope.com/forum/index.php/topic,73758.0.html) post.

You can get the data into a "pseudo" array by using a stem.tail construct used in some of the other languages (most notably REXX). You cannot however address the array as a single unit.

Code: [Select]
@echo off
setlocal
set fname=drive:\path\file.ext

for /f "tokens=* delims=" %%x in (%fname%) do (
call set /a dx=%%dx%%+1
)

set /a last=%dx%-30
if %last% LSS 0 (
for /f "tokens=* delims=" %%x in (%fname%) do (
call set /a idx=%%idx%%+1
call set array.%%idx%%=%%x
)
) else (
for /f "skip=%last% tokens=* delims=" %%x in (%fname%) do (
call set /a idx=%%idx%%+1
call set array.%%idx%%=%%x
))

for /l %%x in (1,1,%idx%) do call echo %%array.%%x%%

Be sure to change the value of fname. Keep in mind that batch coding like this is more of a parlor trick than anything particularly useful. Better to find a language that is not so underpowered.

 8)

Note: if your file contains any NT batch special characters, the results are unpredictable.
Title: Re: New Line In File
Post by: Helpmeh on January 18, 2009, 06:29:13 PM
Quote
I don't get this at all. Why do you want ALL of the lines in the file existing as variables all at once? Why would you need to do that?

I might be wrong, but I suspect it's related to this (http://www.computerhope.com/forum/index.php/topic,73758.0.html) post.

You are correct, it is related, but for a secret that has to remain vetween my friend and I, I can't reveal the complete reason why I actually need each line as a variable (one reason that I'm allowed to talk about is what I said before, my other script gets jumpy).

I will try the script.

EDIT: Tried the script...Just echos %fname% then closes...Can you explain how the code works so I can try and fix the problem myself?
Title: Re: New Line In File
Post by: Sidewinder on January 18, 2009, 06:57:14 PM
Did you set fname variable to the label of a file on your system?

Quote
Tried the script...Just echos %fname% then closes

There is no echo %fname% instruction in the script. I'm as surprised as you!

Quote
but for a secret that has to remain vetween my friend and I, I can't reveal the complete reason why I actually need each line as a variable (one reason that I'm allowed to talk about is what I said before, my other script gets jumpy).

<sigh> Still compressing/decompressing files?

The first block of the script count the records in  the file

The second block subtracts 30 from the recordcount and either loads the array from the file (recordcount - 30 is negative) or skips recordcount - 30 records before loading the array

The third block lists out the contents of the array

I can't duplicate your error. There may have been an error when you copied/pasted the code into your editor.

 8)

Batch code does cloak and dagger. Who knew?
Title: Re: New Line In File
Post by: BC_Programmer on January 18, 2009, 07:09:24 PM
it's a super secret algorithm... the secret has been lost for ages... since the release of windows 3.1 it has lain forgotten.

It's basically a variation of Run length encoding. Ideally it would work, but unfortunately without looking at the "big picture" - otherwise proved by the proposal to replace words with letters, which won't work since those letters may appear in other words - so basically, it's not exactly a "super secret" technology that one must strive to protect.
Title: Re: New Line In File
Post by: Geek-9pm on January 18, 2009, 07:35:03 PM
Pardon me for butting in.
Is this just and exwercise, or is there a real need for this.
Does another program have the file open? If so, it would be hard to delete
anything from it. Why not just compile a small program the reads all the lines in a file and spits out the last thirty? Orr is there a reason to have 30 variables set to the 30 lines?
Title: Re: New Line In File
Post by: BC_Programmer on January 18, 2009, 07:35:52 PM
I made a mega compression algorithm that can compress anything into a single bit.

Unfortunately it cannot be decompressed.
Title: Re: New Line In File
Post by: Geek-9pm on January 18, 2009, 08:38:15 PM
Quote
if you understand this: (0x2b||!0x2b)
you are a programmer.
I am a programmer and I have no idea of what you mean.
I learned to program in COBOL
Title: Re: New Line In File
Post by: BC_Programmer on January 18, 2009, 09:20:31 PM
Quote
if you understand this: (0x2b||!0x2b)
you are a programmer.
I learned to program in COBOL


like I said.

besides, It doesn't say of you don't understand it your not a programmer... but anybody who does likely is.

it's shakespeare.
Title: Re: New Line In File
Post by: Dusty on January 19, 2009, 12:25:14 AM
Quote
it's shakespeare.

Hamlet? 0x2B | ~ 0x2B
Title: Re: New Line In File
Post by: Dias de verano on January 19, 2009, 12:38:21 AM
for a secret that has to remain vetween my friend and I, I can't reveal the complete reason why I actually need each line as a variable (one reason that I'm allowed to talk about is what I said before, my other script gets jumpy).

When you are much older, say about 14, maybe you'll stop dicking around?  ::)

THis is where I say "goodbye".
Title: Re: New Line In File
Post by: BC_Programmer on January 19, 2009, 01:04:01 AM
Quote
it's shakespeare.

Hamlet? 0x2B | ~ 0x2B

well... yeah but mines the C version!

the VB version would be- &H2B Or Not &H2B which is FAR to obvious.
Title: Re: New Line In File
Post by: Helpmeh on January 19, 2009, 02:31:19 PM
Did you set fname variable to the label of a file on your system?

Quote
Tried the script...Just echos %fname% then closes

There is no echo %fname% instruction in the script. I'm as surprised as you!

Quote
but for a secret that has to remain vetween my friend and I, I can't reveal the complete reason why I actually need each line as a variable (one reason that I'm allowed to talk about is what I said before, my other script gets jumpy).

<sigh> Still compressing/decompressing files?

The first block of the script count the records in  the file

The second block subtracts 30 from the recordcount and either loads the array from the file (recordcount - 30 is negative) or skips recordcount - 30 records before loading the array

The third block lists out the contents of the array

I can't duplicate your error. There may have been an error when you copied/pasted the code into your editor.

 8)

Batch code does cloak and dagger. Who knew?

I'll try the code again...But no, it is not compressing then decompressing files.

But, like I said, the complete reason why I need this must remain between my friend and I, although to help you I will say this:

I need to be able to remove a certain line at will (or automatically if its the first line and there are 31 lines) without directly modifying the text file.

Dias, I was asking for help, not for wasting comments.


EDIT: Code works...almost. It will echo each and every line, and if there is more than 30, doesn't show the first lines...but how can I get each visible line as a variable???
Title: Re: New Line In File
Post by: Sidewinder on January 19, 2009, 04:12:18 PM
Quote
EDIT: Code works...almost. It will echo each and every line, and if there is more than 30, doesn't show the first lines...but how can I get each visible line as a variable???

It could be me, but isn't this what you asked for?

Quote
1. I'm trying to get the contents, and set each line as a variable.
2. If the linecount exceeds a certain amount (30 for example) delete the first line and move everything up a line.

If you want all the lines in variables remove some of the logic from the posted code:

Code: [Select]
@echo off
setlocal
set fname=drive:\path\file.ext

for /f "tokens=* delims=" %%x in (%fname%) do (
call set /a idx=%%idx%%+1
call set array.%%idx%%=%%x
)

for /l %%x in (1,1,%idx%) do call echo %%array.%%x%%

Be aware the environment space (where your variables are stored) is limited to 8K bytes on a XP machine.

Quote
I need to be able to remove a certain line at will (or automatically if its the first line and there are 31 lines) without directly modifying the text file.

Determine which line to delete and write out a new file. You don't need a variable for each line, you need a counter. By the way who is Will?

 8)

Perhaps you should get together with the other guy on this board with the 142 labels. You could share the cost of a KISS 101 course :o
Title: Re: New Line In File
Post by: Geek-9pm on January 20, 2009, 12:36:11 AM
Quote
Hamlet? 0x2B | ~ 0x2B
Yes. Hamlet was not a programmer. In the original context it was an XOR that had to have a YES or NO. But the C construct would become -1  if converted directly to a number. What would Hamlet think if we told him the answer is always minus one?
Title: Re: New Line In File
Post by: Dias de verano on January 20, 2009, 12:46:54 AM
Quote
You don't need a variable for each line, you need a counter.

I told him this days ago.
Title: Re: New Line In File
Post by: Helpmeh on January 20, 2009, 04:09:03 PM
Quote
You don't need a variable for each line, you need a counter.

I told him this days ago.
And I said that I DO need a variable for each line. (But atleast my problem is partially done)