I have a batch file that returns a list of data. Currently, every time I run the script, it appends the newly pulled data to the end of the text file. Is there a way that I can add the new data to the beginning of the file, rather than append it to the end? I need to it work this way because the data is pulled chronologically, and I'd like the text file to be sorted from most recent to oldest.
@ECHO OFF
REM Used to log server users stats into a text file located in the local c:\userlog directory
ECHO Terminal Server Users, %Date%, %TIME%>>c:\Userlogs\UserListTest.txt
quser /server:servername>>C:\Userlogs\UserListTest.txt
START C:\Userlogs\UserListTest.txt
Exit
any help will be greatly appreciated
Following will work as you want
echo this will come at the begining of file >>log.txt
type log1.txt >> log.txt
del log1.txt
ren log.txt log1.txt
one way using temporary file.
prepend.bat :
:: copy existing file to a temporary file
copy c:\temp\existing.txt c:\temp\temp.txt
:: replace content with your new value
echo test >c:\temp\existing.txt
:: append content of temp file
for /F %%i in (c:\temp\temp.txt) do echo %%i >> c:\temp\existing.txt
:: remove tempfile
del c:\temp\temp.txt
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With