Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 如何使用.bat而不是追加到结尾来使用文本文件添加到开头?_Batch File - Fatal编程技术网

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

Batch file 如何使用.bat而不是追加到结尾来使用文本文件添加到开头?,batch-file,Batch File,我有一个批处理文件,它返回一个数据列表。目前,每次运行脚本时,它都会将新提取的数据附加到文本文件的末尾。是否有一种方法可以将新数据添加到文件的开头,而不是将其附加到文件的结尾?我需要这样做,因为数据是按时间顺序提取的,我希望文本文件从最新到最旧排序 @ECHO OFF REM用于将服务器用户的统计信息记录到本地c:\userlog目录中的文本文件中 ECHO Terminal Server Users, %Date%, %TIME%>>c:\Userlogs\UserListTes

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

@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

任何使用临时文件的帮助都将不胜感激

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

以下内容将根据您的需要进行操作

    echo this will come at the begining of file >>log.txt
    type log1.txt >> log.txt
    del log1.txt
    ren log.txt log1.txt

首先,读取文件的内容。将其附加到要添加到开头的内容中,并将所有内容写入文件。

您可以尝试使用下面的代码

@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

希望这能解决您的问题。:)

批处理中的一个大问题是构建有序数组,这必须通过文件完成,批处理中没有数组构造。要将内容强制存储到内存中并批量保存一些IOPS,您可以选择使用ramdisk,确保将文件路径抽象为变量。

您能否详细介绍一下您提到的选项,或者举几个例子?