找回密码
 加入
搜索
查看: 2093|回复: 15

[AU3基础] 新人!固定某个文件内容数字替换怎么写?

  [复制链接]
发表于 2019-5-20 21:53:55 | 显示全部楼层 |阅读模式
本帖最后由 aolading 于 2019-5-22 07:57 编辑

刚接触autoit实在不会,我有个D:\XH-1\ZZZ 文件,此文件没后缀。文件内容样式如下:                               测试数据报表
          测试时间:2016年01月16日 22:58试验名称:
          试验条件:                  执行标准:
          样品标注:332135                 测试员:


每天要修改样品标注后的内容上百次,每次都是用文本打开修改,太繁琐,想写个小程序,窗口直接输入数字就能自动替换并保存。
这样要更改时,再输入新的数字就可以了。
说明一下样品标注:后面的有时会是空白,有时就是6位数字,修改一般也基本是6位数字!还有输入的时候最好能先检测一下ZZZ文件是否存在!
我是新手,本想用批处理解决的,折腾好久,也没折腾好,百度了一下,说是autoit什么都能解决,看了一下,呵呵!不会啊!!希望大大们能赐教啊!


本帖子中包含更多资源

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

×
发表于 2019-5-21 13:03:51 | 显示全部楼层
只有一处修改可以直接用 InputBox()
Local $File = 'ZZZ'        ;文件路径
If Not FileExists($File) Then Exit MsgBox(48, '退出', '"' & $File & '" 文件不存在!')
Local $str = FileRead($File), $sNum = ''
Local $aData = StringRegExp($str, '样品标注:(\d*)', 1)
If Not @error Then $sNum = $aData[0]
Local $sNew = InputBox('请输入修改数据', '当前数据如下', $sNum, '', 200, 130)
If @error Then Exit
$str = StringRegExpReplace($str, '(样品标注:)\d*', '${1}' & $sNew)
Local $hFO = FileOpen($File, 2)
If FileWrite($hFO, $str) Then MsgBox(64, '已修改', $str)
FileClose($hFO)

点评

这个基本已经很符合要求了!唯一就是输入的是插入的,我要替换,比如输入6个数字,就替换样品标注后的6个字符位,后面的测试员:位置不能移动,要不我的文件上传了就走不了!  发表于 2019-5-22 08:13
发表于 2019-5-20 22:42:19 | 显示全部楼层
有机会可否上传一下源文件,方便回答。
再就是,如果仅是这样的文件的话,可以fileread,然后读取文件内容后,处理成数组,再写回文件,我大概是这样的想法。
发表于 2019-5-21 07:47:18 | 显示全部楼层
本帖最后由 顽固不化 于 2019-5-21 09:19 编辑

如果只有这么简单的内容,可以考虑GUI方式,填写后按时间保存
例如:


#include <ButtonConstants.au3>
#include <DateTimeConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("这是一个样本", 390,220)
$Label1 = GUICtrlCreateLabel("测试时间:", 24, 24, 64, 16)
$Label2 = GUICtrlCreateLabel("试验名称:", 24, 56, 64, 16)
GUICtrlCreateInput("", 88, 56, 265, 20)
$Label3 = GUICtrlCreateLabel("试验条件:", 24, 88, 64, 16)
GUICtrlCreateInput("", 88, 88, 265, 20)
$Label4 = GUICtrlCreateLabel("执行标准:", 24, 112, 64, 16)
GUICtrlCreateInput("", 88, 112, 265, 20)
$Label5 = GUICtrlCreateLabel("样品标注:", 24, 144, 64, 16)
GUICtrlCreateInput("", 88, 144, 97, 20)
$Label6 = GUICtrlCreateLabel("测试员:", 200, 144, 52, 16)
GUICtrlCreateInput("", 248, 144, 105, 20)
$Button1 = GUICtrlCreateButton("确定", 128, 184, 73, 25)
$Button2 = GUICtrlCreateButton("重填", 216, 184, 73, 25)
$Button3 = GUICtrlCreateButton("退出", 304, 184, 65, 25)
$Date1 = GUICtrlCreateDate("2019/05/21 09:06:31", 88, 24, 186, 20)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE,$Button3
                        Exit

        EndSwitch
WEnd

本帖子中包含更多资源

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

×
发表于 2019-5-21 11:58:16 | 显示全部楼层
没达到楼主需求吧,呵呵
 楼主| 发表于 2019-5-22 07:55:18 | 显示全部楼层
顽固不化 发表于 2019-5-21 07:47
如果只有这么简单的内容,可以考虑GUI方式,填写后按时间保存
例如:

谢谢!这个挺好的!就想要这样的!我只要修改样品标注!其它都不需要改!
发表于 2019-5-22 11:36:06 | 显示全部楼层
afan 发表于 2019-5-21 13:03
只有一处修改可以直接用 InputBox()

如果已有数字,则可以直接原位替换。如无数据,当前是插入输入的数字,因为之前没有上传范本,空格的形式不得而知,可能是空格,也可能是TAB,字符位数最多可能相差8倍,就不猜测了。细节自行处理。
发表于 2019-5-22 11:42:22 | 显示全部楼层
无聊根据A版代码结合弄出一个
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <String.au3>

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 551, 322, 380, 240)
$Group1 = GUICtrlCreateGroup("当前zzz文件内容如下:", 16, 16, 521, 153)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
$Label2 = GUICtrlCreateLabel("", 21, 48, 508, 108)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Label1 = GUICtrlCreateLabel("修改样品标注为:", 32, 224, 149, 28)
GUICtrlSetFont(-1, 14, 400, 0, "MS Sans Serif")
$Input1 = GUICtrlCreateInput("", 181, 216, 201, 32) 
GUICtrlSetFont(-1, 14, 400, 0, "MS Sans Serif")
$Button1 = GUICtrlCreateButton("修改并保存", 408, 216, 97, 41)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
Local $file="D:\XH-1\ZZZ"
If Not FileExists("D:\XH-1\ZZZ") Then Exit MsgBox(64,"提示","文件不存在!",5)
Dim $set=1
wri()

While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        Exit
        Case $Button1
                        diy()
        EndSwitch
               $mousepos=GUIGetCursorInfo()
        If $mousepos[4]=$Input1 And $mousepos[2]=1 And $set=1 Then
                GUICtrlSetState($Input1, $GUI_FOCUS)
                $set=0
        ElseIf $mousepos[4]<>$Input1 And $mousepos[2]=1 Then
                $set=1
        EndIf
WEnd

Func wri()
        
        Local $ff=FileRead($file)
        GUICtrlSetData($Label2,$ff)
        
        Local $f1=FileRead($file)
        Local $f11=StringRegExp($f1, '样品标注:(\d*)', 1)
        If Not @error Then Local $ss=$f11[0]
        GUICtrlSetData($Input1,$ss)
        
        GUICtrlSetState($Button1, $GUI_DEFBUTTON) ;设置默认按钮
    ;ControlFocus($Form1, "", $Input1)  ;设置输入焦点到窗口的指定控件
EndFunc
        
Func diy()
        Local $f1=FileRead($file)
        Local $f11=StringRegExp($f1, '样品标注:(\d*)', 1)
        If Not @error Then Local $ss=$f11[0]

                If @error Then Exit
            $str=StringRegExpReplace($f1, '(样品标注:)\d*', '${1}' & GUICtrlRead($Input1))
                Local $dk = FileOpen($File, 2)
                FileWrite($dk,$str)
            GUICtrlSetData($Label2,$str)
                ;FileClose($dk)
EndFunc

 楼主| 发表于 2019-5-22 13:48:14 | 显示全部楼层
afan 发表于 2019-5-22 11:36
如果已有数字,则可以直接原位替换。如无数据,当前是插入输入的数字,因为之前没有上传范本,空格的形式 ...

原文件传了,对,就是空白是会是插入,有数字时是直接替换的!我该加入什么命令呢?
 楼主| 发表于 2019-5-22 14:00:28 | 显示全部楼层
m765555 发表于 2019-5-22 11:42
无聊根据A版代码结合弄出一个



你这个也蛮好的,就是37行出错啊!但最关键的是空白时是插入的方式输入数据,要求是替换6个数字的空白位。也就是后面的测试员的位置不能移动!如图!

本帖子中包含更多资源

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

×
发表于 2019-5-22 15:53:47 | 显示全部楼层
aolading 发表于 2019-5-22 13:48
原文件传了,对,就是空白是会是插入,有数字时是直接替换的!我该加入什么命令呢?

第8行改为
$str = StringRegExpReplace($str, '(样品标注:)[\d\h]{6}(?=\h+)', '${1}' & $sNew)
发表于 2019-5-22 17:58:11 | 显示全部楼层
aolading 发表于 2019-5-22 14:00
你这个也蛮好的,就是37行出错啊!但最关键的是空白时是插入的方式输入数据,要求是替换6个数字的空 ...

我这里是按你之前没上传文件时,写的,没有想到你文件内容多少,所以显示不完,另外代码没有错误,可能是你复制时编码的问题吧。65行的正则换成a版一样的也就解决插入问题了。
 楼主| 发表于 2019-5-22 20:51:09 | 显示全部楼层
m765555 发表于 2019-5-22 17:58
我这里是按你之前没上传文件时,写的,没有想到你文件内容多少,所以显示不完,另外代码没有错误,可能是 ...

这个错误不是打开出现的,修改读取都正常,只是程序不点关闭的话,鼠标点击其它窗口或桌面时就蹦出来错误窗口了!代码复制没问题!
发表于 2019-5-23 22:02:17 | 显示全部楼层
这样可能好用点,可以结帖了.
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <String.au3>
Opt('GUIOnEventMode', 1)
Opt('TrayAutoPause',0)
Opt('TrayMenuMode',1)
Opt('TrayIconHide',0) ;不隐藏


$g_szVersion = "My Script 1.1"
If WinExists($g_szVersion) Then Exit MsgBox(16,"提示","程序已运行了!",2); 此脚本已经运行了
AutoItWinSetTitle($g_szVersion)

Local $file="D:\XH-1\ZZZ"
If Not FileExists("D:\XH-1\ZZZ") Then Exit MsgBox(64,"提示","文件不存在!",5)

$Form1 = GUICreate("Form1", 951, 622)
GUISetOnEvent(-3, '_Exit')
$Group1 = GUICtrlCreateGroup("当前zzz文件内容如下:", 16, 16, 921, 553)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")

$Edit2 = GUICtrlCreateEdit("", 21, 48, 908, 508)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")

;$Label2 = GUICtrlCreateLabel("", 21, 48, 508, 108)
;GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Label1 = GUICtrlCreateLabel("修改样品标注为:", 132, 580, 149, 28)
GUICtrlSetFont(-1, 14, 400, 0, "MS Sans Serif")
$Input1 = GUICtrlCreateInput("", 311, 580, 201, 32,0x2000) 
$hInput1 = GUICtrlGetHandle($Input1)

GUICtrlSetFont(-1, 14, 400, 0, "MS Sans Serif")
$Button1 = GUICtrlCreateButton("修改并保存", 608, 572, 97, 41)
GUISetState(@SW_SHOW)
GUIRegisterMsg(0x0111, 'WM_COMMAND')

wri()
GUICtrlSetOnEvent($Button1, "diy")

While 1
        Sleep(1000)
WEnd

Func wri()
        
        Local $ff=FileRead($file)
        GUICtrlSetData($Edit2,$ff)
        
        Local $f1=FileRead($file)
        Local $f11=StringRegExp($f1, '样品标注:(\d*)', 1)
        If Not @error Then Local $ss=$f11[0]
        GUICtrlSetData($Input1,$ss)
        
        GUICtrlSetState($Button1, $GUI_DEFBUTTON) ;设置默认按钮
    ;ControlFocus($Form1, "", $Input1)  ;设置输入焦点到窗口的指定控件
EndFunc
        
Func diy()
            If Not GUICtrlRead($Input1) Then 
                        GUISetState(@SW_DISABLE,$Form1)
                        MsgBox(262144,"提示","输入有误或者为空,请检查后再执行!",3)
                        GUISetState(@SW_ENABLE,$Form1)
                ElseIf StringLen(GUICtrlRead($Input1)) <> 6 Then
                        GUISetState(@SW_DISABLE,$Form1)
                        MsgBox(262144,"提示","输入的位数可能不对,请检查后再执行!",3)
                        GUISetState(@SW_ENABLE,$Form1)
                Else
        Local $f1=FileRead($file)
        Local $f11=StringRegExp($f1, '样品标注:(\d*)', 1)
        If Not @error Then Local $ss=$f11[0]

                If @error Then Exit
            $str=StringRegExpReplace($f1, '(样品标注:)[\d\h]{6}(?=\h+)', '${1}' & GUICtrlRead($Input1))
                Local $dk = FileOpen($File, 2)
                FileWrite($dk,$str)
            GUICtrlSetData($Edit2,$str)
                FileClose($dk)
                EndIf
EndFunc



Func _Exit()
        Exit
EndFunc   ;==>_Exit

Func WM_COMMAND($hWnd, $iMsg, $iwParam, $ilParam)
        #forceref $hWnd, $iMsg
        If $ilParam = $hInput1 And BitShift($iwParam, 16) = 256 Then GUICtrlSetState($Input1, 256)
EndFunc   ;==>WM_COMMAND

 楼主| 发表于 2019-5-24 16:27:51 | 显示全部楼层
m765555 发表于 2019-5-23 22:02
这样可能好用点,可以结帖了.

谢谢!!!
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-3-29 23:48 , Processed in 0.088521 second(s), 25 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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