2014-12-09 49 views
0

我有一個包含以下數據的文本文件中替換字符串中的一個文件面對的問題使用VBScript

mkdir language 

這裏是我的VBScript其替代語言的字符串文本文件

Const ForReading = 1 
Const ForWriting = 2 

Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objFile = objFSO.OpenTextFile("C:\Scripts\Text.txt", ForReading) 

strText = objFile.ReadAll 
objFile.Close 
strNewText = Replace(strText, "language", "english,french,spanish") 

Set objFile = objFSO.OpenTextFile("C:\Scripts\Text.txt", ForWriting) 
objFile.WriteLine strNewText 
objFile.Close 

我得到輸出mkdir英文,法文,西班牙文,但我想要在文本文件中輸出如下所示

mkdir english 
mkdir french 
mkdir spanish 

How to a chieve這個請大家幫忙

回答

0

這將工作太...

Const ForReading = 1 
Const ForWriting = 2 

Dim langs 
langs = Array("english","french","spanish") 

Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objFile = objFSO.OpenTextFile("Text.txt", ForReading) 

strText = objFile.ReadAll 
objFile.Close 
tempTxt = "" 
strNewText = "" 
For Each i in langs 
    tempTxt = "mkdir " + i + vbCrLf 
    strNewText = strNewText + tempTxt 
Next 

strNewText = Replace(strText, "mkdir language", strNewText) 

Set objFile = objFSO.OpenTextFile("Text.txt", ForWriting) 
objFile.WriteLine strNewText 
objFile.Close 
+0

感謝您的快速回復。它解決了我的問題 – user3727850 2014-12-10 03:42:50

0

這裏嘗試這種變化:

Const ForReading = 1 
Const ForWriting = 2 

Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objFile = objFSO.OpenTextFile("C:\Scripts\Text.txt", ForReading) 

strText = objFile.ReadAll 
objFile.Close 
Set objFile = objFSO.OpenTextFile("C:\Scripts\Text.txt", ForWriting) 

For Each strLanguage In Array("english", "french", "spanish") 
    objFile.WriteLine Replace(strText, "language", strLanguage) 
Next 

objFile.Close 
0

使用Join從陣列構建一個字符串:

>> c = "mkdir " 
>> l = Split("english,french,spanish", ",") 
>> WScript.Echo c & Join(l, vbCrLf & c) 
>> 
mkdir english 
mkdir french 
mkdir spanish 

當然,c可能來自一個文件。

(但我懷疑你的簡化樣品中沒有顯示你的真實世界中的問題)