Hi I am newly in the batch/command line.
I want to create a shortcut of an application only if the shortcut/file doesn't already exist. I wrote the part that create the short that works if it is not in the condition IF, but when it is inside, nothing happens.
Here is the code :
@echo off
if exist "C:\Users\%USERNAME%\Desktop\Tks.lnk" (
echo file
) else (
echo not file
set SCRIPT="%TEMP%\%RANDOM%-%RANDOM%-%RANDOM%-%RANDOM%.vbs"
echo Set oWS = WScript.CreateObject("WScript.Shell") >> %SCRIPT%
echo sLinkFile = "%USERPROFILE%\Desktop\Tks.lnk" >> %SCRIPT%
echo Set oLink = oWS.CreateShortcut(sLinkFile) >> %SCRIPT%
echo oLink.TargetPath = "C:\Users\%USERNAME%\Documents\000_DF_P\SF - Tks.accdb" >> %SCRIPT%
echo oLink.IconLocation = "destination_icon" >> %SCRIPT%
echo oLink.Save >> %SCRIPT%
cscript /nologo %SCRIPT%
del %SCRIPT%
)
pause
Thank you
CodePudding user response:
There are a few issues to avoid and other ways to do this but just to correct your attempt.
Move the definitions to the start and escape internal else ) using ^)
@echo off
set "SCRIPT=%TEMP%\%RANDOM%-%RANDOM%-%RANDOM%-%RANDOM%.vbs"
if exist "C:\Users\%USERNAME%\Desktop\Tks.lnk" (
echo file
) else (
echo not file
echo Dim oWS >> %SCRIPT%
echo Set oWS = WScript.CreateObject("WScript.Shell"^) >> %SCRIPT%
echo sLinkFile = "%USERPROFILE%\Desktop\Tks.lnk" >> %SCRIPT%
echo Set oLink = oWS.CreateShortcut(sLinkFile^) >> %SCRIPT%
echo oLink.TargetPath = "C:\Users\%USERNAME%\Documents\000_DF_P\SF - Tks.accdb" >> %SCRIPT%
echo oLink.IconLocation = "destination_icon" >> %SCRIPT%
echo oLink.Save >> %SCRIPT%
cscript /nologo %SCRIPT%
del %SCRIPT%
)
pause