Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Friends Batch File Question- Is there a way to append to top of file in batch  (Read 5899 times)

0 Members and 1 Guest are viewing this topic.

DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
So my friend was asking me about batch files today and I showed him some that I use and the log text files that they create. He asked me if there was a way to append the information to the top of the file within batch so that instead of having to scroll to the bottom in notepad etc that the most recent info was at the top of the file instead of the bottom.

I told him that getting to the bottom of the log file is just a simple key stroke combination of the CTRL + END key in which I showed him how that worked in notepad to get there without having to manually scroll. He seemed determined to challenge if it could be done in batch, but I have not seen it done so I figured I'd post here on his behalf to see if it can be done in just pure batch.

The only way i can see it done would be to run a script that would read the log file from the last line and then write that as the first line in the new ( newest to oldest ) log order in which it would read in the last line and remove this and then work its way to the last line of the original log file and end when no more data was left and the end result would be the original log file reversed.  :-\

Personally I have always just used CTRL + END key to get there quickly, but he does have a interesting question that now has me interested as well.

Normally I just send information appending to the log file such as

Code: [Select]
@echo. Backup Started on %date% at %time%>>Z:\Backup.log
cls
@echo. Processing Backup
c:
cd\.
cd data
xcopy *.* z:\data\*.* /s/d/y >>Z:\changes.log
@echo. Backup Ended on %date% at %time%>>Z:\Backup.log
cls
@echo. Backup Complete
pause

with the xcopy or robocopy routine in between the logging which writes what files were copied or updated with new to changes.log file since I also send the output from xcopy or robocopy to file vs display through the appending redirection to file. During this process leading up to that I have a Processing Backup displayed and when its done a CLS followed by a Backup Completed with a pause statement so I have to close it out when manually run, however the pause is removed from the batch file that is in the startup folder as for I just want it to run and match files and close on its own.

Salmon Trout

  • Guest
Prepending data to log files...

People commonly use temp files for this sort of thing, for example:

1. Echo the new data to a temp file (instead of using >> with the main log, which would put it at the bottom)
2. Rename the main log file to a throwaway name
3. Rename the temp file created in (1) above, to the main log file name
4. Append the old renamed main log file to this new main log file
5. Delete the old main log file
6. Delete the temp file

Code: [Select]
@echo off
REM You have an existing log file called Mylog.txt
REM and you want to add some new log data to the top, not the bottom

REM The process which creates text to be logged
echo %date% %time% this is some data to be logged > Mylog.tmp

If exist Mylog.txt Ren Mylog.txt Mylog.xxx
Copy Mylog.tmp+Mylog.xxx Mylog.txt > nul
if exist Mylog.xxx Del mylog.xxx
Del mylog.tmp

Contents of Mylog.txt after 1, 2, and 3 runs:

Code: [Select]
Run (1)
25/01/2015 19:01:56.02 this is some data to be logged

Run (2)
25/01/2015 19:02:42.96 this is some data to be logged
25/01/2015 19:01:56.02 this is some data to be logged

Run (3)
25/01/2015 19:03:13.63 this is some data to be logged
25/01/2015 19:02:42.96 this is some data to be logged
25/01/2015 19:01:56.02 this is some data to be logged






« Last Edit: January 25, 2015, 12:32:59 PM by Salmon Trout »

DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Thanks Salmon for your help with this.

I never knew you could join via + with the copy command  as you did in:

Quote
echo %date% %time% this is some data to be logged > Mylog.tmp

If exist Mylog.txt Ren Mylog.txt Mylog.xxx
Copy Mylog.tmp+Mylog.xxx Mylog.txt > nul
if exist Mylog.xxx Del mylog.xxx
Del mylog.tmp

I was messing around with a way to do it also and came up with this, then checked back here and found your much better and professional coded method. I found type on a google hit that showed someone doing something similar but different and tweaked it some to work as intended below.

Code: [Select]
@echo. -------------------------->>temp_log.txt
@echo. Logged at %time% on %date%>>temp_log.txt
@echo. -------------------------->>temp_log.txt
type write_log.txt>>temp_log.txt
type temp_log.txt>write_log.txt
erase temp_log.txt

I was playing around with the shuffle earlier but forgot that > doesn't wipe the file clean to enter new text and so I was getting a mess of the following without deleting temp file to start fresh with a new temp file.

Here is the mess I had where duplicate entries started to show up vs a nice newest to oldest log.

Quote
--------------------------
 Logged at 17:20:36.10 on Sun 01/25/2015
 --------------------------
 --------------------------
 Logged at 17:20:43.18 on Sun 01/25/2015
 --------------------------
 --------------------------
 Logged at 17:20:36.10 on Sun 01/25/2015
 --------------------------
 --------------------------
 Logged at 17:20:54.19 on Sun 01/25/2015
 --------------------------
 --------------------------
 Logged at 17:20:36.10 on Sun 01/25/2015
 --------------------------
 --------------------------
 Logged at 17:20:43.18 on Sun 01/25/2015
 --------------------------
 --------------------------
 Logged at 17:20:36.10 on Sun 01/25/2015
 --------------------------

The correct output to the log file with adding the removal of the temp file after each time its run gave better results.

correct output:

Quote
--------------------------
 Logged at 17:14:10.24 on Sun 01/25/2015
 --------------------------
 --------------------------
 Logged at 17:13:46.97 on Sun 01/25/2015
 --------------------------
 --------------------------
 Logged at 17:13:45.48 on Sun 01/25/2015
 --------------------------
 --------------------------
 Logged at 17:13:43.96 on Sun 01/25/2015
 --------------------------
 --------------------------
 Logged at 17:13:42.21 on Sun 01/25/2015
 --------------------------
 --------------------------
 Logged at 17:13:39.40 on Sun 01/25/2015
 --------------------------
 --------------------------
 Logged at 17:13:30.52 on Sun 01/25/2015
 --------------------------
 --------------------------
 Logged at 17:13:20.77 on Sun 01/25/2015
 --------------------------
 --------------------------
 Logged at 17:13:14.75 on Sun 01/25/2015
 --------------------------

I am going to print this and share this with him to show both of our examples that work. With yours looking much better than what I pieced together through some searching and testing.  :)

Salmon Trout

  • Guest
I never knew you could join via + with the copy command

C:\>copy /?
Copies one or more files to another location.

COPY [/D] [/V] [/N] [/Y | /-Y] [/Z] [/L] [/A | /B ] source [/A | /B]
     [+ source [/A | /B] [+ ...]] [destination [/A | /B]]

  source       Specifies the file or files to be copied.
  /A           Indicates an ASCII text file.
  /B           Indicates a binary file.
  /D           Allow the destination file to be created decrypted
  destination  Specifies the directory and/or filename for the new file(s).
  /V           Verifies that new files are written correctly.
  /N           Uses short filename, if available, when copying a file with a
               non-8dot3 name.
  /Y           Suppresses prompting to confirm you want to overwrite an
               existing destination file.
  /-Y          Causes prompting to confirm you want to overwrite an
               existing destination file.
  /Z           Copies networked files in restartable mode.
  /L           If the source is a symbolic link, copy the link to the target
               instead of the actual file the source link points to.

The switch /Y may be preset in the COPYCMD environment variable.
This may be overridden with /-Y on the command line.  Default is
to prompt on overwrites unless COPY command is being executed from
within a batch script.


To append files, specify a single file for destination, but multiple files
for source (using wildcards or file1+file2+file3 format).


To get

(Top)
Contents of File3.txt
Contents of File2.txt
Contents of File1.txt
(Bottom)

... all in one file, fileAll.txt,  in that order,

you'd do:

copy file3.txt+file2.txt+file1.txt fileAll.txt

(i.e. left to right equals top to bottom in the final file)

You could also use TYPE and redirection but it would need more lines of code to keep track of.


DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Thanks for additional info Salmon. I have always kind of ignored the top portion of the DOS help and just payed attention to the switch listings without Microsofts Poor Examples as to how to use them  :-[

I remember many years ago ( 1980s ) trying to decipher what they are showing for examples for command use and getting frustrated and just by habit using the switches and learning by trial and error and from others showing the correct syntax to do what you want done etc.

Such as:

Quote
XCOPY source [destination] [/A | /M] [/D[:date]] [/P] [/S [/E]] [/V] [/W]
                           [/C] [/I] [/Q] [/F] [/L] [/G] [/H] [/R] [/T] [/U]
                           [/K] [/N] [/O] [/X] [/Y] [/-Y] [/Z] [/B]
                           [/EXCLUDE:file1[+file2][+file3]...]

Being new to DOS back in the 80s I would have typed something like this and gotten an error:

Quote
xcopy source c:\data\*.* [destination z:\data\*.*] [/s][/d][/y]

Which we all know is wrong and some might say what the heck were you thinking typing that and using the [ ] and actual source and destination terminology..  But hey i was only about 9 or 10 years old when I started with Microsoft DOS ;D

foxidrive



    Specialist
  • Thanked: 268
  • Experience: Experienced
  • OS: Windows 8
I'm not sure I followed the convo - but putting lines at the top of a log file can be done like this too:

Code: [Select]
@echo off
 >"%temp%\temp.log" echo newest line %date% %time%
>>"%temp%\temp.log" type file.log
move "%temp%\temp.log" file.log >nul

DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Thanks for sharing a 3rd way to do this foxidrive. Printing this now to share with him tomorrow. Also going to promote Computer Hope to see if I can get him here to ask directly with questions.  :)

Salmon Trout

  • Guest
I'm not sure I followed the convo - but putting lines at the top of a log file can be done like this too:

Code: [Select]
@echo off
 >"%temp%\temp.log" echo newest line %date% %time%
>>"%temp%\temp.log" type file.log
move "%temp%\temp.log" file.log >nul

It's the same method. Since you can only append (not prepend) to a file in Windows, you have to write the new stuff to a new file, append the old file to that, and replace the old file with the new file.

Squashman



    Specialist
  • Thanked: 134
  • Experience: Experienced
  • OS: Other
Always two ways to skin a cat.

foxidrive



    Specialist
  • Thanked: 268
  • Experience: Experienced
  • OS: Windows 8