夜猫猫 发表于 2011-3-8 01:49:31

如何读取文件夹下的TXT文件并将文件内容逐行合并一个文件(结贴)

本帖最后由 夜猫猫 于 2011-3-11 03:47 编辑

比如1文件夹下有1.txt,1.txt.3.txt
1.txt内容为1111111111
2txt内容为2222222222
3.txt内容为3333333333
怎么读取他们,并将所有内容合并一起为4.txt
4.txt内容为其他三个txt的合并内容
即:4.txt
1111111111
2222222222
3333333333

下面是获取文件路径:#include <File.au3>
#include <GUIConstantsEx.au3>

#Region ### START Koda GUI section ### Form=
Global $str, $file, $hFile, $FileList, $folder

$Form1 = GUICreate("", 473, 189, 192, 114)
$Input1 = GUICtrlCreateInput("", 48, 136, 225, 21)
$Button1 = GUICtrlCreateButton("读取", 220, 160, 57, 21)
$Button2 = GUICtrlCreateButton("打开", 300, 136, 57, 21)
$Edit1 = GUICtrlCreateEdit("", 8, 8, 441, 113)
GUISetState(@SW_SHOW)

Func Ru()
$FileList=_FileListToArray($folder)
$file=FileOpen ("log.log",2)
For $i = 1 To $FileList
      FileWriteLine ($file, $folder&"\"&$FileList[$i])
Next   
FileClose($file)
EndFunc


While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        Exit
                Case$Button1
          Case$Button2
                  $folder = FileSelectFolder("选择一个文件夹", "",1+4,"")
                  If Not @error Then
            Ru()
                  GUICtrlSetData($Edit1, "")
                  GUICtrlSetData($Edit1, FileRead(@ScriptDir&"\log.log"))            
                  EndIf
        EndSwitch
WEnd

happytc 发表于 2011-3-8 07:45:08

回复 1# 夜猫猫

$str1 = FileRead("F:\1.txt")
$str2 = FileRead("F:\2.txt")
$str3 = FileRead("F:\3.txt")
$str4 = $str1 & @CRLF & $str2 & @CRLF & $str3
FileWrite("F:\4.txt", $str4)
或者直接调用dos的copy命令吧

happytc 发表于 2011-3-8 07:48:41

你的“逐行”是啥意思,不会是如4.txt的前三行是分别是1.txt,2.txt,3.txt的第一行吧?
若是这样,你用FileReadLine()吧

lxz 发表于 2011-3-8 08:10:18

进来学习学习.

夜猫猫 发表于 2011-3-9 06:26:46

逐行就是三楼说的。
郁闷!我用FileReadLine()写不进去,运行后4.TXT 是空白的。

骗子 发表于 2011-3-9 14:47:56

逐行就是三楼说的。
郁闷!我用FileReadLine()写不进去,运行后4.TXT 是空白的。
夜猫猫 发表于 2011-3-9 06:26 http://www.autoitx.com/images/common/back.gif
那个命令是读取的命令吧?

www378660084 发表于 2011-3-9 18:20:01

filereadline
filewriteline

夜猫猫 发表于 2011-3-11 03:45:44

FileWriteLine

netegg 发表于 2011-3-11 04:15:55

filewrite($file4, fileread($file1)&@crlf& fileread($file2)&@crlf&fileread($file3))
页: [1]
查看完整版本: 如何读取文件夹下的TXT文件并将文件内容逐行合并一个文件(结贴)