找回密码
 加入
搜索
查看: 4597|回复: 9

[AU3基础] 如何循环某txt文件,并将每一行都赋予变量

  [复制链接]
发表于 2011-4-1 17:19:13 | 显示全部楼层 |阅读模式
如何循环某txt文件,并将每一行都赋予变量
类似与下面的bat语句。
for /f "delims=" %%i in (a.txt) do (
   set var=%%i
   echo !var!
)
发表于 2011-4-1 18:04:32 | 显示全部楼层
local $hFile=fileopen("1.txt")

$i=1
while 1
$line=filereadline($hFile,$i)
if @error then exitloop
msgbox(0,$i,$line)
$i+=1
wend
fileclose($hFile)
运行看看,没测试。
 楼主| 发表于 2011-4-1 18:36:56 | 显示全部楼层
回复 2# kxing

谢谢,可行,这个连空行也显示出来了,能忽略空行么?
发表于 2011-4-1 20:11:41 | 显示全部楼层
在第六行加一个
 If StringStripWS($line,8)="" Then ContinueLoop
 楼主| 发表于 2011-4-1 20:29:41 | 显示全部楼层
回复 4# xxoojoeooxx
测试,遇到空行时,脚本不会退出,只是停止显示,即使中途遇空行也是一样
发表于 2011-4-1 21:01:47 | 显示全部楼层
本帖最后由 afan 于 2011-4-2 10:15 编辑
Local $sR = StringRegExp(FileRead('a.txt'), '.*[^\s]+[^\r]*', 3)
If @Error Then Exit -1
For $i = 0 To UBound($sR) - 1
        Msgbox(0, $i, $sR[$i])
Next
 楼主| 发表于 2011-4-1 23:16:27 | 显示全部楼层
local $hFile=fileopen("a.txt")
$i=1
while 1
   $line=filereadline($hFile,$i)
   if @error then exitloop
   If Not StringStripWS($line,8)="" Then msgbox(0,$i,$line)
   $i+=1
wend
fileclose($hFile)
这样就可以了,但用ContinueLoop为什么不行呢?
发表于 2011-4-2 06:48:56 | 显示全部楼层
afan一来就是正则
发表于 2011-4-2 18:17:30 | 显示全部楼层
本帖最后由 netegg 于 2011-4-2 18:46 编辑
#include<file.au3>
#include<array.au3>
Local $aA, $temp
_filereadtoarray($file, $aA)
_arraydelete($aA, 0)
$temp= _ArrayToString($aA, '|')
Do 
$temp = stringreplace($temp, '||','|')
Until Not(StringInStr($temp, '||'))
$aRet = stringsplit($temp, '|', 2)
_arraydisplay($aRect, ' ')
;fileopen($file, 2)
;_filewritefromarray($file, $aRet)
;fileclose($file)
发表于 2011-4-2 20:58:52 | 显示全部楼层
回复 7# temp

可以用ContinueLoop,只是你没加$i += 1.
Local $hFile = FileOpen("a.txt")
$i = 1
While 1
        $line = FileReadLine($hFile, $i)
        If @error Then ExitLoop
        If $line = "" Then
                $i += 1
                ContinueLoop
        EndIf
        MsgBox(0, $i, $line)
        $i += 1
WEnd
FileClose($hFile)
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-9-21 10:41 , Processed in 0.094076 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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