2017-01-17 22 views
1

特定の文字列をファイル名に置き換えるにはどうすればよいですか?例:テキスト "ABC123"を含むサブフォルダ内に、名前が異なる複数のファイル(Test.asp、Constant.asp、Letter.aspなど)があります。私は各ファイルの "ABC123"をファイル名に置き換えたいと思います。 以下は、文字列を見つけて特定の文字列に置き換えたコードですが、上記の仕事はしません。私はあなたに1つのラインRubyのソリューションを与えることができ特定の文字列をファイル名に置き換えますか?

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

、Pythonでそれを翻訳するにはあまりにも難しいが、私は怖いVBScriptでやや広範囲ではないはずです。最初に一般的な検索と置き換えのバージョン。それ保存

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

スクリプトでは、例えばreplace.rb

あなたは私が何が起こっているのかを説明できるように壊れ

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

とコマンドライン(ここではCMD.EXE)上で開始したが引き続き実行可能

# 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にファイル名を指定します。もちろん は、テストの結果、私のTESTFILESの1の

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

内容(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

関連する問題