2

I need to print the last page of 50 text files. Currently I am opening up all the 50 text files, on a daily basis, one by one and printing the last page from each file - which is a very painful task.

I am aware this task can be done by writing a batch file, however, I am completely ignorant about batch programming.

Would appreciate any kind of help.

Thanks!

wksoh
  • 21
  • 1
  • Do you want to print to the screen or to an actual printer? Also, how do you define the "last page" of a text file? A text file could use form feed characters to delimit pages, but that is not typical of text files. Most text files are a continuous stream of lines. You could use Anshu's idea to print a fixed number of lines at the end of each file. – dbenham Oct 01 '12 at 16:30

1 Answers1

0

Fleshing out the ideas in Anshu's answer. Here is a script that will print the last 50 lines of all .TXT files in the current directory. The output is sent to the default printer.

@echo off
setlocal enableDelayedExpansion
set "tempFile=%temp%\printEnd "
set "pageSize=50"

for %%F in (*.txt) do (
  for /f %%N in ('find /c /v "" ^<"%%F"') do set /a skip=%%N-pageSize
  if !skip! lss 0 set skip=0
  >"%tempFile%%%~nxF" more +!skip! "%%F"
  notepad /p "%tempFile%%%~nxF"
)
echo "%tempFile%"
2>nul del "%tempFile%*"
dbenham
  • 119,153
  • 25
  • 226
  • 353
  • Thank you for your help! The code works like a charm. However, is it possible to add a line to get notepad to print in landscape? Right now it's printing in portrait. Thanks. – wksoh Oct 02 '12 at 03:09
  • @wksoh - I haven't a clue. If you want landscape, then that seems like something that should have been put in your question. – dbenham Oct 02 '12 at 03:20
  • @wksoh - No. You can't do that in batch. To do something like that, you'll need to `start notepad.exe [filename.txt]` and then use VBS to `sendkeys` the proper commands to notepad. – James K Oct 03 '12 at 22:25
  • There are free utilities like [AutoHotKey](http://www.autohotkey.com/) that can be used to send keystrokes to a running application. – dbenham Oct 03 '12 at 22:42