Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

objFile.Name & Korean Characters

Status
Not open for further replies.

Keyster86

IS-IT--Management
Jul 23, 2008
50
US
OK, I am not Korean; however, I am a United States soldier over here in South Korea. I have a portion of my script that is not working right and would like some input.

When my script (a backup script) gets to a file that is named with Korean Characters, I received the below error.

My code is below as well.

What I want is a way to list the filename [in a log file] after it copied the file; any suggestions?

---Error---
8/1/2008 12:18:04 PM: There was an error.
Error Description: Invalid procedure call or argument
Error Number: 5
Error Source: Microsoft VBScript runtime errorError


---Code---
strLogFileName = objFile.Name
LogIt("Backed up file # " & intFileCount & " " & RepSource & "\" & strLogFileName)
Set strLogFileName = Nothing

V/r,

SPC Key
United States Army
 
[0] First with a side-note.
>Set strLogFileName = Nothing
This is conceptually incorrect, though no runtime and no damaging consequence. If you really want to reset some scalar variable, assign it empty or null, even it is rarely needed.
[tt]strLogFileName=null 'or =empty[/tt]

[1] The main problem is probably resulted from your LogIt() with which detail is not shown. If Logit() use either a global or local textstream object opened without explicitly assigned the "format" parameter, it would be defaulted to 0 (ascii). You _must_ put that parameter to -1 (unicode) (or -2 for NT-series system default unicode).
 
Here is my LogIt Function...

What would I need to change to make it support the "format" parameter?


Function LogIt(strText)
strDirectory = "<Directory Path>"
strFile = "\<filename>"

Set objFSO = CreateObject("Scripting.FileSystemObject")

If objFSO.FolderExists(strDirectory) Then
Set objFolder = objFSO.GetFolder(strDirectory)
Else
Set objFolder = objFSO.CreateFolder(strDirectory)
End If

If objFSO.FileExists(strDirectory & strFile) Then
Set objFolder = objFSO.GetFolder(strDirectory)
Else
Set objFile = objFSO.CreateTextFile(strDirectory & strFile)
End If

Set objFile = nothing
Set objFolder = Nothing

Set objTextFile = objFSO.OpenTextFile _
(strDirectory & strFile, ForAppending, True)

objTextFile.WriteLine(strText)
objTextFile.Close

'Free memory
Set strDirectory = Nothing
Set strFile = Nothing
Set objFSO = Nothing
Set objTextFile = Nothing
End Function

V/r,

SPC Key
United States Army
 
Thank-you-so-much! :D

V/r,

SPC Key
United States Army
 
Yes! You are right - I had the wrong reference listed. Anyways, so the changes would like something like this code (below)?


Red text reflects changes...
Code:
Function LogIt(strText)

[COLOR=red]Const TristateTrue = -1[/color]

    strDirectory = "<Directory Path>"
    strFile = "\<filename>"

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    If objFSO.FolderExists(strDirectory) Then
           Set objFolder = objFSO.GetFolder(strDirectory)
    Else
          Set objFolder = objFSO.CreateFolder(strDirectory)
    End If

    If objFSO.FileExists(strDirectory & strFile) Then
           Set objFolder = objFSO.GetFolder(strDirectory)
    Else
           Set objFile = objFSO.CreateTextFile(strDirectory & strFile)
    End If

    Set objFile = nothing
    Set objFolder = Nothing

    Set objTextFile = objFSO.OpenTextFile _
        (strDirectory & strFile, ForAppending, True[COLOR=red], TristateTrue[/color])

    objTextFile.WriteLine(strText)
    objTextFile.Close

    'Free memory
    Set strDirectory = Nothing
    Set strFile = Nothing
    Set objFSO = Nothing
    Set objTextFile = Nothing
End Function

V/r,

SPC Key
United States Army
 
[2] I keep a _very_ open mind to dim or not dim a variable in the main part; but in a sub or function, I might like to see more rigor, though no reason to be dogmatic, as the sub/function might be re-used some day and with some caprice or fausse rigor, people put option explicit in the main... and another main reason for doing so in sub/function is the necessity to maintain the scope of variable. For this latter reason, I would prefer to see the rigor.

[3] This is the revised version containing some practice you have to discover the reasons major or minor.
[tt]
Function LogIt(strText)

Const TristateTrue = -1
[red]Const ForAppending = 8[/red]

[blue]dim strDirectory, strFile, objFSO, objTextFile[/blue]

strDirectory = "<Directory Path>"
strFile = "\<filename>"

Set objFSO = CreateObject("Scripting.FileSystemObject")

If [red]not[/red] objFSO.FolderExists(strDirectory) Then
'Make sure only the last folder does not exist
'You cannot createfolder with missing chain in one go; the line below is shaky and have to be improved.
objFSO.CreateFolder strDirectory
End If

'the whole section is not needed, as the parameter True would work for you

Set objTextFile = objFSO.OpenTextFile _
(strDirectory & strFile, ForAppending, True, TristateTrue)

objTextFile.WriteLine(strText)
objTextFile.Close

'Free memory
[red]'[/red]Set strDirectory = Nothing
[red]'[/red]Set strFile = Nothing
'if you want to do this clean up, do it in reverse order of its dependency
'it is not straightly necessary; it becomes more material for some web-based script
Set objTextFile = Nothing
Set objFSO = Nothing
End Function
[/tt]
[4] For logging message, the writing is often very intensive. Hence, the checking of the existence of the directory should better be taken out as a maintenamce instead of checking and creating scripted inside it. (As if need to create, it only has to do once.) Furthermore, you have to weight between open the text file once and do all the writing before closing it or each time repeat the open-write-close cycle. There are pros and cons for each method. Your script takes the second option.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top