获取批处理文件旁边的exe名称

时间:2023-01-19 18:41:45

I have used this batch script to create shortcut of an .exe but I want to get the .exe name also dynamically. In a folder I have batch file and also one .exe file I want to get that .exe name dynamically in my batch file.

我已经使用此批处理脚本创建.exe的快捷方式,但我想动态获取.exe名称。在一个文件夹中我有批处理文件和一个.exe文件我想在我的批处理文件中动态获取.exe名称。

@echo off

set SCRIPT="%TEMP%\%RANDOM%-%RANDOM%-%RANDOM%-%RANDOM%.vbs"

echo Set oWS = WScript.CreateObject("WScript.Shell") >> %SCRIPT%
echo sLinkFile = "%USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\Player111.lnk" >> %SCRIPT%
echo Set oLink = oWS.CreateShortcut(sLinkFile) >> %SCRIPT%
echo oLink.TargetPath = "%CD%\Pacman.exe" >> %SCRIPT%
echo oLink.Save >> %SCRIPT%

cscript /nologo %SCRIPT%
del %SCRIPT%

I want to set oLink.TargetPath dynamically. It will be the path where the batch file located along with the .exe name. How can I get Pacman.exe as mentioned in above code dynamically? Remember, exactly one .exe is in the folder.

我想动态设置oLink.TargetPath。它将是批处理文件与.exe名称一起定位的路径。如何动态获取上面代码中提到的Pacman.exe?请记住,文件夹中只有一个.exe。

2 个解决方案

#1


1  

To retrieve the full path to the .exe located in the parent directory of the batch file, use the following code:

要检索位于批处理文件的父目录中的.exe的完整路径,请使用以下代码:

for %%F in ("%~dp0*.exe") do set "EXEFILE=%%~fF"

(Note: To use the current working directory instead, replace %~dp0*.exe by %CD%\*.exe.)

(注意:要使用当前工作目录,请将%~dp0 * .exe替换为%CD%\ * .exe。)

Finally, replace the line echo oLink.TargetPath = "%CD%\Pacman.exe" >> %SCRIPT% by:

最后,将行echo oLink.TargetPath =“%CD%\ Pacman.exe”>>%SCRIPT%替换为:

echo oLink.TargetPath = "%EXEFILE%" >> %SCRIPT%

Here is the full script -- also with some other improvements:

这是完整的脚本 - 还有一些其他改进:

@echo off

set "SCRIPT=%TEMP%\%RANDOM%-%RANDOM%-%RANDOM%-%RANDOM%.vbs"

for %%F in ("%~dp0*.exe") do set "EXEFILE=%%~fF"

> "%SCRIPT%" (
    echo Set oWS = WScript.CreateObject^("WScript.Shell"^)
    echo sLinkFile = "%USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\Player111.lnk"
    echo Set oLink = oWS.CreateShortcut^(sLinkFile^)
    echo oLink.TargetPath = "%EXEFILE%"
    echo oLink.Save
)

cscript //NoLogo "%SCRIPT%"
del "%SCRIPT%"

#2


1  

to get a filename to a variable (assuming there is only one match):

获取变量的文件名(假设只有一个匹配):

for %%a in ("%~dp0*.exe") do set "file=%%a"
echo %file%

and later

echo oLink.TargetPath = "%file%" >> %SCRIPT%

#1


1  

To retrieve the full path to the .exe located in the parent directory of the batch file, use the following code:

要检索位于批处理文件的父目录中的.exe的完整路径,请使用以下代码:

for %%F in ("%~dp0*.exe") do set "EXEFILE=%%~fF"

(Note: To use the current working directory instead, replace %~dp0*.exe by %CD%\*.exe.)

(注意:要使用当前工作目录,请将%~dp0 * .exe替换为%CD%\ * .exe。)

Finally, replace the line echo oLink.TargetPath = "%CD%\Pacman.exe" >> %SCRIPT% by:

最后,将行echo oLink.TargetPath =“%CD%\ Pacman.exe”>>%SCRIPT%替换为:

echo oLink.TargetPath = "%EXEFILE%" >> %SCRIPT%

Here is the full script -- also with some other improvements:

这是完整的脚本 - 还有一些其他改进:

@echo off

set "SCRIPT=%TEMP%\%RANDOM%-%RANDOM%-%RANDOM%-%RANDOM%.vbs"

for %%F in ("%~dp0*.exe") do set "EXEFILE=%%~fF"

> "%SCRIPT%" (
    echo Set oWS = WScript.CreateObject^("WScript.Shell"^)
    echo sLinkFile = "%USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\Player111.lnk"
    echo Set oLink = oWS.CreateShortcut^(sLinkFile^)
    echo oLink.TargetPath = "%EXEFILE%"
    echo oLink.Save
)

cscript //NoLogo "%SCRIPT%"
del "%SCRIPT%"

#2


1  

to get a filename to a variable (assuming there is only one match):

获取变量的文件名(假设只有一个匹配):

for %%a in ("%~dp0*.exe") do set "file=%%a"
echo %file%

and later

echo oLink.TargetPath = "%file%" >> %SCRIPT%