so I have this script to create shortcuts for files I have to check, the problem is it creates the shortcut weather the file exists OR not. Any way to stop it from creating shortcuts for files that don't exist YET?
@echo off
set SCRIPT="%TEMP%\%RANDOM%-%RANDOM%-%RANDOM%-%RANDOM%.vbs"
echo Set oWS = WScript.CreateObject("WScript.Shell") >> %SCRIPT%
echo sLinkFile = "%USERPROFILE%\Desktop\check\file_shortcut.lnk" >> %SCRIPT%
echo Set oLink = oWS.CreateShortcut(sLinkFile) >> %SCRIPT%
echo oLink.TargetPath = "C:\Firm\F1000\file_to_check.pdf" >> %SCRIPT%
echo oLink.Save >> %SCRIPT%
cscript /nologo %SCRIPT%
del %SCRIPT%
Probably the simplest way for you to implement this would be like this:
@Set "TARGET=C:\Firm\F1000\file_to_check.pdf"
@If Not Exist "%TARGET%" GoTo :EOF
@Set "SCRIPT=%TEMP%\%RANDOM%-%RANDOM%.vbs"
@( Echo Set objShell = WScript.CreateObject("WScript.Shell"^)
Echo strLinkFile = "%USERPROFILE%\Desktop\check\file_shortcut.lnk"
Echo Set objLink = objShell.CreateShortcut(strLinkFile^)
Echo objLink.TargetPath = "%TARGET%"
Echo objLink.Save) 1> "%SCRIPT%"
@"%__AppDir__%cscript.exe" //B //NoLogo "%SCRIPT%"
@Del "%SCRIPT%"
If you had several shortcuts to create, and you knew that the check
directory already existed on the users' desktop, you could use a for-loop. In the following example, I've used three different targets, "C:\Firm\F1000\file_to_check.pdf"
, "C:\Somewhere\ADirectory\somefile.ext"
, and "D:\Elsewhere\filename.ext"
. You can just list yours in the same manner as below:
@Set "SCRIPT=%TEMP%\%RANDOM%-%RANDOM%.vbs"
@( Echo Set objShell = WScript.CreateObject("WScript.Shell"^)
Echo strLinkFile = "%USERPROFILE%\Desktop\check\"+Wscript.Arguments(1^)+".lnk"
Echo Set objLink = objShell.CreateShortcut(strLinkFile^)
Echo objLink.TargetPath = Wscript.Arguments(0^)
Echo objLink.Save
) 1> "%SCRIPT%"
@For %%G In (
"C:\Firm\F1000\file_to_check.pdf"
"C:\Somewhere\ADirectory\somefile.ext"
"D:\Elsewhere\filename.ext"
) Do @If Exist %%G "%__AppDir__%cscript.exe" //B //NoLogo "%SCRIPT%" %%G "%%~nG"
@Del "%SCRIPT%"
In both examples, I've removed some of those %RANDOM%
parts of the string, I do not think it's likely that you'll have an existing .vbs
file in %TEMP%
, which you'll need in the future, and which already matches that generated random name.