'######################################################## '### Writes the content of string into a file ### '### ### '### (c) Dirk Pelzer 2004 ### '######################################################## Option Explicit Dim strFileName Dim strContent Dim intRet strFileName = "C:\Temp\Test.txt" strContent = "Sampletext." intRet = WriteFile(strFileName, strContent) Select Case intRet Case 0 wscript.echo "File " & strFileName & " created successfully." wscript.quit (0) Case 1 wscript.echo "Error: Specified file already exists." wscript.echo " Please delete the file and start again." & vbCRLF wscript.echo "Script aborted." wscript.quit(1) Case 2 wscript.echo "Error: Unable to create file " & strFileName wscript.echo " Please verify that the folder exists and you have sufficient permissions." & vbCRLF wscript.echo "Script aborted." wscript.quit(1) Case Else wscript.echo "Unknown error. Script aborted." wscript.quit (1) End Select '************************************************* ' WriteFile '************************************************* Function WriteFile(strFileName, strContent) 'Writes the strContent into a file specified by strFilename ' WriteFile = 0: sucess ' WriteFile = 1: the specified file already exists ' WriteFile = 2: unable to write to the specified file Const ForWriting = 2 Dim fso, f Set fso = CreateObject("Scripting.FileSystemObject") If (fso.FileExists(strFileName)) Then 'Specified file already exists WriteFile = 1 Exit Function End If On error resume next Set f = fso.OpenTextFile(strFileName, ForWriting, True) f.Write strContent If err.number <> 0 Then 'Error occurred writing to file WriteFile = 2 Set f = Nothing Set fso = Nothing Exit Function End If on error goto 0 Set f = Nothing Set fso = Nothing WriteFile = 0 End Function