I have a shortcut (called file.lnk
on the desktop) for file.exe
in another path.
Then I have a run.bat
on the desktop near my shortcut (file.lnk
).
My run.bat
has following code:
Start "" file.lnk Echo "all done or crashed shortcut even my shortcut corrupted continue to process and don't stop"
My problem is following:
When my file.exe
is deleted (or my shortcut file.lnk
is corrupted), and there is run.bat
executed, there is shown a pop-up message with the information that the executable file has been deleted and lets the user choose from one of three options (restore file - delete file - cancel). But I don't want to stop the process on pop-up. I want to continue even on an error with showing the message window.
This is my structure. I must use shortcuts and the only thing that is needed is to skip the corrupt shortcut message or auto answer to delete file.lnk
. I don't want to stop the process.
I tried this code get from: Get targeted file's path from .lnk shortcut file (cmd | .bat)
@echo off
:this is for check all shortcut file in a folder
for %%x in (*) do (
set shortcutpath=%%~x
for /f "tokens=2 delims==" %%a in ('wmic path win32_shortcutfile where "name='%shortcutpath:\=\\%'" get target /value') do (IF EXIST %%a echo "no problem,File exist & can you start")
)
echo by
pause
It works and %%a
gives me the correct output (shortcut target path) on using !shortcutpath:\=\\!
, but the existence check with IF EXIST
does not work as expected. It gives the wrong output, although the path is correct, it seems that IF EXIST
cannot check correct the file existence in the loop.
The following batch file works for nearly all shortcut file names and command lines defined with property Target in the shortcut file.
@echo off
setlocal EnableExtensions DisableDelayedExpansion
for %%I in ("%~dp0*.lnk") do (
set "ShortcutFile=%%I"
setlocal EnableDelayedExpansion
for /F "tokens=1* delims==" %%G in ('%SystemRoot%\System32\wbem\wmic.exe PATH Win32_ShortcutFile where "name='!ShortcutFile:\=\\!'" GET Target /VALUE 2^>nul') do (
if "%%G" == "Target" (
endlocal
for /F "eol=| delims=" %%J in ("%%H") do (
if /I not "%%~xJ" == ".exe" if /I not "%%~xJ" == ".com" set "ShortcutFile="
if defined ShortcutFile (
if exist "%%J" (
echo Shortcut "%%~nI" can be used as file "%%J" exists.
) else (
set "ExecutableFile=%%J"
set "ShortcutFile=%%~nI"
setlocal EnableDelayedExpansion
set "ExecutableFile=!ExecutableFile:&=&!"
if exist "!ExecutableFile!" (
echo Shortcut "!ShortcutFile!" can be used as file "!ExecutableFile!" exists.
) else (
echo Shortcut "!ShortcutFile!" cannot be used as file "!ExecutableFile!" does not exist.
)
endlocal
set "ExecutableFile="
)
set "ShortcutFile="
) else echo Shortcut "%%~nI" does not reference an executable. Simple execution verification is not possible.
)
if defined ShortcutFile (
echo Shortcut "%%~nI" has no property Target with a file name. Simple execution verification is not possible.
set "ShortcutFile="
)
)
)
if defined ShortcutFile endlocal & echo Shortcut "%%~nI" verification failed as WMIC failed to process the shortcut file.
)
endlocal
The batch file processes all *.lnk
files in the batch file directory. Replace %~dp0
in the third line by a different path like %USERPROFILE%\Desktop
(Windows default for desktop directory of current user) or %PUBLIC%\Desktop
(Windows default for the all users desktop directory). It is also possible to just remove %~dp0
for using the current working directory, but the fourth line must be modified in this case to set "ShortcutFile=%%~fI"
because of WMIC requires the fully qualified file name of the shortcut file.
The batch file considers following use cases:
*.lnk
file which does not have the property Target with a file name. Some shortcut files created for Microsoft applications do not have the property Target.&()[]{}^=;!'+,`~
which requires care on handling them in a batch file. For example, delayed variable expansion cannot be enabled permanently on processing such file names because of that causes a double parsing of the file name string before the execution of the command with interpreting every exclamation mark inside the file name string as beginning/end of a delayed expanded variable reference.&()[]{}^=;!'+,`~
which requires care on handling them in a batch file as well. Equal signs, exclamation marks and ampersands in program file name require special code in batch file for being correct processed.&
instead of just &
which must be considered on checking the existence of the program file..lnk
is not a shortcut file at all. The file has a wrong file extension.Very tricky to process is a shortcut file with name Test!.lnk
with Target beginning with "C:\Temp\Test\Development & Test 100% (!) = Full Test\Test!.exe"
. That is a hard to process file name because of the unusual folder name. But this batch file can process that shortcut file too.
There was used for testing a set of shortcut files which resulted in the following output of the batch file:
Shortcut "Folder" does not reference an executable. Simple execution verification is not possible.
Shortcut "Invalid" verification failed as WMIC failed to process the shortcut file.
Shortcut "MediaViewer" can be used as file "C:\Programs\MediaViewer\MediaViewer.exe" exists.
Shortcut "Missing Executable" cannot be used as file "C:\Temp\Test\iexplore.exe" does not exist.
Shortcut "Software Center" has no property Target with a file name. Simple execution verification is not possible.
Shortcut "Test!" can be used as file "C:\Temp\Test\Development & Test 100% (!) = Full Test\Test!.exe" exists.
Shortcut "Word" can be used as file "C:\Program Files\Microsoft Office\root\Office16\WINWORD.EXE" exists.
Folder.lnk
has as Target the fully qualified file name of an existing folder.Invalid.lnk
is not a shortcut file at all. It is a copy of the batch file with name Invalid.lnk
.MediaViewer.lnk
is easy to process as the fully qualified program file name contains no special character, not even a space character.Missing Executable.lnk
was created while C:\Temp\Test\iexplore.exe
really existed, but the executable file was deleted after the shortcut file creation.Software Center.lnk
is a special shortcut with no property Target.C:\Windows\CCM\scclient.exe
.Test!.lnk
has as Target a command line beginning with "C:\Temp\Test\Development & Test 100% (!) = Full Test\Test!.exe"
which is difficult to process in a batch file.Word.lnk
is a typical shortcut with spaces in program file name, but without characters with a special meaning for the Windows Command Processor.There should be read also: How to escape both comma and closing parenthesis in WHERE clause of WMIC? The batch file above does not workaround the file name limitations of WMIC in a where
clause with file name enclosed in '
. It is of course possible to include a workaround if that should be ever necessary because of an unusual shortcut file name. A comma in a shortcut file name results in wrong processing of this shortcut file with the posted code.
The batch file does also not check if wmic.exe
is installed at all. The WMI command-line utility is declared as deprecated by Microsoft and could be not installed anymore at all by default. It is an option Windows feature. It would be good to check first if wmic.exe
is available at all and inform the user about missing executable required here for the verification of the shortcuts. See second batch code in my answer on Find out if file is older than 4 hours in batch file for an example code for wmic.exe
existence check and user information on executable is missing.
Another solution is using WMIC for searching for *.lnk
files in the directory of the batch file as suggested by Compo in his answer.
@echo off
setlocal EnableExtensions DisableDelayedExpansion
pushd "%~dp0" 2>nul || exit /B 1
set "ShortcutPath=%CD:~2%"
for /F "skip=2 tokens=2* delims=," %%G in ('%SystemRoot%\System32\wbem\wmic.exe PATH Win32_ShortcutFile where "Drive='%CD:~,2%' and Extension='lnk' and Path='%ShortcutPath:\=\\%\\'" GET Name^,Target /Format:CSV 2^>nul') do (
set "ShortcutFile=%%~nG"
for /F "delims=" %%I in ("%%H") do (
if /I not "%%~xI" == ".exe" if /I not "%%~xI" == ".com" set "ShortcutFile="
if defined ShortcutFile (
if exist "%%I" (
echo Shortcut "%%~nG" can be used as file "%%I" exists.
) else (
set "ExecutableFile=%%I"
setlocal EnableDelayedExpansion
set "ExecutableFile=!ExecutableFile:&=&!"
if exist "!ExecutableFile!" (
echo Shortcut "!ShortcutFile!" can be used as file "!ExecutableFile!" exists.
) else (
echo Shortcut "!ShortcutFile!" cannot be used as file "!ExecutableFile!" does not exist.
)
endlocal
set "ExecutableFile="
)
set "ShortcutFile="
) else echo Shortcut "%%~nG" does not reference an executable. Simple execution verification is not possible.
)
if defined ShortcutFile (
echo Shortcut "%%~nG" has no property Target with a file name. Simple execution verification is not possible.
set "ShortcutFile="
)
)
popd
endlocal
The output of this batch file is:
Shortcut "Folder" does not reference an executable. Simple execution verification is not possible.
Shortcut "MediaViewer" can be used as file "C:\Programs\MediaViewer\MediaViewer.exe" exists.
Shortcut "Missing Executable" cannot be used as file "C:\Temp\Test\iexplore.exe" does not exist.
Shortcut "Software Center" has no property Target with a file name. Simple execution verification is not possible.
Shortcut "Test!" can be used as file "C:\Temp\Test\Development & Test 100% (!) = Full Test\Test!.exe" exists.
Shortcut "Word" can be used as file "C:\Program Files\Microsoft Office\root\Office16\WINWORD.EXE" exists.
An advantage of this solution in comparison to the first solution is its speed. The second solution finishes the processing of the shortcut files much faster than the first solution.
A disadvantage is the missing processing of the shortcut Invalid.lnk
as filtered out by WMIC. But that could be also interpreted as an advantage if the goal is using the command START for valid shortcuts.
The second solution as posted fails also processing correct a shortcut file with a comma in file name.
See also: What could be the reason for error message "Invalid XSL format (or) file name" output by WMIC on retrieving day of week? The usage of the format CSV
could not work on some Windows computers.
To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.
echo /?
endlocal /?
for /?
if /?
set /?
setlocal /?
wmic /?
wmic path /?
wmic path Win32_ShortcutFile /?
wmic path Win32_ShortcutFile get /?
There should be read also the Microsoft documentation for the Win32_ShortcutFile class as used here.
Read the Microsoft documentation about Using command redirection operators for an explanation of 2>nul
. The redirection operator >
must be escaped with caret character ^
on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded wmic
command line with using a separate command process started in background. The comma between Name
and Target
in second solution must be escaped also with ^
to be interpreted literally on parsing the FOR command line and not as arguments separator which would be otherwise replaced by a space character before passing the command line to background started cmd.exe
.