Computing.Net > Forums > Programming > batch file to read last four lines.

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

batch file to read last four lines.

Reply to Message Icon

Name: all0gIc
Date: April 16, 2009 at 03:34:58 Pacific
OS: Windows XP
Subcategory: Batch
Comment:

Greetings,

I've been doing some researching on the forum & I can't quite find the exact answer that I am looking for.

What I would like is to create a batch file to read the last four digits of a text file from the first line, then rename a .txt to the four digits that I just read from the file.

I've got a rough idea, but don't quite know how to put it all together...

Here is the information that i've gathered so far

something like this...

@echo off
echo "Read a line of text from a text file" >Text.txt
set /p var= <Text.txt
echo %var%
pause
exit

I know the above will only read

i am a bit of a newbie, so any pointers would greatly be appreciated!



Sponsored Link
Ads by Google

Response Number 1
Name: reno
Date: April 16, 2009 at 03:57:00 Pacific
Reply:

@echo off
echo Read a line of text from a text file>Text.txt
set /p var=<Text.txt
set var=%var:~-4%
echo ren "sometextfile.txt" "%var%.txt"


0

Response Number 2
Name: all0gIc
Date: April 16, 2009 at 04:50:59 Pacific
Reply:

super, I will give that a try & get back to you!


0

Response Number 3
Name: all0gIc
Date: April 16, 2009 at 05:21:02 Pacific
Reply:

ok, that seems to do something, however my output is like this.

batch echo's

ren "file.txt" "    .txt"

it looks like its not reading the last four numbers from the file, however when doing some testing i took out everything from the source txt file and left four numbers & it then worked.

this is an example of the txt file first line

FPURINVOIC518102009041405599


0

Response Number 4
Name: all0gIc
Date: April 16, 2009 at 05:29:46 Pacific
Reply:

ah, ok found the problem. it seems there is spaces at the end of txt file, is it possible to get the command to ignore these empty spaces?


0

Response Number 5
Name: all0gIc
Date: April 16, 2009 at 05:38:59 Pacific
Reply:

if it helps, the string is always 28 characters long


0

Related Posts

See More



Response Number 6
Name: Mechanix2Go
Date: April 16, 2009 at 08:50:01 Pacific
Reply:

This is a solution to lose trailing spaces. But it's not elegant.

============================
@echo off > myfile.txt & setLocal EnableDelayedExpansion

for /f "tokens=* delims=" %%a in (infofile) do (
call :sub1 %%a
ren myfile.txt !str!.txt
)
goto :eof

:sub1
set str=%1
set str=!str:~-4!
goto :eof


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 7
Name: Sen Hu
Date: April 27, 2009 at 09:43:16 Pacific
Reply:

The following biterscripting script is an alternate solution.

SCRIPT
var str text ; cat Text.txt > $text
var str line1 ; lex "1" $text > $line1
var str digits ; chex -e "[25" $line1 > $digits ; chex -e "[5" $digits > null
system ren myfile.txt $digits

First command line reads file Text.txt in variable $text. Second command line extracts the first line into $line1. Third command line extracts chars 25-28 into variable $digits. The last command line renames myfile.txt to the value of $digits.

If chars 25-28 will have a space in them, and you want to retain that space in the renamed file name, use ("\""+$digits+"\"") in the system command.

To try this, install biterscripting from http://www.biterscripting.com/insta... . It is free. It seems to have more flexibility. And, its command are more conducive toward formulating a solution.

Sen



0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: batch file to read last four lines.

DOS batch file to read newest text file www.computing.net/answers/programming/dos-batch-file-to-read-newest-text-file/19729.html

Batch File To Delete Line www.computing.net/answers/programming/batch-file-to-delete-line/15539.html

batch file to read all lines from txt file www.computing.net/answers/programming/batch-file-to-read-all-lines-from-txt-file-/19834.html