2017-01-17 3 views
1

Как заменить определенную строку на имя файла? Пример. У меня есть несколько файлов с разными именами (например: Test.asp, Constant.asp, Letter.asp и т. Д.) В подпапке, содержащей текст «ABC123». Я хотел бы заменить «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 
+0

* «это не делает работу, которую я перечислил выше» * довольно общий. Что происходит при запуске этого кода? Вы сталкиваетесь с какими-либо ошибками? Если да, то в какой строке и какие ошибки? –

+0

Этот код находит строку старой строки и заменяет ее на новую строку, перебирая каждую подпапку. – Loic

+0

Внутри 'ReplaceText' заменить' strOldText' на 'objFile.Name'. –

ответ

1

Я могу дать вам одну строку решение на Ruby, должна быть не слишком трудно перевести, что в Python, но несколько более обширны в VBScript Я боюсь. Сначала общий вариант поиска и замены.

ARGV[0..-3].each{|f| File.write(f, File.read(f).gsub(ARGV[-2],ARGV[-1]))} 

сохранить его в сценарии, например replace.rb

Вы начинаете в командной строке (здесь cmd.exe) с

replace.rb *.txt <string_to_replace> <replacement> 

разбитого вниз так, что я могу объяснить, что происходит, но все еще исполняемый

# ARGV is an array of the arguments passed to the script. 
ARGV[0..-3].each do |f| # enumerate the arguments of this script from the first to the last (-1) minus 2 
    File.write(f, # open the argument (= filename) for writing 
    File.read(f) # open the argument (= filename) for reading 
    .gsub(ARGV[-2],ARGV[-1])) # and replace all occurances of the beforelast with the last argument (string) 
end 

И, наконец, ваш запрос на замену ABC123 с именем файла. Конечно испытания и работы

ARGV[0..-1].each{|f| File.write(f, File.read(f).gsub('ABC123', f))} 

Содержание одного из моих testfiles (1.TXT) после выполнения

test phrase 
1.txt 

EDIT

Я вижу, что вы хотите подпапку рекурсии на фиксированной папке нет проблема

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

Спасибо. Это сработало. – Loic

Смежные вопросы