【发布时间】:2023-03-29 10:31:02
【问题描述】:
我想要一个批处理文件,我可以将一个文本文件拖到上面(最好是一次多个文本文件),它将逐行读取每个文本文件并将每一行添加到指定的目标文本文件中。目标文本文件将不包含任何重复的行,并将按字母顺序排序。源文件永远不会包含同一行两次,但可能包含非字母数字字符,例如:{ - : _ ~ !
例子:
a.txt:
apple
banana
garbage carrot
{Elmer Fudd}
b.txt
1 tequila
2 tequila
3 tequila
garbage carrot
{Bugs Bunny}
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
它将文件复制到一个临时文件中,但我不知道如何对其进行排序。排序命令对我不起作用,它只是挂起程序。感谢所有帮助。谢谢!
【问题讨论】:
-
按字母顺序批量处理特殊字符将非常困难。
-
我知道困难所在。我目前陷入困境,我什至无法将非特殊字符复制到临时文件中。它一直打开最初拖动的文件而不是读取它:
REM @echo offREM setlocal disabledelayedexpansionecho "%~1" > temp.txtpausefor /f "delims=;" %%F in ('%~1') do (for /f "delims=;" %%F in ('%~1') do (echo %%F>>temp.txt)pausedel /q destination.txtren temp.txt destination.txtspan>ren temp.txt destination.txtspan>
标签: windows file sorting batch-file