开发者

Calling any file/script with Russian name from VBScript fails

开发者 https://www.devze.com 2023-02-28 22:46 出处:网络
I am reading a name of the file/script from an utf-8 encoded XML file. This is then passed to VBScript. VBScript starts a second program to which this passed program/script is provided as an argument.

I am reading a name of the file/script from an utf-8 encoded XML file. This is then passed to VBScript. VBScript starts a second program to which this passed program/script is provided as an argument. When the argument is in English, the VBScript executes successfully.

But if the name read from XML is non-english( Russian in my case ), VBScript fails to find that file.

I am running VBScript from Java code only using "cscript" as I am running it on Windows.

However, if I copy the command fired by Java program to run VBScript, and paste it on command prompt, it executes normally despite the argument name is in non-english language.

I then hardcoded file/script name in VBScript. Changed encoding of VBScript to UCS2-LE, and directly run it from command prompt. It executed normally. It failed to execute for any other encoding used for VBScript. Also the non-english text is displayed as ? in any other encoding than UCS2-LE.

Then I tried to encode file/script name into UTF16-LE in Java and then passed it to VBScript. Irrespective of which encoding was used in VBScript, it fails. Again if I copy the command printed on standard output from Java program and run it from cmd, it executes. The command printed from Java displays non-english text correctly.

Can anyone please help me to resolve the issue? Any relative help would be greatly appreciated.

This is what I am doing currently. I need to pass an argument contatining Russian Text to VBScript from Java.

I tried to use two different approaches.

First approach in the code below writes the Russian text in a file using encoding UnicodeLittle. File is found to be in encoding UCS-2LE. And then VBScript reads the value from that file, and script is executed successfully.

In second approach, I tried to directly pass encoded Russian text as argument to script. VbScript fails saying that script can't be opened.This is the approach I want solution for.

Below is the Java code attached.

Any help would be greatly appreciated.

public class CallProgram
{
     private static String encodeType     = "UnicodeLittle";
private File scriptName          = new File( "F:\\Trial Files\\scriptName.txt" );

public static void main(String[] args) 
{
    CallProgram obj          = new CallProgram();
    Runtime rt           = Runtime.getRuntime();

    try 
    {
        **//Approach1 - Writes text to file and calls vbscript which reads text from file and uses it as an argument to a program**

        String sName    =  "D:\\CheckPoints_SCRIPTS\\Менеджер по качеству";  //Russian Text

        byte [] encodedByte= sName.getBytes( encodeType ); 

        String testCase = new String( encodedByte, encodeType ); //New string containing russian text in UnicodeLittle encoding...

        obj.writeToFile( testCase ); //Writing russian string to file...

        String mainStr  = "cscript /nologo \"D:\\Program Files\\2.0.1.3\\Adapter\\bin\\scriptRunner_FileRead_Write.vbs\"";

                        Process proc1   = rt.exec( mainStr );
        int exit        = proc1.waitFor();
        System.out.println( "Exit Value = " + exit );

                       **//Approach 2 - Passing encoded Russian text directly to VbScript...**
//This is not working for me...
        String [] arrArgs   = { "cscript", "/nologo", "\"D:\\Program Files\\IBM\\Rational Adapters\\2.0.1.3\\QTPAdapter\\bin\\scriptRunner.vbs\"", testcase };

        ProcessBuilder process     = new ProcessBuilder( arrArgs );
                     Process proc2      = process.start();
        proc2.waitFor();

              }
    catch (IOException e) 
    {
        e.printStackTrace();
    }
    catch ( InterruptedException intue ) 
    {
        intue.printStackTrace();
    } 
}

     //Function to write Russian text to file using encoding UnicodeLittle...
private void writeToFile( String testCase ) 
{
    FileOutputStream fos    = null;
    Writer out      = null;
    try 
    {
       fos          = new FileOutputStream( this.scriptName );
       out          = new OutputStreamWriter( fos, encodeType );
           out.write( testCase );
       out.close();
       fos.close();
    }
    catch (FileNotFoundException e) 
    {
       e.printStackTrace();
    }
    catch ( IOException ioe )
    {
       ioe.printStackTrace();
    }
    finally
    {
        try
        {
            if ( fos != null )
            {
 开发者_开发技巧               fos.close();
                fos = null;
            }

            if ( out != null)
            {
                out.close();
                out = null;
            }
        }
        catch( IOException ioe )
        {
            fos = null;
            out = null;
        }
    }
} // End of method writeToFile....
}


I've resolved similar problems before by using the short 8.3-style filename instead of the long filename. I get this short name using the ShortPath method of FileSystemObject. Here's a VBScript example... you may want to try something similar in Java.

Function GetShortPath(strLongPath)

    Dim FSO 
    Dim objFolder 
    Dim objFile 
    Dim strShortPath 

    Set FSO = CreateObject("Scripting.FileSystemObject")

    ' Is it a file or a folder?
    If FSO.FolderExists(strLongPath) Then
       ' It's a folder.
       Set objFolder = FSO.GetFolder(strLongPath)
       strShortPath = objFolder.ShortPath
    ElseIf FSO.FileExists(strLongPath) Then
       ' It's a file.
       Set objFile = FSO.GetFile(strLongPath)
       strShortPath = objFile.ShortPath
    Else
        ' File not found.
        strShortPath = ""
    End If

    GetShortPath = strShortPath

End Function

For example,

Debug.Print GetShortPath("C:\öêåéèüø.çõâ")

returns C:\B373~1, which can be used in place of the long filename with non-English characters. Example with dir /x (reveals the short filename) and notepad:

C:\sandbox>dir /x
 Volume in drive C has no label.
 Volume Serial Number is BC90-DF37

 Directory of C:\sandbox

13/01/2011  15:12    <DIR>                       .
13/01/2011  15:12    <DIR>                       ..
13/01/2011  14:52                22 NEWTEX~1.TXT New Text Document.txt
13/01/2011  15:05                 0 C7F0~1.TXT   öêåéèüø.txt
13/01/2011  15:05                 0 B373~1       öêåéèüø.çõâ
               3 File(s)             22 bytes
               2 Dir(s)  342,158,913,536 bytes free

C:\sandbox>notepad B373~1
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号