cmd批处理中的自动完成路径

时间:2016-07-09 11:40:56

标签: windows batch-file cmd

当您不确定正确的路径或文件名时,Windows CMD提示符的自动完成(其他终端中的类似功能)非常方便。

底线是,如何在批处理脚本中使用此功能。
示例:
脚本
"C:\Program Files\Java\ jre1.8.0_92 \bin\javaw.exe" -jar post.jar
这里的事情是java版本并不总是相同的,所以它需要像这样的东西  "C:\Program Files\Java\ jre* \bin\javaw.exe" -jar post.jar

2 个答案:

答案 0 :(得分:2)

你不能在路径中间使用通配符,但你可以在最后(最后一个元素)。因为你需要它,分开它:

for /f "delims=" %%a in ('dir /b /ad /on "C:\Program Files\Java\jre*"') do set ver=%%a
set "exec=C:\Program Files\Java\%ver%\javaw.exe"
"%exec%" -jar post.jar

如果有多个版本号,这将为您提供最高版本号的路径。

答案 1 :(得分:0)

Wildcards are supported by only some commands。此外,cmd仅在路径 leaf 中限制文件路径中的通配符,即在最后反斜杠后面的令牌中...

未知 Windows系统上:如果您无法控制环境变量,则需要查找文件路径,例如如下(注意_checkPath变量赋值已更改为合理输出,因为我没有安装java

@ECHO OFF
SETLOCAL EnableExtensions DisableDelayedExpansion
    rem assign path with a wildcard * not in path leaf 
set "_checkPath=C:\Program Files\Java\jre*\bin\javaw.exe"
    rem delete or comment-up next line i.e. my test data      set "_checkPath=..." 
set "_checkPath=%ProgramFiles%*\Microsoft SQL Server\1*\LocalDB\Binn\sqlservr%1.exe"
set "_itemFirst="
    rem previous line: delete variable; next line: show setting
set _checkPath
echo(---
    rem next two lines: ensure that a wildcard * not in path leaf is not allowed
echo dir /B /S "%_checkPath%"
dir /B /S "%_checkPath%"
echo(---
    rem find all files by parsing powershell output ( note proper escaping ^^^| )
for /F "usebackq tokens= 1* delims=: " %%G in (`
  powershell -C Get-ChildItem '"%_checkPath%"' -Recurse ^^^| Format-List -Property FullName
  `) do (
        rem assign first found item
    if not defined _itemFirst set "_itemFirst=%%~H"
        rem assign every found item thus last found item
    set "_item_Last=%%~H"
        rem show every found item
    echo .exe found %%~H
)
echo(---
    rem show setting found
set _item
echo(---
if defined _itemFirst (
    echo success: "%_checkPath%"
        rem commands are merely ECHOed and (my test data) commented up 
        rem use found items (choose any)
    rem ECHO "%_itemFirst%" -jar post.jar
    rem ECHO "%_item_Last%" -jar post.jar
) else (
    echo NOT FOUND "%_checkPath%"
)

上面的代码片段在必要时进行了丰富的评论,以便更好地理解。
输出显示两种变体:

==> D:\bat\SO\38281447.bat XXX
_checkPath=C:\Program Files*\Microsoft SQL Server\1*\LocalDB\Binn\sqlservrXXX.exe
---
dir /B /S "C:\Program Files*\Microsoft SQL Server\1*\LocalDB\Binn\sqlservrXXX.exe"
The filename, directory name or volume label syntax is incorrect.
---
---
Environment variable _item not defined
---
NOT FOUND "C:\Program Files*\Microsoft SQL Server\1*\LocalDB\Binn\sqlservrXXX.exe"

==> D:\bat\SO\38281447.bat
_checkPath=C:\Program Files*\Microsoft SQL Server\1*\LocalDB\Binn\sqlservr.exe
---
dir /B /S "C:\Program Files*\Microsoft SQL Server\1*\LocalDB\Binn\sqlservr.exe"
The filename, directory name or volume label syntax is incorrect.
---
.exe found C:\Program Files\Microsoft SQL Server\100\LocalDB\Binn\sqlservr.exe
.exe found C:\Program Files\Microsoft SQL Server\110\LocalDB\Binn\sqlservr.exe
.exe found C:\Program Files\Microsoft SQL Server\120\LocalDB\Binn\sqlservr.exe
---
_itemFirst=C:\Program Files\Microsoft SQL Server\100\LocalDB\Binn\sqlservr.exe
_item_Last=C:\Program Files\Microsoft SQL Server\120\LocalDB\Binn\sqlservr.exe
---
success: "C:\Program Files*\Microsoft SQL Server\1*\LocalDB\Binn\sqlservr.exe"

==>