如何从可能的参数中拆分命令行应用程序路径? [英] How to split commandline application path from possible arguments?

查看:74
本文介绍了如何从可能的参数中拆分命令行应用程序路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有包含应用程序路径的命令行字符串,并且可能包含参数.一些示例:

I have commandline strings that contains application paths and might contain arguments. Some examples:

1." C:\ Program Files \ Microsoft Office \ winword.exe" C:\ some dir \ documentname.doc"
2." C:\ Program Files \ Some Dir \ app .exe"/dosomething
3.C:\ Program Files \ Some Dir \ somefile.exe
4.&C:\ somedir \ somefile.exe/任何
5.&C: \ Windows \ notepad.exe
6. someapp.exe

1. "C:\Program Files\Microsoft Office\winword.exe" "C:\some dir\documentname.doc"
2. "C:\Program Files\Some Dir\app.exe" /dosomething
3. C:\Program Files\Some Dir\somefile.exe
4. C:\somedir\somefile.exe /whatever
5. C:\Windows\notepad.exe
6. someapp.exe

我正在寻找一种简单的方法来拆分应用程序的路径和可选参数.
有时,字符串包含路径(1& 2)周围的引号,有时,即使路径包含空格,字符串不包含引号(3).有时,字符串在路径(4& 5& 6)中不包含空格,而是在arguments(4)之前使用空格. (6)是没有路径的可执行文件.我必须使用environmentvariables查找可执行文件的完整路径.此字符串(6)也可以带有参数.

I'm looking for a simple way to split the path to the application and the optional argument.
Sometimes the string contains quotes around the path (1&2) and sometimes, even when the path contains spaces, the string does not contain quotes(3). And sometimes the string does not contain spaces in the path (4&5&6), but a space is used before the argument(4). And (6) is a executable without a path. I have to use the environmentvariables the find the fullpath of the executable. This string (6) can also have arguments.

是否有可用于此拆分的标准功能?

Is there a standard function available to do this split?

注意:
我有一个正则表达式模式可以处理(1,2,4,5,6),但不适用于(3),因为其中有空格
"[] +(?=(?:[^"] *""[^"] *")*(?![^"] *"))"
I无法控制字符串.其中一些来自用户输入,另一些来自注册表.

Note:
I have a regex pattern to handle (1,2,4,5,6), but is doesn't work for (3), because of the spaces in the path.
"[ ]+(?=(?:[^""]*""[^""]*"")*(?![^""]*""))"
I cannot control the strings. Some of them come from user input and some of them come from the registry.

推荐答案

 

 

 

如果您只想找到应用程序的路径

If you just want to find the application's path

Application.path将有所帮助

Application.path will help

如果您想要更多....,这就是我的处理方法...

If you want more .... Here's how I may approach it...

作为字符串的昏暗路径=  "C:\ Program Files \ Microsoft Office \ winword.exe""C:\ some dir \ documentname.doc"

Dim path as string =  "C:\Program Files\Microsoft Office\winword.exe" "C:\some dir\documentname.doc"

引号短

缩小空格

对于我来说,path.length-1的short = 0

for i as short = 0 to path.length-1

 选择大小写path.substring(i,1)

  select case path.substring(i,1)

     情况""

       case """"

       quotes + = 1

       quotes += 1

     情况"

       case " "

      空格+ = 1

       spaces += 1

   结束选择

    end select 

下一个

选择大小写空格

  情况0

    case 0

      然后您可以检查报价计数等

        then you can check the quote count etc

    情况2

      case 2

    案例n

      case n

结束选择


这篇关于如何从可能的参数中拆分命令行应用程序路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆