从 VBScript 调用任何具有俄语名称的文件/脚本失败

发布于 2024-11-03 09:04:46 字数 3675 浏览 0 评论 0原文

我正在从 utf-8 编码的 XML 文件中读取文件/脚本的名称。然后将其传递给 VBScript。 VBScript 启动第二个程序,该程序/脚本作为参数提供给该程序。当参数为英文时,VBScript 会成功执行。

但是,如果从 XML 读取的名称不是英语(在我的例子中是俄语),VBScript 将无法找到该文件。

我只使用“cscript”从 Java 代码运行 VBScript,因为我在 Windows 上运行它。

但是,如果我复制 Java 程序触发的命令来运行 VBScript,并将其粘贴到命令提示符上,则尽管参数名称是非英语,它仍会正常执行。

然后我在 VBScript 中对文件/脚本名称进行硬编码。将VBScript的编码更改为UCS2-LE,并直接从命令提示符运行它。就正常执行了。对于用于 VBScript 的任何其他编码,它都无法执行。非英文文本也显示为 ?除 UCS2-LE 之外的任何其他编码。

然后我尝试在Java中将文件/脚本名称编码为UTF16-LE,然后将其传递给VBScript。无论 VBScript 使用哪种编码,它都会失败。同样,如果我从 Java 程序复制标准输出上打印的命令并从 cmd 运行它,它就会执行。 从 Java 打印的命令可以正确显示非英文文本。

谁能帮我解决这个问题吗? 任何相关帮助将不胜感激。

这就是我目前正在做的事情。我需要将包含俄语文本的参数从 Java 传递给 VBScript。

我尝试使用两种不同的方法。

下面代码中的第一种方法使用 UnicodeLittle 编码将俄语文本写入文件中。发现文件采用 UCS-2LE 编码。然后 VBScript 从该文件中读取值,脚本成功执行。

在第二种方法中,我尝试直接将编码的俄语文本作为参数传递给脚本。 VbScript 失败,提示脚本无法打开。这是我想要的解决方案。

下面是附加的 Java 代码。

任何帮助将不胜感激。

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 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....
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

删除会话 2024-11-10 09:04:46

我之前通过使用短 8.3 样式文件名而不是长文件名解决了类似的问题。我使用 FileSystemObjectShortPath 方法获得这个短名称。这是一个 VBScript 示例...您可能想在 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

例如,

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

返回C:\B373~1,它可以用来代替包含非英文字符的长文件名。使用 dir /x (显示短文件名)和记事本的示例:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文