多次调用批处理函数时出错

时间:2016-09-01 12:47:11

标签: function batch-file

我正在构建一个小批量脚本,它会移动一些文件并创建三个startmenu条目。问题是关于第30到32行,我称之为“createLink”方法。在这些行中,我调用一个函数来创建startmenu的条目。但只创建了第一个。这三个中的哪一个首先被称为无关紧要。仅创建第一个被调用的条目。这是洞脚本:

@echo off
chcp 1252

set ProgramFilesPath=%ProgramFiles%\Grün2_Mitgliederverwaltung\
set AppDataPath=%AppData%\Grün2_Mitgliederverwaltung\
set MenuEntryFolderPath=%AppData%\Microsoft\Windows\Start Menu\Programs\Grün2 Mitgliederverwaltung\

if not exist "%AppDataPath%" (
    mkdir "%AppDataPath%"
)

move /y "%~dp0Grün2.VisualElementsManifest.xml" "%AppDataPath%"
del "%~dp0*.sh"
del "%~dp0*.desktop"

if not exist "%ProgramFilesPath%" (
    move /y "%~dp0Grün2.conf" "%AppDataPath%"

    call :getAdminrights

    rem "install"
    mkdir "%ProgramFilesPath%"
    move /y "%~dp0*" "%ProgramFilesPath%"
    rem moving directories between drives is not possible
    xcopy /y "%~dp0lib" "%ProgramFilesPath%lib\"
    rd /s /q "%~dp0lib"

    rem create startmenuentries
    mkdir "%MenuEntryFolderPath%"
    call :createLink "Grün2 starten" "%ProgramFilesPath%" "Grün2_Launcher.jar"
    call :createLink "Grün2 deinstallieren" "%ProgramFilesPath%" "uninstall.bat"
    call :createLink "Grün2 konfigurieren" "%AppDataPath%" "Grün2.conf"

    echo.
    echo Programm wurde installiert
) else (
    del "%~dp0Grün2.conf"

    call :getAdminrights

    rem "update"
    move /y "%~dp0*" "%ProgramFilesPath%"
    rem moving directories between drives is not possible
    xcopy /y "%~dp0lib" "%ProgramFilesPath%lib\"
    rd /s /q "%~dp0lib"

    echo.
    echo Programm wurde aktualisiert
)

goto :eof

rem createLink <linkname> <workingdir> <file of workingdir>
:createLink
set SCRIPT="%TEMP%\%RANDOM%-%RANDOM%-%RANDOM%-%RANDOM%.vbs"

echo Set oWS = WScript.CreateObject("WScript.Shell") >> %SCRIPT%
echo sLinkFile = "%MenuEntryFolderPath%%~1.lnk" >> %SCRIPT%
echo Set oLink = oWS.CreateShortcut(sLinkFile) >> %SCRIPT%
echo oLink.TargetPath = "%~2%~3" >> %SCRIPT%
echo oLink.WorkingDirectory = "%~2" >> %SCRIPT%
echo oLink.IconLocation = "%ProgramFilesPath%icon.ico" >> %SCRIPT%
echo oLink.Save >> %SCRIPT%

cscript /nologo %SCRIPT%
del %SCRIPT%
goto :eof


:getAdminrights
REM  --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\icacls.exe" "%SYSTEMROOT%\system32\config\system"

set ADMINSCRIPT="%TEMP%\%RANDOM%-%RANDOM%-%RANDOM%-%RANDOM%.vbs"
REM --> If error flag set, we do not have admin.
if '%ERRORLEVEL%' NEQ '0' (
    echo Set UAC = CreateObject^("Shell.Application"^) > %ADMINSCRIPT%
    set params = %*:"=""
    echo UAC.ShellExecute "cmd.exe", "/c %~s0 %params%", "", "runas", 1 >> %ADMINSCRIPT%

    cscript /nologo %ADMINSCRIPT%
    del %ADMINSCRIPT%
)
goto :eof

我还认识到只有:createLink的第一次调用才会在%Temp%中生成一个vbs文件。至少它包含正确的代码:

Set oWS = WScript.CreateObject("WScript.Shell") 
sLinkFile = "C:\Users\Username\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Grün2 Mitgliederverwaltung\Grün2 starten.lnk" 
Set oLink = oWS.CreateShortcut(sLinkFile) 
oLink.TargetPath = "C:\Program Files\Grün2_Mitgliederverwaltung\Grün2_Launcher.jar" 
oLink.WorkingDirectory = "C:\Program Files\Grün2_Mitgliederverwaltung\" 
oLink.IconLocation = "C:\Program Files\Grün2_Mitgliederverwaltung\icon.ico" 
oLink.Save

0 个答案:

没有答案