标题: [问题求助] 【已解决】求助VBS如何将文件夹下文本中的某些字符替换? [打印本页]
作者: loveforjg 时间: 2020-12-28 15:12 标题: 【已解决】求助VBS如何将文件夹下文本中的某些字符替换?
1. 某文件夹下有多个文本TXT
2. 需求: 将这些TXT文件中,PINORDER PINSWAP FUNCTION后面的有文件名字符串的字符串替换为NAME RENAME。
RESC1608X50M.txt 内容如下:(详细见附件)
(DEVICE FILE: resc1608x50m)
PACKAGE resc1608x50m
CLASS IC
PINCOUNT 2
PINORDER resc1608x50m 1 2
PINSWAP resc1608x50m 1 2
FUNCTION resc1608x50m resc1608x50m 1 2
END
只替换:PINORDER PINSWAP FUNCTION后面的文件名字符串,其他不变
(注意有的文本会有多行数据,见附件QFN50P600X600X80-41N.txt)
替换结果为:
(DEVICE FILE: resc1608x50m)
PACKAGE resc1608x50m
CLASS IC
PINCOUNT 2
PINORDER NAME 1 2
PINSWAP NAME 1 2
FUNCTION NAME RENAME 1 2
END
作者: WHY 时间: 2020-12-31 15:38
- Rem On Error Resume Next
- Dim reg1
- Set reg1 = New RegExp
- reg1.Pattern = "\b(PIN(?:ORDER|SWAP)[ \t]+)\S+"
- reg1.Global = True
-
- Dim reg2
- Set reg2 = New RegExp
- reg2.Pattern = "\b(FUNCTION[ \t]+)\S+([ \t]+)\S+"
- reg2.Global = True
-
- Dim fso, objFile, ext
- Set fso = CreateObject("Scripting.FileSystemObject")
-
- For Each objFile In fso.GetFolder(".").Files
- ext = fso.GetExtensionName(objFile.Path)
- If LCase(ext) = "txt" Then
- replaceStr objFile.Path
- End If
- Next
-
- Function replaceStr(filePath)
- Dim f, s
- Set f = fso.OpenTextFile(filePath)
- s = f.ReadAll
- s = reg1.Replace(s, "$1NAME")
- s = reg2.Replace(s, "$1NAME$2RENAME")
- f.Close
- fso.OpenTextFile(filePath, 2, True).Write(s)
- End Function
-
- MsgBox "Done"
复制代码
作者: loveforjg 时间: 2021-1-5 15:51
回复 2# WHY
大神您好! 再请教下,如果要把PIN SWAP打头这节的内容都去掉,表达式咋个改?((注意有的文本会有多行数据)
输出为:
(DEVICE FILE: resc1608x50m)
PACKAGE resc1608x50m
CLASS IC
PINCOUNT 2
PINORDER NAME 1 2
FUNCTION NAME RENAME 1 2
END
作者: WHY 时间: 2021-1-5 23:01
本帖最后由 WHY 于 2021-1-5 23:30 编辑
回复 3# loveforjg
把 PINORDER 和 FUNCTION 之后的字符改名,把 PINSWAP 及之后的多行数据删除,多行数据判断的依据:以逗号+回车换行结束。- Rem On Error Resume Next
- Dim reg1
- Set reg1 = New RegExp
- reg1.Pattern = "\b(PINORDER[ \t]+)\S+"
- reg1.Global = True
-
- Dim reg2
- Set reg2 = New RegExp
- reg2.Pattern = "\b(FUNCTION[ \t]+)\S+([ \t]+)\S+"
- reg2.Global = True
-
- Dim reg3
- Set reg3 = New RegExp
- reg3.Pattern = "\bPINSWAP[ \t](?:[^\r\n,]|,\r\n)+(?:\r\n)?"
- reg3.Global = True
-
- Dim fso, objFile, ext
- Set fso = CreateObject("Scripting.FileSystemObject")
-
- For Each objFile In fso.GetFolder(".").Files
- ext = fso.GetExtensionName(objFile.Path)
- If LCase(ext) = "txt" Then
- replaceStr objFile.Path
- End If
- Next
-
- Function replaceStr(filePath)
- Dim f, s
- Set f = fso.OpenTextFile(filePath)
- s = f.ReadAll
- s = reg1.Replace(s, "$1NAME")
- s = reg2.Replace(s, "$1NAME$2RENAME")
- s = reg3.Replace(s, "")
- f.Close
- fso.OpenTextFile(filePath, 2, True).Write(s)
- End Function
-
- MsgBox "Done"
复制代码
作者: loveforjg 时间: 2021-1-7 15:35
回复 4# WHY
谢谢 膜拜大神!
FUNCTION的表达式为何后面多了一截 “([ \t]+)\S+”,有何讲究?
欢迎光临 批处理之家 (http://bathome.net./) |
Powered by Discuz! 7.2 |