[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[问题求助] 【已解决】求助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

  1. Rem On Error Resume Next
  2. Dim reg1
  3. Set reg1 = New RegExp
  4. reg1.Pattern = "\b(PIN(?:ORDER|SWAP)[ \t]+)\S+"
  5. reg1.Global = True
  6. Dim reg2
  7. Set reg2 = New RegExp
  8. reg2.Pattern = "\b(FUNCTION[ \t]+)\S+([ \t]+)\S+"
  9. reg2.Global = True
  10. Dim fso, objFile, ext
  11. Set fso = CreateObject("Scripting.FileSystemObject")
  12. For Each objFile In fso.GetFolder(".").Files
  13.     ext = fso.GetExtensionName(objFile.Path)
  14.     If LCase(ext) = "txt" Then
  15.         replaceStr objFile.Path
  16.     End If
  17. Next
  18. Function replaceStr(filePath)
  19.     Dim f, s
  20.     Set f = fso.OpenTextFile(filePath)
  21.     s = f.ReadAll
  22.     s = reg1.Replace(s, "$1NAME")
  23.     s = reg2.Replace(s, "$1NAME$2RENAME")
  24.     f.Close
  25.     fso.OpenTextFile(filePath, 2, True).Write(s)
  26. End Function
  27. MsgBox "Done"
复制代码
1

评分人数

TOP

回复 2# WHY
大神您好! 再请教下,如果要把PIN SWAP打头这节的内容都去掉,表达式咋个改?((注意有的文本会有多行数据)
输出为:
(DEVICE FILE: resc1608x50m)

PACKAGE resc1608x50m
CLASS IC
PINCOUNT 2

PINORDER NAME  1 2
FUNCTION NAME RENAME 1 2

END

TOP

本帖最后由 WHY 于 2021-1-5 23:30 编辑

回复 3# loveforjg


    把 PINORDER 和 FUNCTION 之后的字符改名,把 PINSWAP 及之后的多行数据删除,多行数据判断的依据:以逗号+回车换行结束。
  1. Rem On Error Resume Next
  2. Dim reg1
  3. Set reg1 = New RegExp
  4. reg1.Pattern = "\b(PINORDER[ \t]+)\S+"
  5. reg1.Global = True
  6. Dim reg2
  7. Set reg2 = New RegExp
  8. reg2.Pattern = "\b(FUNCTION[ \t]+)\S+([ \t]+)\S+"
  9. reg2.Global = True
  10. Dim reg3
  11. Set reg3 = New RegExp
  12. reg3.Pattern = "\bPINSWAP[ \t](?:[^\r\n,]|,\r\n)+(?:\r\n)?"
  13. reg3.Global = True
  14. Dim fso, objFile, ext
  15. Set fso = CreateObject("Scripting.FileSystemObject")
  16. For Each objFile In fso.GetFolder(".").Files
  17.     ext = fso.GetExtensionName(objFile.Path)
  18.     If LCase(ext) = "txt" Then
  19.         replaceStr objFile.Path
  20.     End If
  21. Next
  22. Function replaceStr(filePath)
  23.     Dim f, s
  24.     Set f = fso.OpenTextFile(filePath)
  25.     s = f.ReadAll
  26.     s = reg1.Replace(s, "$1NAME")
  27.     s = reg2.Replace(s, "$1NAME$2RENAME")
  28.     s = reg3.Replace(s, "")
  29.     f.Close
  30.     fso.OpenTextFile(filePath, 2, True).Write(s)
  31. End Function
  32. MsgBox "Done"
复制代码
1

评分人数

TOP

回复 4# WHY
谢谢 膜拜大神!
FUNCTION的表达式为何后面多了一截 “([ \t]+)\S+”,有何讲究?

TOP

返回列表