找回密码
 加入
搜索
查看: 2591|回复: 4

[AU3基础] 【已解决】关于fileopen()写入模式1和2的怪事?

[复制链接]
发表于 2012-3-30 09:40:19 | 显示全部楼层 |阅读模式
本帖最后由 apoorman1986 于 2012-3-30 23:04 编辑

我把AU3装个U盘,u:\AU3\SciTe\UserHome\用户设置.properties里面设置了很多SCITE编辑器的工具栏,如图,
U盘的盘符每次不同的电脑都会变(忘了用$(autoit3dir) ),所以自己想个自动获取U盘盘符,然后把盘符写入用户设置.properties
的文件中。谢谢元老3mile 的保持原有编码格式写文件,代码如下:
Local $Code[5]=[-1,0,32,64,128]
$Drive=DriveGetDrive("removable")
If $Drive = 0 Then
MsgBox(0, "提示", "没有插入U盘")
Exit
EndIf
For $i=1 To $Drive[0]
$s = StringUpper($Drive[$i])
Next
$sfile=$s&"\AU3\SciTe\UserHome\用户设置.properties"
$encoding = FileGetEncoding($sFile)
If $encoding=-1 Then
    MsgBox(4096, "错误", "不能获取文件编码.")
    Exit
Else
        $str=FileRead($sfile)
        For $i=0 To UBound($Code)-1;根据文件编码定义字符串标志
                If $encoding=$Code[$i] Then
                        $encoding=$i
                        ExitLoop
                EndIf
                        Next
                    $sReplace=StringRegExpReplace($str,'"(.*?)\\',$s)
                        $str=StringToBinary($sReplace,$encoding)
                $rfile=FileOpen($sfile,2)
                FileWrite($rfile, $str)
                FileClose($rfile)
EndIf        
        
问题是这个$rfile=FileOpen($sfile,2),如果选用$rfile=FileOpen($sfile,1)追加模式则可保持原来的编码,选用$rfile=FileOpen($sfile,2)则不能,试了N次也一样。
选用清空再写入符合目的,但不能保持原有编码,导致U盘里的SCITE编辑器上的工具栏用不了!所以来求解!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×

评分

参与人数 1金钱 +15 收起 理由
afan + 15 感谢主动将修改帖子分类为[已解决],请继续 ...

查看全部评分

发表于 2012-3-30 10:41:40 | 显示全部楼层
用户设置.properties 这个文件必须用 UTF-8 编码吧?
何必检测原用的编码呢?
如果一定要用 FileOpen($sfile,2) 写入新内容, 那么函数参数试试用 2+128 !
 楼主| 发表于 2012-3-30 10:43:15 | 显示全部楼层
本帖最后由 apoorman1986 于 2012-3-30 11:56 编辑

回复 2# user3000


就是想自动根据文件编码写入,128+2是可以的!
Local $Code[5]=[-1,0,32,64,128]
$Drive=DriveGetDrive("removable")
If $Drive = 0 Then
MsgBox(0, "提示", "没有插入U盘")
Exit
EndIf
For $i=1 To $Drive[0]
$s=StringUpper($Drive[$i])
Next
MsgBox(0,"",$s)
$sfile=$s&"\AU3\SciTe\UserHome\用户设置.properties"
$str=FileRead($sfile)
$sReplace=StringRegExpReplace($str,'"(.*?)\\',$s)
$rfile=FileOpen($sfile,130)
FileWrite($rfile, $sReplace)
FileClose($rfile)

        
发表于 2012-3-30 13:31:24 | 显示全部楼层
本帖最后由 lixiaolong 于 2012-3-30 13:33 编辑

回复 1# apoorman1986

修改了一楼代码,有多余的代码去掉了,还有你这个正则有问题.
用你的正则【"(.*?)\\】就变成这样【command.29.*=I:AU3\Au3.REHelper.exe"】.
应该转换成【command.29.*="I:\AU3\Au3.REHelper.exe"】.

$Drive = DriveGetDrive("removable")
If $Drive = 0 Then
        MsgBox(0, "提示", "没有插入U盘")
        Exit
EndIf
For $i = 1 To $Drive[0]
        $s = StringUpper($Drive[$i])
Next
$sfile = $s & "\AU3\SciTe\UserHome\用户设置.properties"
$encoding = FileGetEncoding($sfile)
If $encoding = -1 Then
        MsgBox(4096, "错误", "不能获取文件编码.")
        Exit
Else
        $str = FileRead($sfile)
        $sReplace = StringRegExpReplace($str, '(\w:)', $s)
        $rfile = FileOpen($sfile, 2 + $encoding)
        FileWrite($rfile, $sReplace)
        FileClose($rfile)
EndIf

评分

参与人数 1金钱 +35 贡献 +1 收起 理由
xms77 + 35 + 1

查看全部评分

 楼主| 发表于 2012-3-30 23:04:20 | 显示全部楼层
多谢小龙指点!你的代码测试成功,我的正则还要继续努力!
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-4-16 18:22 , Processed in 0.089006 second(s), 29 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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