1

I´m building a small batch script which moves some files and creates three startmenu entries. The problem is about the lines 30 to 32 where I call the method "createLink". In these lines I call a function which creates the entries for the startmenu. But only the first one is created. It does not matter which one of these three is first called. Only the first entry called is created. Here is the hole script:

@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

I also recognized that only the first call of :createLink produces a vbs file in %Temp%. At least it contains the correct code:

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
TrackerSB
  • 317
  • 6
  • 19
  • 1
    Well thats a bunch of code... Your problem might be the leak of [Delayed Expansion](http://ss64.com/nt/delayedexpansion.html). Read through that and well will see if that solves the problem :) – geisterfurz007 Sep 01 '16 at 12:51
  • I cannot see where this could take effect actually. I am not changing a variable in a loop or something like that. At the beginning I set needed variables and I´m calling createLink with different constants. – TrackerSB Sep 01 '16 at 13:35
  • you do so once in getAdminrights I think. Another idea. I have no idea of cscript so sorry if I write something wrong. Do you want to clear the variable with `del %SCRIPT%` or what purpose does that have? Try to put in a `pause`after that line to see if any error messages occur. – geisterfurz007 Sep 01 '16 at 13:46
  • 1
    At first sight: [**never** use `:label` nor `:: label-like comment` inside a command block enclosed in `()` parentheses](http://stackoverflow.com/a/32147995/3439404). Change **all** `::` to `rem`. – JosefZ Sep 01 '16 at 17:36
  • With that del-command i want to clear the file on path %SCRIPT%. Just putting a pause right after there is not enough because every call opens up a new window. So i tried executing createLink in a separate file: No error. Just working as expected. – TrackerSB Sep 01 '16 at 18:06
  • Actually using a label inside a command block is bad practise. Using a `::` which is explicitly not interpreted as label seems to speed things up. Why I should use `rem`? May read [this](http://xset.tripod.com/tip3.htm). Nevertheless I replaced `::` with `rem`. – TrackerSB Sep 01 '16 at 18:21
  • @TrackerSB: You should review the info at your given link, that specify that _"The maximum lenght of %PATH% in COMMAND.COM is 127 characters"_! Such a site is outdated 20 years at last. Yes, using `::` instead of `rem` speed things up, in computers with floppy disks! See [this answer](http://stackoverflow.com/questions/16632524/what-does-double-colon-mean-in-dos-batch-files/16639875#16639875). – Aacini Sep 02 '16 at 14:32

0 Answers0