Assigning the last line of a txt file to a value

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
drdumont
Posts: 4
Joined: 09 Sep 2011 16:10

Assigning the last line of a txt file to a value

#1 Post by drdumont » 09 Sep 2011 16:25

I'm almost there! I have a script which will search through a txt file (list.txt) to the very last line (or record).
This last line is always a decimal number between 1 and 1000.
I'd like to be able to assign this to a value in a batch file, but cannot understand how to import this one line of
data from the file.
I'm building a rudimentary CD database, all I want is the disk number (that begs the question above).

1) open the data file
2) read the last entry (it is the disk number)
3) increment the number by 1
4) write this number to the file
5) send the disk directory contents to the file
6) write the same disk number to the file.
(That way when I search for the disk number, each disk's directory is between the two same numbers)
I've got everything but item (2). Right now I have to enter the next disk number manually and I would like to
automate this.

I'm having to learn the batch file language after all these years. I usually do 'most everything in Turbo Basic (yes I am THAT old), but every Basic I find will only export filenames in 8.3. Even when SHELLing to a command file. Ahhhrghh!
Would really appreciate some help, I thank you in advance
-- Doc

trebor68
Posts: 146
Joined: 01 Jul 2011 08:47

Re: Assigning the last line of a txt file to a value

#2 Post by trebor68 » 09 Sep 2011 23:56

If only in the last line a number and nothing else?

If yes then is this the solution:

Code: Select all

@echo off
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
if not exist ich.txt echo 0 >ich.txt

for /f %%a in (ich.txt) do set last=%%a
set /a last+=1
echo !last!>>ich.txt

drdumont
Posts: 4
Joined: 09 Sep 2011 16:10

Re: Assigning the last line of a txt file to a value

#3 Post by drdumont » 10 Sep 2011 10:53

Thanks! I'll give this a try. Doesn't seem to be a book or list anywhere of all the Batch commands. Not i one place, anyway. I've got three copies of my GWBASIC and TURBO BASIC books, so I can usually lay my hands on one within a few minutes. (I'm organizationally challenged, as I like to say).
Thanks again,
-- Doc

trebor68
Posts: 146
Joined: 01 Jul 2011 08:47

Re: Assigning the last line of a txt file to a value

#4 Post by trebor68 » 11 Sep 2011 12:02

Please see this site: http://www.dostips.com/DosCommandIndex.php

Also see the different other topics in this forum.

Trebor68

drdumont
Posts: 4
Joined: 09 Sep 2011 16:10

Re: Assigning the last line of a txt file to a value

#5 Post by drdumont » 11 Sep 2011 16:43

Thanks, Trebor! Just for what I've been looking.

This is a an Helluva good support forum. Maybe one day I will be able to answer rather than just ask.

Yours for Better Television,
== Doc

Remember, a preposition is the wrong thing to end a sentence with - and That is something up with which I shall not put!

Post Reply