如何使用.bat添加开始使用文本文件而不是追加到结尾?

Mat*_*att 3 batch-file

我有一个返回数据列表的批处理文件.目前,每次运行脚本时,它都会将新提取的数据附加到文本文件的末尾.有没有办法可以将新数据添加到文件的开头,而不是将其追加到最后?我需要以这种方式工作,因为按时间顺序拉动数据,我希望文本文件从最新到最旧排序.

@ECHO OFF
Run Code Online (Sandbox Code Playgroud)

REM用于将服务器用户统计信息记录到位于本地c:\ userlog目录中的文本文件中

ECHO Terminal Server Users, %Date%, %TIME%>>c:\Userlogs\UserListTest.txt
quser /server:servername>>C:\Userlogs\UserListTest.txt

START C:\Userlogs\UserListTest.txt
Exit
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激

小智 6

以下将按您的意愿工作

    echo this will come at the begining of file >>log.txt
    type log1.txt >> log.txt
    del log1.txt
    ren log.txt log1.txt
Run Code Online (Sandbox Code Playgroud)


Loï*_*HEL 2

使用临时文件的一种方法。

前置.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
Run Code Online (Sandbox Code Playgroud)