temp 发表于 2011-4-1 17:19:13

如何循环某txt文件,并将每一行都赋予变量

如何循环某txt文件,并将每一行都赋予变量
类似与下面的bat语句。
for /f "delims=" %%i in (a.txt) do (
   set var=%%i
   echo !var!
)

kxing 发表于 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)
运行看看,没测试。

temp 发表于 2011-4-1 18:36:56

回复 2# kxing

谢谢,可行,这个连空行也显示出来了,能忽略空行么?

xxoojoeooxx 发表于 2011-4-1 20:11:41

在第六行加一个 If StringStripWS($line,8)="" Then ContinueLoop

temp 发表于 2011-4-1 20:29:41

回复 4# xxoojoeooxx
测试,遇到空行时,脚本不会退出,只是停止显示,即使中途遇空行也是一样

afan 发表于 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

temp 发表于 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为什么不行呢?

xlcwxl 发表于 2011-4-2 06:48:56

afan一来就是正则{:face (303):}

netegg 发表于 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)

lixiaolong 发表于 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)
页: [1]
查看完整版本: 如何循环某txt文件,并将每一行都赋予变量