4

データのリストを返すバッチファイルがあります。現在、スクリプトを実行するたびに、新しく取得されたデータがテキスト ファイルの末尾に追加されます。新しいデータをファイルの最後に追加するのではなく、ファイルの先頭に追加する方法はありますか? データは時系列で取得されるため、この方法で作業する必要があり、テキスト ファイルを最新のものから古いものへと並べ替えたいと考えています。

@ECHO OFF

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

どんな助けでも大歓迎です

4

5 に答える 5

8

以下はあなたが望むように動作します

    echo this will come at the begining of file >>log.txt
    type log1.txt >> log.txt
    del log1.txt
    ren log.txt log1.txt
于 2013-01-30T05:25:02.883 に答える
2

以下のコードを使用して試すことができます。

@ECHO OFF
:: Store the string  you want to prepend in a variable
SET "text=%1"
:: copy the contents into a temp file
type userlist.txt > temp.txt
:: Now Overwrite the file with the contents in "text" variable
echo %text% > userlist.txt 
:: Now Append the Old Contents
type temp.txt >> userlist.txt
:: Delete the temporary file
del temp.txt

これで問題が解決することを願っています。:)

于 2013-01-30T11:19:03.923 に答える
2

一時ファイルを使用する一方向。

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
于 2013-01-29T18:48:22.487 に答える
-2

まず、ファイルの内容を読み取ります。それを最初に追加したいものに追加し、そのすべてをファイルに書き込みます。

于 2013-01-30T05:59:49.967 に答える