找回密码
 加入
搜索
查看: 6337|回复: 5

[AU3基础] 【已解决】怎样批量添加文本到另一文件末尾?

[复制链接]
发表于 2014-8-8 20:17:37 | 显示全部楼层 |阅读模式
本帖最后由 chamlien 于 2014-8-8 23:20 编辑

假如D盘文件有100个txt文件,E盘也有100个和D盘同名的文件,但是文本内容不同:

比如D盘的123.txt 里面的文本是12345678

E盘的123.txt 里面的文本是abcdefg

如何批量读取D盘100个文本里的内容添加到E盘的100个同名文本的末尾呢?
 楼主| 发表于 2014-8-8 21:08:43 | 显示全部楼层
回复 1# chamlien

自己在弄源码了,成功后分享
发表于 2014-8-8 21:13:48 | 显示全部楼层
这几个函数应该派上用场:
_FileListToArray
FileOpen
FileWrite

放到FOR...NEXT循环控制语句里组合起来,就实现你的目的了.
发表于 2014-8-8 22:04:07 | 显示全部楼层
回复 3# user3000


   楼上威武~~
 楼主| 发表于 2014-8-8 23:17:17 | 显示全部楼层
本帖最后由 chamlien 于 2014-8-8 23:18 编辑
#NoTrayIcon
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Constants.au3>
#include <ButtonConstants.au3>
#include <Misc.au3>
#include <File.au3>
#include <StaticConstants.au3>
#include <GuiButton.au3>

If _Singleton(@ScriptName, 1) = 0 Then
        MsgBox(64, "警告!", "您只能运行一个此应用程序!")
        Exit (1)
EndIf

;GUI开始
$form = GUICreate("文本批量合成器", 500, 250)
GUISetBkColor(0xCCCCFF)

$Button1 = GUICtrlCreateButton("读取目录", 370, 10, 100, 28, 0)
GUICtrlSetFont(-1, 12, 400, 0, "黑体")
GUICtrlSetColor(-1, 0x009933)
$Button2 = GUICtrlCreateButton("写入目录", 370, 55, 100, 28, 0)
GUICtrlSetFont(-1, 12, 400, 0, "黑体")
GUICtrlSetColor(-1, 0x009933)
$Button3 = GUICtrlCreateButton("保存目录", 370, 100, 100, 28, 0)
GUICtrlSetFont(-1, 12, 400, 0, "黑体")
GUICtrlSetColor(-1, 0x009933)
$Button4 = GUICtrlCreateButton("开始合成", 370, 165, 108, 45, 0)
GUICtrlSetFont(-1, 14, 400, 0, "楷体_GB2312")
GUICtrlSetColor(-1, 0x0000FF)
$input1 = GUICtrlCreateInput("", 28, 10, 310, 28)
$input2 = GUICtrlCreateInput("", 28, 55, 310, 28)
$input3 = GUICtrlCreateInput("", 28, 100, 310, 28)
;$btn = GUICtrlCreateButton("开 始 生 成", 33, 112, 176, 25)
$label3 = GUICtrlCreateLabel("温馨提示:读取目录里的文件内容将被写入到写入目录里的同名文本的末尾!", 38, 145, 239, 80)
GUICtrlSetFont(-1, 11, 400, 0, "黑体")
GUICtrlSetColor(-1, 0x110000)
GUISetState(@SW_SHOW)
;$var = FileSelectFolder("选择一个文件夹.", "")

While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        Exit
                Case $Button1
                        $read = FileSelectFolder("选择要读取的文件夹", "")
                        GUICtrlSetData($input1,$read)
                        GUICtrlSetColor($input1, 0xFF0433)
                        $duqu=GUICtrlRead($input1)
                        ;MsgBox(0,"",$duqu)
                Case $Button2
                        $write = FileSelectFolder("选择要写入的文件夹", "")
                        GUICtrlSetData($input2,$write)
                        GUICtrlSetColor($input2, 0xFF0433)
                        $xieru=GUICtrlRead($input2)
                Case $Button3
                        $save = FileSelectFolder("选择要保存的文件夹", "")
                        GUICtrlSetData($input3,$save)
                        GUICtrlSetColor($input3, 0xFF0433)
                        $baocun=GUICtrlRead($input3)
                Case $Button4
                                GUICtrlSetState($Button4, $GUI_DISABLE)
                                $label3 = GUICtrlCreateLabel("正在合成,请勿关闭....", 38, 190, 270, 20)
                                GUICtrlSetFont(-1, 11, 400, 0, "黑体")
                                GUICtrlSetColor(-1, 0xFF0433)
                                FileCopy($xieru & "\*.*",$baocun,9)
                                start($duqu)
        EndSwitch
WEnd
Func start($duqu)
        
        Local $hSearch = FileFindFirstFile($duqu & "\*.*")
    ; 检查搜索是否成功
    If $hSearch = -1 Then Return
    While 1
        Local $sFile = FileFindNextFile($hSearch)
        If @error Then ExitLoop
        
                If @extended Then 
                        start($duqu & "" & $sFile)
            ContinueLoop
                EndIf
                $file1 = FileOpen($duqu & "" & $sFile, 0)
                $file2 = FileOpen($baocun & "" & $sFile, 1)
                Local $CountLines = _FileCountLines($duqu & "" & $sFile)
                ;MsgBox(0,"",$CountLines)
                For $i=1 To $CountLines Step 1
                Local $line = FileReadLine($file1,$i)
                FileWrite($file2,@CRLF&$line)
                If $i == $CountLines Then ExitLoop
                Next
                FileClose($file1)
                FileClose($file2)
    WEnd
        FileClose($hSearch)
                GUICtrlSetState($Button4, $GUI_ENABLE)
                $label3 = GUICtrlCreateLabel("合成完毕,请及时查看!", 38, 190, 270, 20)
                GUICtrlSetFont(-1, 11, 400, 0, "黑体")
                GUICtrlSetColor(-1, 0xFF0433)
                
EndFunc
自己写的,可能效率很低,错误判断未设置,具体思路是:

文件夹A有N个txt文件,文件夹B有同样多的同名txt文件,

打开程序,读取A文件夹的txt文件,写入要追加的B文件夹的txt文件里面

其中为了保存文件完整性,要写入的B文件夹的txt文件会复制到用户选择的文件夹中..

欢迎各位大侠优化,加上各种错误判断
 楼主| 发表于 2014-8-8 23:19:48 | 显示全部楼层
回复 3# user3000


正运用了这种思路,不过用的是filerealine。
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-9-29 08:16 , Processed in 0.096782 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表