Unicode 命名的文件夹显示?在 wscript 提示符下

发布于 2024-10-06 09:37:23 字数 1102 浏览 0 评论 0 原文

我遇到 Unicode 命名文件夹的问题。当我将文件夹拖到脚本中时,它没有正确显示文件夹的路径。

简单的VBScript(这只是其中的一部分):

Dim Wshso : Set Wshso = WScript.CreateObject("WScript.Shell")
Dim FSO : Set FSO = CreateObject("Scripting.FileSystemObject")

If WScript.Arguments.Count = 1 Then
    If FSO.FileExists(Wscript.Arguments.Item(0)) = true and FSO.FolderExists(Wscript.Arguments.Item(0)) = false Then
        Alert "You dragged a file, not a folder! My god." & vbcrlf & "Script will terminate immediately", 0, "Alert: User is stupid", 48
        WScript.Quit
    Else
        targetDir = WScript.Arguments.Item(0)
        Wshso.Popup targetDir
    End If
Else
    targetDir = Wshso.SpecialFolders("Desktop")
    Alert "Note: No folder to traverse detected, default set to:" & vbcrlf & Wshso.SpecialFolders("Desktop"), 0, "Alert", 48
End If

如果是没有Unicode字符的普通路径就可以了。但在这种情况下: 目录:4Minute (포미닛) - Hit Your Heart

然后它会显示类似 4Minute (?) - Hit Your Heart

如果我做一个FolderExists它找不到拖动的文件夹。

是否有任何解决方法来支持 Unicode 命名文件夹?

谢谢!

如果这还不够清楚,我会编辑

I am facing problems with Unicode named folders. When I drag the folder to the script, it doesn't show the path of the folder properly.

Simple VBScript (this is just a portion of it):

Dim Wshso : Set Wshso = WScript.CreateObject("WScript.Shell")
Dim FSO : Set FSO = CreateObject("Scripting.FileSystemObject")

If WScript.Arguments.Count = 1 Then
    If FSO.FileExists(Wscript.Arguments.Item(0)) = true and FSO.FolderExists(Wscript.Arguments.Item(0)) = false Then
        Alert "You dragged a file, not a folder! My god." & vbcrlf & "Script will terminate immediately", 0, "Alert: User is stupid", 48
        WScript.Quit
    Else
        targetDir = WScript.Arguments.Item(0)
        Wshso.Popup targetDir
    End If
Else
    targetDir = Wshso.SpecialFolders("Desktop")
    Alert "Note: No folder to traverse detected, default set to:" & vbcrlf & Wshso.SpecialFolders("Desktop"), 0, "Alert", 48
End If

If it is a normal path without Unicode characters, it's fine. But in this case:
Directory: 4minute (포미닛) - Hit Your Heart

Then it will show something like 4minute (?) - Hit Your Heart

And if I do a FolderExists it can't find the dragged folder.

Is there any workaround to support Unicode named Folders?

Thanks!

I'll edit if this is not clear enough

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

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

发布评论

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

评论(4

千と千尋 2024-10-13 09:37:23

这似乎确实是 Windows 脚本宿主 DropHandler shell 扩展。然而:

test.vbs "C:\포미닛.txt"
C:\WINDOWS\System32\WScript.exe "test.vbs" "C:\포미닛.txt"

当从控制台键入时,两者都可以工作(即使控制台无法呈现韩文,因此它看起来像 ?),拖放操作应该会导致相同的命令通过 Unicode ->ANSI->Unicode 转换会丢失当前 ANSI 代码页中不存在的所有字符。 (因此 포미닛 将适用于默认的韩语 Windows 安装,但不适用于西语。)

我不知道解决该问题的正确方法。您也许可以通过更改注册表中 .vbs 文件的 DropHandler 来解决此问题:

HKEY_CLASSES_ROOT\VBSFile\ShellEx\DropHandler\(Default)

从 WSH DropHandler ({60254CA5-953B-11CF-8C96-00AA00B8708C}) 到 {86C86720-42A0 -1069-A2E8-08002B30309D},用于 .exe、.bat 和类似文件的,不会出现此问题。您可能还需要更改 .vbs 的文件关联,以便在文件名参数周围加上引号,因为 EXE DropHandler 不会这样做,以避免文件名中出现空格问题。

由于这会影响所有 VBS 文件的参数传递,因此部署在除您自己的计算机之外的任何计算机上都是一个危险的修复。如果您需要这样做,也许您可​​以尝试使用适当的 DropTarget 创建一个新的文件扩展名,而不是更改 VBSFile 本身?或者也许放弃拖放到脚本上的行为并提供文件打开对话框或手动拖放字段。

This does seem to be a problem peculiar to the Windows Script Host's DropHandler shell extension. Whereas:

test.vbs "C:\포미닛.txt"
C:\WINDOWS\System32\WScript.exe "test.vbs" "C:\포미닛.txt"

both work when typed from the console (even if the console can't render the Hangul so it looks like ?), a drag and drop operation that should result in the same command goes through a Unicode->ANSI->Unicode translation that loses all characters that aren't in the current ANSI code page. (So 포미닛 will work on a default Korean Windows install but not Western.)

I'm not aware of a proper way to fix the problem. You could perhaps work around it by changing the DropHandler for .vbs files in the registry:

HKEY_CLASSES_ROOT\VBSFile\ShellEx\DropHandler\(Default)

from the WSH DropHandler ({60254CA5-953B-11CF-8C96-00AA00B8708C}) to {86C86720-42A0-1069-A2E8-08002B30309D}, the one used for .exe, .bat and similar, which doesn't suffer from this issue. You would also probably have to change the file association for .vbs to put quotes around the filename argument too, since the EXE DropHandler doesn't, to avoid problems with spaces in filenames.

Since this affects argument-passing for all VBS files it would be a perilous fix to deploy on any machine but your own. If you needed to do that, maybe you could try creating a new file extension with the appropriate DropTarget rather than changing VBSFile itself? Or maybe forgo drop-onto-script behaviour and provide a file Open dialog or manual drop field instead.

傲鸠 2024-10-13 09:37:23

对于从 Google 登陆这里的任何人...

Bobince 的提示引导我通过将 vbscript 文件 (myscript.vbs) 包装在 dos 批处理文件 (mybatch.bat) 中来解决此问题。

提示是:

“似乎是 Windows 脚本宿主特有的问题
DropHandler shell 扩展而...用于 .exe、.bat 和
类似...不会遇到这个问题。”

mybatch.bat 包含:

:Loop
IF "%1"=="" GOTO Continue
     set allfiles=%allfiles% "%1"
SHIFT
GOTO Loop
:Continue
"myscript.vbs" %allfiles%

您可能还会发现我的 myscript.vbs 中的这段代码很有帮助

For Each strFullFileName In Wscript.Arguments
  ' do stuff
Next

For anyone landing here from Google...

Bobince's tip lead me to work around this problem by wrapping my vbscript file (myscript.vbs) in a dos batch file (mybatch.bat).

The tip was:

"Seem to be a problem peculiar to the Windows Script Host's
DropHandler shell extension whereas.... the one used for .exe, .bat and
similar... doesn't suffer from this issue."

mybatch.bat contains:

:Loop
IF "%1"=="" GOTO Continue
     set allfiles=%allfiles% "%1"
SHIFT
GOTO Loop
:Continue
"myscript.vbs" %allfiles%

You may also find this code from my myscript.vbs to be helpful

For Each strFullFileName In Wscript.Arguments
  ' do stuff
Next
淡淡の花香 2024-10-13 09:37:23

根据DG的回答,如果您只想接受一个文件作为放置目标,那么您可以编写一个批处理文件(如果您将其命名为“x.bat”,请将文件名为“x.bat.vbs”的VBScript放置在同一文件夹中)仅包含:

@"%0.vbs" %1

@ 表示不在显示屏上输出该行(我发现即使您使用 chcp 1250 作为第一个命令,它也会显示垃圾文本)

不要在 %1 周围使用双引号,如果您的 VBScript 使用如下逻辑(我下面使用的代码来自 http://jeffkinzer.blogspot.com/2012/06/vbscript-to-convert-excel-to-csv.html)。测试了一下,它可以很好地处理文件和文件夹名称中的空格:

  Dim strExcelFileName
  strExcelFileName = WScript.Arguments.Item(0) 'file name to parse

  ' get path where script is running
  strScript = WScript.ScriptFullName
  Dim fso
  Set fso = CreateObject ("Scripting.FileSystemObject") 
  strScriptPath = fso.GetAbsolutePathName(strScript & "\..")
  Set fso = Nothing

  ' If the Input file is NOT qualified with a path, default the current path
  LPosition = InStrRev(strExcelFileName, "\")
  if LPosition = 0 Then 'no folder path
    strExcelFileName = strScriptPath & "\" & strExcelFileName
    strScriptPath = strScriptPath & "\"
  else 'there is a folder path, use it for the output folder path also
    strScriptPath = Mid(strExcelFileName, 1, LPosition)
  End If
  ' msgbox LPosition & " - " & strExcelFileName & " - " & strScriptPath

Based on DG's answer, if you just want to accept one file as drop target then you can write a batch file (if you have it named as "x.bat" place VBScript with filename "x.bat.vbs" at same folder) that just contains:

@"%0.vbs" %1

the @ means to not output the row on the display (I found it to show garbage text even if you use chcp 1250 as first command)

don't use double-quotes around %1, it won't work if your VBScript uses logic like the following (code I was using below was from http://jeffkinzer.blogspot.com/2012/06/vbscript-to-convert-excel-to-csv.html). Tested it and it works fine with spaces in the file and folder names:

  Dim strExcelFileName
  strExcelFileName = WScript.Arguments.Item(0) 'file name to parse

  ' get path where script is running
  strScript = WScript.ScriptFullName
  Dim fso
  Set fso = CreateObject ("Scripting.FileSystemObject") 
  strScriptPath = fso.GetAbsolutePathName(strScript & "\..")
  Set fso = Nothing

  ' If the Input file is NOT qualified with a path, default the current path
  LPosition = InStrRev(strExcelFileName, "\")
  if LPosition = 0 Then 'no folder path
    strExcelFileName = strScriptPath & "\" & strExcelFileName
    strScriptPath = strScriptPath & "\"
  else 'there is a folder path, use it for the output folder path also
    strScriptPath = Mid(strExcelFileName, 1, LPosition)
  End If
  ' msgbox LPosition & " - " & strExcelFileName & " - " & strScriptPath
情深已缘浅 2024-10-13 09:37:23

将 WSH DropHandler ({60254CA5-953B-11CF-8C96-00AA00B8708C}) 修改为 {86C86720-42A0-1069-A2E8-08002B30309D} 并添加此函数以将短路径转换为长路径:

Function Short2Long(shortFullPath)
    dim fs
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFile(shortFullPath)
    Set app = CreateObject("Shell.Application")
    Short2Long = app.NameSpace(f.ParentFolder.Path).ParseName(f.Name).Path
end function

Modify WSH DropHandler ({60254CA5-953B-11CF-8C96-00AA00B8708C}) to {86C86720-42A0-1069-A2E8-08002B30309D} and add this function to convert short path to long:

Function Short2Long(shortFullPath)
    dim fs
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFile(shortFullPath)
    Set app = CreateObject("Shell.Application")
    Short2Long = app.NameSpace(f.ParentFolder.Path).ParseName(f.Name).Path
end function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文