Windows 批处理文件,用于在从其他txt追加后对txt进行排序
我想要一个批处理文件,我可以把一个文本文件拖到其中(最好是一次多个文本文件),它将逐行读取每个文本文件,并将每一行添加到指定的目标文本文件中。目标文本文件将不包含任何重复的行,并将按字母顺序排序。源文件永远不会包含同一行两次,但可能包含非字母数字字符,例如:{-:} 例如: a、 txt:Windows 批处理文件,用于在从其他txt追加后对txt进行排序,windows,file,sorting,batch-file,Windows,File,Sorting,Batch File,我想要一个批处理文件,我可以把一个文本文件拖到其中(最好是一次多个文本文件),它将逐行读取每个文本文件,并将每一行添加到指定的目标文本文件中。目标文本文件将不包含任何重复的行,并将按字母顺序排序。源文件永远不会包含同一行两次,但可能包含非字母数字字符,例如:{-:} 例如: a、 txt: apple banana garbage carrot {Elmer Fudd} b、 文本 destination.txt之前: {daffy duck} floor 将a.txt和b.txt拖动到批处
apple
banana
garbage carrot
{Elmer Fudd}
b、 文本
destination.txt之前:
{daffy duck}
floor
将a.txt和b.txt拖动到批处理文件后,返回destination.txt:
{Bugs Bunny}
{daffy duck}
{Elmer Fudd}
1 tequila
2 tequila
3 tequila
apple
banana
floor
garbage carrot
我已经开始了:
@echo off
setlocal disabledelayedexpansion
set "sourcefile=%~1"
echo "%sourcefile%" > temp.txt
for /f "delims=;" %%F in (%sourcefile%) do (
echo %%F>>temp.txt
)
del /q destination.txt
ren temp.txt destination.txt
它将文件复制到临时文件中,但我不知道如何对其进行排序。排序命令对我不起作用,它只是挂断了程序。非常感谢所有帮助。谢谢!@echo off
延迟扩展
设置“prev=”
回声“%~1”
复制/y destination.txt+“%~1”temp.txt>nul
(
对于/f“delims=“%%f in('sort temp.txt')do(
如果“!prev!”neq“%%F”回波(%%F
设置“上一个=%%F”
)
)>destination.txt
del temp.txt
应该对你有用(我没试过)
无需使用for/f
-复制a+b c
将a和b连接到c中来处理新文件。/y
强制覆盖已存在的目标
然后处理每一行,如果该行与前一行不匹配,则回显从排序中读取的行,但“将字符串括在引号中”,以便batch知道如果字符串包含空格或其他分隔符,则将其作为单个单元处理
(…echo…>)文件
格式(re)使用echo
ed数据创建文件,而不是将数据发送到屏幕。另存为批处理文件。这是一个混合批处理/jscript文件。文件处理/排序在批处理部分完成。然后,调用javascript部分以消除重复的行
编辑-适应评论
@if (@This==@IsBatch) @then
@echo off
rem **** batch zone *********************************************************
setlocal enableextensions disabledelayedexpansion
rem If there are no files, nothing to do
if "%~1"=="" goto endProcess
rem Configure the output final file
set "outputFile=destination.txt"
if not exist "%outputFile%" >"%outputFile%" break
rem Configure and initialize temporary file
set "tempFile=%temp%\%~nx0.%random%%random%.tmp"
find /v "" <"%outputFile%" >"%tempFile%"
rem Iterate over the file list sending output to temporary file and deleting input file
for %%a in (%*) do (
find /v "" <"%%a" >>"%tempFile%"
rem del /q "%%a" 2>nul
)
rem Process temporary file into outpufile, sorting and eliminating duplicates
type "%tempFile%" | sort | cscript //nologo //e:Javascript "%~f0" > "%outputFile%"
rem Cleanup
del /q "%tempFile%" 2>nul
:endProcess
endlocal
exit /b
@end
// **** Javascript zone *****************************************************
var stdin=WScript.StdIn, stdout=WScript.StdOut, previous=null, current;
while (!stdin.AtEndOfStream){
if (previous !== (current=stdin.ReadLine())) stdout.WriteLine(current);
previous = current;
};
@如果(@This==@IsBatch)@那么
@回音
rem****批处理区*********************************************************
setlocal enableextensions disabledelayedexpansion
rem如果没有文件,则无需执行任何操作
如果“%~1”==”转到结束进程
rem配置输出最终文件
设置“outputFile=destination.txt”
如果不存在“%outputFile%”>“%outputFile%”中断
rem配置并初始化临时文件
设置“tempFile=%temp%\%~nx0.%random%%random%.tmp”
查找/v“”%tempFile%
rem迭代文件列表,将输出发送到临时文件并删除输入文件
对于%%a in(%*)do(
查找/v“”>%tempFile%
rem del/q“%%a”2>nul
)
rem将临时文件处理为输出文件,排序并消除重复项
键入“%tempFile%”| sort | cscript//nologo//e:Javascript“%~f0”>“%outputFile%”
快速眼动清理
删除/q“%tempFile%”2>nul
:结束进程
端部
退出/b
@结束
//****Javascript区域*****************************************************
var stdin=WScript.stdin,stdout=WScript.stdout,previous=null,current;
而(!stdin.AtEndOfStream){
如果(上一个!==(当前=stdin.ReadLine()))stdout.WriteLine(当前);
先前=当前;
};
如果我理解正确,那么这就足够了:
@echo off
:loop
type "%~1" >>"c:\destination.txt"
shift
if not "%~1"=="" goto :loop
sort < "c:\destination.txt" > "%temp%\random file.txt"
move "%temp%\random file.txt" "c:\destination.txt"
@echo关闭
:循环
键入“%~1”>>“c:\destination.txt”
移位
如果不是“%~1”==”转到:循环
排序<“c:\destination.txt”>%temp%\random file.txt”
将“%temp%\random file.txt”移动到c:\destination.txt
批量按字母顺序使用特殊字符将非常困难。我意识到了困难。我目前陷入困境,甚至无法让它将非特殊字符复制到临时文件中。它一直打开最初拖动的文件,而不是读取它:REM@echo off
REM setlocal disabledelayedexpansion>echo“%$1”>temp.txt
pause
for/f“delims=;”%%f in(''%1')do(
echo%%f>>temp.txt
)
pause
del/q destination.txt
ren temp.txt destination.txt
pause
不幸的是,sort命令导致批挂起。不知道为什么,它不像是在数十亿行中排序。我的测试文件每行只有3行字母。感谢您的帮助。谢谢,MC ND.我知道这在很大程度上是如何工作的。它似乎可以完美地处理两个文件,但一次拖动两个以上的文件会导致部分行重叠。此外,如果我想让它在运行过程中删除源文件,我将如何完成此操作?@RADRaze2KX,如果输入文件中的行不重叠,则会发生行重叠以CRLF结束。使用find
过滤器更改了type
命令,以强制包含行终止符。在输入文件上添加了一个循环,以允许在处理过程中删除这些文件(但包含rem
,如果对您有效,请将其删除)。这一命令在我的桌面(Windows Server 2012)上似乎运行良好但在运行Windows 8.1的笔记本电脑上似乎不起作用。我还没有尝试过其他操作系统,但你知道为什么会这样吗?到目前为止,这是一个完美的程序,谢谢你的努力。我删除了REM,但它没有删除文件。没关系,我想我现在更喜欢这种方式。有什么方法可以获取完成的文件吗一旦javascript完成操作,就将其放在某个地方?我有一个代码可以将该文件上载到我的web服务器,但不确定将其粘贴到何处。我总是可以创建一个父批处理文件,然后运行它来运行该批处理文件,然后再运行上载程序。@RADRaze2KX,在清理之后,在:endProcess
之前调用上载程序。此时您拥有该文件并可以调用进程的其余部分的时间。对于操作系统问题,这取决于您的配置。它应该从XP开始工作。在您的8.1中,什么不工作?
@echo off
:loop
type "%~1" >>"c:\destination.txt"
shift
if not "%~1"=="" goto :loop
sort < "c:\destination.txt" > "%temp%\random file.txt"
move "%temp%\random file.txt" "c:\destination.txt"