2016-11-23 41 views
-1

我的問題是,我想在記事本中打開一個文件,然後從記事本保存它,而不是手動,但使用VBScript(我想這是爲了克服一些編碼問題)。到現在爲止,我找到了一個相關article這應該可以解決我的問題,但它不起作用。它有這樣的代碼:用記事本打開一個文件,並保存它,使用VBScript

Dim notepad, wndNotepad, strDesktop 

Set notepad = Sys.Process("notepad") 
Set wndNotepad = notepad.Window("Notepad") 

' Open a file in Notepad 
wndNotepad.MainMenu.Click("File|Open...") 
notepad.Window("#32770", "Open").OpenFile "C:\Program Files\SmartBear\TestComplete 12\install.txt" 

' Save the file to the desktop 
strDesktop = WshShell.SpecialFolders("Desktop") 
wndNotepad.MainMenu.Click "File|Save as..." 
notepad.Window("#32770", "Save As").SaveFile strDesktop & "\install.txt" 

的問題是,VBScript中無法識別Sys系統在第二行(Sys.Process)。我假設在這一行中應該創建一個記事本的過程(like here),然後像這個過程的一個對象返回到變量記事本(我不知道是否以及如何可以實現)以便成爲用於第三行(notepad.Window("Notepad"))。

如果任何人有任何想法如何工作的代碼,我會非常感謝幫助。還有其他關於如何解決這個問題的建議(或者關於它是否可以真正解決的任何想法)都非常受歡迎。感謝您的時間。

編輯

正如我在下面的評論說,上面的代碼需要TestComplete軟件,這是昂貴的。因此,如果任何人有任何想法以另一種方式解決我的問題(其他軟件,其他代碼,其他編程語言),我很樂意學習它。

+1

我會用一個文本流。將Charset屬性設置爲您的文件,讀取文件,更改charset並寫回。 https://msdn.microsoft.com/en-us/library/ms675032(v=vs.85).aspx – 2016-11-23 07:28:34

+1

簡短答案是Sys.Process不在任何Windows本機庫中,它需要額外的庫。可能想要搜索谷歌找出哪個庫(DLL)有這些進程和功能。 –

+0

您發佈的代碼是無效的VBScript。此外,你爲什麼要在記事本中打開一個文本文件,只保存在一個不同的名字中?只需複製文件。解決編碼問題:發表一個描述實際問題的問題。 –

回答

0

我認爲你需要將文件從一個路徑複製到另一個路徑。請參考下面的代碼複製文件,

功能的CopyFile()

strSourceFile = "c:\.....\source.txt" 
strDestFile = "c:\.....\dest.txt" 

Set fso = CreateObject("Scripting.FileSystemObject") 
'Check to see if the file is read-only 
If Not fso.GetFile(DestinationFile).Attributes And 1 Then 
    'The file exists and is not read-only. Safe to replace the file. 
    fso.CopyFile strSourceFile, strDestFile, strOverWrite 
Else 
    'The file exists and is read-only. 
    'Remove the read-only attribute 
    fso.GetFile(DestinationFile).Attributes = fso.GetFile(DestinationFile).Attributes - 1 
    'Replace the file 
    fso.CopyFile strSourceFile, strDestFile, strOverWrite 
    'Reapply the read-only attribute 
    fso.GetFile(DestinationFile).Attributes = fso.GetFile(DestinationFile).Attributes + 1 
End If 

端功能

+0

感謝您的回答,但我希望代碼打開記事本,然後用記事本保存。原因是我在兩個文件的編碼中看到的差異(保存記事本後的原始文件和文件)。最後,我發現這不是必需的,並不能解決我編碼方面的問題。 – Thanasis

相關問題