Vbscript 是否用文件名替换特定字符串?

Vbscript 是否用文件名替换特定字符串?,vbscript,Vbscript,如何用文件名替换特定字符串?示例:在包含文本“ABC123”的子文件夹中,我有几个名称不同的文件(如:Test.asp、Constant.asp、Letter.asp等)。我想用文件名替换每个文件中的“ABC123”。 下面是我的代码,它查找字符串并用一个特定的字符串替换它,但它没有完成我上面列出的工作 Option Explicit Dim objFilesystem, objFolder, objFiles, objFile, tFile, objShell, objLogFile,objF

如何用文件名替换特定字符串?示例:在包含文本“ABC123”的子文件夹中,我有几个名称不同的文件(如:Test.asp、Constant.asp、Letter.asp等)。我想用文件名替换每个文件中的“ABC123”。 下面是我的代码,它查找字符串并用一个特定的字符串替换它,但它没有完成我上面列出的工作

Option Explicit
Dim objFilesystem, objFolder, objFiles, objFile, tFile, objShell, objLogFile,objFSO, objStartFolder, colFiles
Dim SubFolder, FileText, bolWriteLog, strLogName, strLogPath, strCount, strCount2, strOldText, strNewText, strEXT
bolWriteLog = True
Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2
Set objFilesystem = WScript.CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
strLogName = "log.txt"
strLogPath = "C:\" & strLogName

strCount = 0
strCount2 = 0
strOldText = "ABC123"
strNewText = ""
strEXT = "asp"

'Initialize log file
If bolWriteLog Then
  On Error Resume Next
  Set objLogFile = objFileSystem.OpenTextFile(strLogPath, 2, True)
  WriteLog "############### Start Log ##################"
  If Not Err.Number = 0 Then
    MsgBox "There was a problem opening the log file for writing." & Chr(10) & _
       "Please check whether """ & strLogPath & """ is a valid file and can be openend for writing." & _
       Chr(10) & Chr(10) & "If you're not sure what to do, please contact your support person.", vbCritical, "Script Error"
    WScript.Quit
  End If
  On Error Goto 0
End If

Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "D:\MyFolder"

Set objFolder = objFSO.GetFolder(objStartFolder)
WScript.Echo objFolder.Path
Set colFiles = objFolder.Files
For Each objFile In colFiles
  'WScript.Echo objFile.Name
  ' Now we have an exception for all files that can not be opened in text modus: all extensions such as "exe" should be listed upfront.
  ReplaceText(objFile) 
Next

ShowSubfolders objFSO.GetFolder(objStartFolder)

Sub ReplaceText(objFile)
  If InStr(1, strEXT, Right(LCase(objFile.Name), 3)) = 0 Or objFile.Size = 0 Then
  Else
    strCount = strCount + 1
    WriteLog("Opening " & objFile.Name)
    Set tFile = objFile.OpenAsTextStream(ForReading, TriStateUseDefault)
    FileText = tFile.ReadAll
    tFile.Close
    If InStr(FileText, strOldText) Then
      WriteLog("Replacing " & strOldText & " with " & strNewText & ".")
      FileText = Replace(FileText, strOldText, strNewText)
      WriteLog("Text replaced")
    Else
      WriteLog(strOldText & " was not found in the file.")
      strCount2 = strCount2 + 1
    End If
    Set tFile = objFile.OpenAsTextStream(ForWriting, TriStateUseDefault)
    tFile.Write FileText
    tFile.Close
    FileText = ""
    strCount = 0
    strCount2 = 0
  End If
End Sub

Sub ShowSubFolders(Folder)
  For Each Subfolder in Folder.SubFolders
    'WScript.Echo Subfolder.Path
    Set objFolder = objFSO.GetFolder(Subfolder.Path)
    Set colFiles = objFolder.Files
    For Each objFile in colFiles
      'WScript.Echo objFile.Name
      ReplaceText(objFile)
    Next
    ShowSubFolders Subfolder
  Next
End Sub

WriteLog "###############  EndLog  ##################"

WScript.Echo "Script Complete"
objShell.Run "C:\" & strLogName

'Clear environment and exit
On Error Resume Next

Set tFile = Nothing
Set objFile = Nothing
Set objFiles = Nothing
Set objFolder = Nothing
Set objLogFile = Nothing
Set objFilesystem = Nothing
Set objShell = Nothing

WScript.Quit

'Subs and functions ********** DO NOT EDIT ***************

Sub WriteLog(sEntry)
  If bolWriteLog Then objLogFile.WriteLine(Now() & ": Log:      " & sEntry)
End Sub

我可以给你一个一行的Ruby解决方案,用Python翻译应该不太困难,但用VbScript恐怕要更广泛一些。首先是一个通用的搜索和替换版本

ARGV[0..-3].each{|f| File.write(f, File.read(f).gsub(ARGV[-2],ARGV[-1]))}
将其保存在脚本中,例如
replace.rb

在命令行(此处为cmd.exe)中使用

最后是将
ABC123
替换为文件名的请求。 当然,测试和工作

ARGV[0..-1].each{|f| File.write(f, File.read(f).gsub('ABC123', f))}
执行后我的一个测试文件(1.txt)的内容

test phrase
1.txt
编辑

我知道你想要固定文件夹上的子文件夹递归,没问题

Dir['**/*'].each{|f| File.write(f, File.read(f).gsub('ABC123', f)) unless File.directory?(f) }

“它不做我上面列出的工作”是相当通用的。运行此代码时会发生什么情况?您是否面临任何错误?如果是,在哪一行和哪一个错误?此代码查找字符串旧字符串,并通过循环每个子文件夹将其替换为新字符串。在
ReplaceText
replace
strOldText
中使用
objFile.Name
。谢谢。它成功了。
test phrase
1.txt
Dir['**/*'].each{|f| File.write(f, File.read(f).gsub('ABC123', f)) unless File.directory?(f) }