Topic: something about batch syntax and cmd.exe  (Read 7808 times)

Re: something about batch syntax and cmd.exe
« Reply #20 on: December 17, 2018, 03:52:44 PM »

slore

  • WimBuilder
  • Sr. Chef
  • ****
  • Date Registered: Jun 2016
  • Posts: 664
Someone will be  :confused:  by the CMD's "var syntax expands variables at input time" feature.

What is this feature?

variables in block init(expand) at input time.

Code: [Select]
@echo off
set var1=abc
set var2=def
if [%var1%]==[abc] (
    set var1=xyz
    echo var1=%var1% [in block]
    set var2=%var1%
)
echo var1=%var1% [out block]
echo var2=%var2%
pause

Output:
Code: [Select]
var1=abc [in block]
var1=xyz [out block]
var2=abc

because the block will be:
Quote
if [abc]==[abc] (
    set var1=xyz
    echo var1=abc [in block]
    set var2=abc
)

then execute it.
It will lead wrong result of %errorlevel% in block carelessly.

Code: [Select]
@echo off

set InternetConnection=False
echo errorlevel=%errorlevel%
rem if not [0]==[1] (
   ping.exe 999.999 -w 1
   if %errorlevel% EQU 0 set InternetConnection=True
rem )
echo InternetConnection=%InternetConnection%

echo errorlevel=%errorlevel%

echo reset errorlevel to 0
cd.
set InternetConnection=False
echo errorlevel=%errorlevel%
if not [0]==[1] (
   ping.exe 999.999 -w 1
   if %errorlevel% EQU 0 set InternetConnection=True
)
echo InternetConnection=%InternetConnection%

echo reset errorlevel to 0
cd.
set InternetConnection=False
echo errorlevel=%errorlevel%
if not [0]==[1] (
   call abcdefxyz "is not recognized as an internal or external command operable program or batch file."
   if %errorlevel% EQU 0 set InternetConnection=True
)
echo InternetConnection=%InternetConnection%
pause

output:
Quote
errorlevel=0
Ping request could not find host 999.999. Please check the name and try again.
InternetConnection=False
errorlevel=1
reset errorlevel to 0
errorlevel=0
Ping request could not find host 999.999. Please check the name and try again.
InternetConnection=True
errorlevel=1
reset errorlevel to 0
errorlevel=0
'abcdefxyz' is not recognized as an internal or external command,
operable program or batch file.
InternetConnection=True
Press any key to continue . . .

How to deal with this?

for errorlevel condition, use IF [NOT] ERRORLEVEL num <command> ... than %errorlevel% variable.

Code: [Select]
if %errorlevel% EQU 0 set InternetConnection=True
===>
Code: [Select]
if not errorlevel 1 set InternetConnection=True

see if /? for the details.

for normal variable, a normal way is use "delayed environment variable expansion" Extension.

Quote
@echo off
setlocal ENABLEDELAYEDEXPANSION
echo sum 1 to 10
set /a sum=0
for /l %%i in (1,1,10) do (
    set /a sum+=%%i
    echo %sum%
)
echo %sum% [out block]

echo use DELAYEDEXPANSION
set /a sum=0
for /l %%i in (1,1,10) do (
    set /a sum+=%%i
    echo !sum!
)
echo %sum% [out block]
pause

Quote
sum 1 to 10
0
0
0
0
0
0
0
0
0
0
55 [out block]
use DELAYEDEXPANSION
1
3
6
10
15
21
28
36
45
55
55 [out block]

also %errorlevel% variable works in this way.
Quote
@echo off
setlocal ENABLEDELAYEDEXPANSION
if not
  • ==[1] (

   ping.exe 999.999 -w 1
   if !errorlevel! EQU 0 set InternetConnection=True
)
pause

There are some other tricks without enable DELAYEDEXPANSION.
a). call

Quote
@echo off
set /a sum=0
for /l %%i in (1,1,10) do (
    set /a sum+=%%i
    call echo %%sum%%
)
echo %sum% [out block]

b). call :label_function
Quote
@echo off
set /a sum=0
for /l %%i in (1,1,10) do (
    set /a sum+=%%i
    rem call :label_function %sum%
    call :label_function
)
echo %sum% [out block]
pause
goto :EOF

:label_function
rem echo %1 always 0, because this will be [call :label_function 0] at input time

rem show_value_in_label_function_without_block
echo %sum%
goto :EOF

« Last Edit: December 17, 2018, 04:27:20 PM by slore »

Re: something about batch syntax and cmd.exe
« Reply #21 on: December 17, 2018, 03:55:07 PM »

Lancelot

  • Gena Baker
  • Grand Chef
  • *****
  • Date Registered: Sep 2010
  • Posts: 10350
Quote
just updated cmd at
Lancelot Reply 11

I tested it, and it just remove Temp dirs, and use 7z extract the file.

Update_with_zip.cmd OLD_PESE.zip  :confused:
I didn't see any DOWNLOAD action... how do it update?  :confused:

as you Updated Win10PE_SE_2018-12-16 - ZIP and Project servers, I downloaded with my smartphone in 5 minutes. this is good to me.

Hi Slore,

It aims to update following zip file
(I updated PESE plugins with 1 hour. download the ZIP toke 20 minutes.)
download action is you (or your phone)
extract (update) action is cmd file.

cmd only extracts in a way further updating will take low time.
(further updates requires files to be checked on download_... folder.... long to explain)
It also helps to switch between old and new zip.
eg.
here I can quickly switch between previous zip and new zip by only dropping zip to cmd file....

Anyway, It requires more codes to get cmd work out of box and understandable with better instructions..... Above is a beta work.

:turtle:

Re: something about batch syntax and cmd.exe
« Reply #22 on: December 18, 2018, 02:07:55 AM »

slore

  • WimBuilder
  • Sr. Chef
  • ****
  • Date Registered: Jun 2016
  • Posts: 664
Update_with_zip.cmd sounds it will be:
Code: [Select]
Update_with_zip.cmd OLD_VERSION_PESE_folder latest_ZIP_PESE.zip

OLD_VERSION_PESE folder contains custom plugins, modifed custom files, then with the batch can merge/update it to the latest version.





something about batch:
Code: [Select]
:Test7zOperational
::It seems 7z.exe without 7z.dll can extract .zip files (need to be better tested) for now no test to check 7z.dll Operational
cls
7z.exe
If %ErrorLevel% NEQ 0 Call :Test7zOperationalFailure %ErrorLevel%
GOTO:EOF

for just testing purpose, we avoid the unnecessary output in practice.

Code: [Select]
7z.exe 1>nul 2>nul
::Or
7z.exe  1>nul 2>&1

Re: something about batch syntax and cmd.exe
« Reply #23 on: December 18, 2018, 02:25:37 AM »

slore

  • WimBuilder
  • Sr. Chef
  • ****
  • Date Registered: Jun 2016
  • Posts: 664
Forgot to tell you an use-method about "delayed environment variable expansion" Extension.

Array in batch:

Code: [Select]
@echo off
setlocal ENABLEDELAYEDEXPANSION

set "Error[1]=not found"
set "Error[2]=bad parameter"
set "Error[3]=unknown error"


echo ErrorList:
for /l %%i in (1,1,3) do (
   echo    %%i.!Error[%%i]!
)

echo.
echo.

set ecode=2
echo Error:!Error[%ecode%]!
pause

echo.
echo.
echo with call trick
set /a n=0
for /l %%i in (1,1,3) do (
   set /a n+=1
   call echo    %%n%%.%%Error[!n!]%%
)
pause
 

Re: something about batch syntax and cmd.exe
« Reply #24 on: December 23, 2018, 06:48:00 PM »

Lancelot

  • Gena Baker
  • Grand Chef
  • *****
  • Date Registered: Sep 2010
  • Posts: 10350
Hi slore,

sorry I have not catch homeworks with cmd yet .... only posting around and continue other things...

following rdc on other topic :

I could not find Binmay homepage following readme
Web Site: http://www.filewut.com/spages/page.php/software/binmay

web archive provides website without files:
https://web.archive.org/web/20121023004318/http://www.filewut.com/spages/page.php/software/binmay

I guess this is the homepage now or a website which binmay continue development ?
http://freshmeat.sourceforge.net/projects/binmay
and or maybe you are user "sloaring" there ???

I like to distribute binmay with license and link to homepage like we have done for all tools,
so Bob.Omb and others can use binmay with different plugins when required.
where to get binmay ........ ?

ps:
freshmeat.sourceforge.net/projects/binmay
--> Dependencies link not working.


*
I have a question with binmay like tools (gsar, and type...) but my time is up now....
 Sorry It is very busy time of the year for me and extra things always come up in my life which make me not catch many things....
   time to go ...
:turtle:

Re: something about batch syntax and cmd.exe
« Reply #25 on: December 24, 2018, 01:16:50 AM »

slore

  • WimBuilder
  • Sr. Chef
  • ****
  • Date Registered: Jun 2016
  • Posts: 664
HI, Lancelot

about binmay.exe

Homepage is http://www.filewut.com/spages/page.php/software/binmay

but it can't access now.

I was use this tool for 10+ years.

I have the binmay-0.3.tar.gz soruce code and README, 2005-1-11 version.

I upload in other post is new version from other download sites, named binmay-mingw-110615.zip,

So this one should be new, but the binmay no version info to confirm.

It has alot mode for searching, replacing binary datas in README.


here is another URL new.

https://x3w.org/i4/ilpsf.php?id=64093

but I can't download it, you can try, if it is new, and has source.
« Last Edit: December 24, 2018, 03:11:41 AM by slore »

Re: something about batch syntax and cmd.exe
« Reply #26 on: December 24, 2018, 07:05:40 AM »

Lancelot

  • Gena Baker
  • Grand Chef
  • *****
  • Date Registered: Sep 2010
  • Posts: 10350
Thanks slore,

x3w... using credit card info too much to avoid "spam" ......

I will follow your experience with binmay.exe  :thumbsup:

:turtle:

Re: something about batch syntax and cmd.exe
« Reply #27 on: December 24, 2018, 12:45:55 PM »

slore

  • WimBuilder
  • Sr. Chef
  • ****
  • Date Registered: Jun 2016
  • Posts: 664
well, as it don't have x64, I compile one with updated source code.

* add getopt() for windows
* correct the output result as binmay-mingw-110615.zip
* fix a bug for the search and replace function(take 2 hours)  (binmay-mingw-110615.zip fixed)


Re: something about batch syntax and cmd.exe
« Reply #28 on: December 24, 2018, 03:50:16 PM »

Lancelot

  • Gena Baker
  • Grand Chef
  • *****
  • Date Registered: Sep 2010
  • Posts: 10350
Thanks slore,

with your fixes It will be good to have both x86 and x64 ....exe
eg. binmay0.3m_x86_x64_by_slore.zip

so results will always be same when used x86 or x64 ...

:turtle:


Re: something about batch syntax and cmd.exe
« Reply #29 on: December 24, 2018, 04:32:02 PM »

slore

  • WimBuilder
  • Sr. Chef
  • ****
  • Date Registered: Jun 2016
  • Posts: 664
with your fixes It will be good to have both x86 and x64 ....exe
eg. binmay0.3m_x86_x64_by_slore.zip

I don't know what did in 2005~2011.
I leave x86 Fallback to the 2011 version as it fixed some bugs and improved.

x64 version just test the basic usage -s -r for patching drvinst.exe, Windows.UI.CredDialogController.dll.

Code: [Select]
so results will always be same when used x86 or x64 ...
:embarrassed: if there got a difference, we can know one of them has BUG.
« Last Edit: December 24, 2018, 04:35:39 PM by slore »

Re: something about batch syntax and cmd.exe
« Reply #30 on: December 25, 2018, 05:34:51 PM »

Lancelot

  • Gena Baker
  • Grand Chef
  • *****
  • Date Registered: Sep 2010
  • Posts: 10350
Hi slore,

I did homework reply 20, and get better understanding now, thanks for providing a very good summary.  :cheers:

I need some more time to digest and continue...... (reply 11 + update ping cmd...)
I have to follow Bob.Omb now.

I put a copy of test.cmd I study so far here. (better not lost  :wink: )
Code: [Select]
cd /d %~dp0
@Echo off

::Slore Reply 20 http://theoven.org/index.php?topic=2669.msg30034#msg30034


::variables in block init(expand) at input time.
::--- Set inside block valid out of block not inside block - block: If For block
::--- Call not effected with this behaviour - good news - you can Call inside a block to get variables out of block - reminding inblock does not change normally - see slore examples.
::----
::SetLocal ENABLEDELAYEDEXPANSION
:: delayed variable expansion means the variables are "expanded" not before but during command execution. https://www.robvanderwoude.com/variableexpansion.php
Set var1=abc
Set var2=def
Echo var1=%var1% [start]
Echo var2=%var2% [start]
Echo ----
If [%var1%]==[abc] (
    Set var1=xyz
    Echo var1=%var1% [in block]
    Set var2=%var1%
    Echo var2=%var2% [in block]
    Echo ----
)
Echo var1=%var1% [out block]
Echo var2=%var2% [out block]
Echo ----

Echo ----------------
Echo ----------------
::pause

Set var1=abc
Set var2=def
Echo var1=%var1% [start]
Echo var2=%var2% [start]
Echo ----
If [%var1%]==[abc] Call :VarTest
Echo var1=%var1% [out block out Call]
Echo var2=%var2% [out block out Call]
Echo ----

Echo ----------------
Echo ----------------
::pause

Set var1=abc
Set var2=def
Echo var1=%var1% [start]
Echo var2=%var2% [start]
Echo ----
If [%var1%]==[abc] (
    Set var1=xyz
    Echo var1=%var1% [in block]
    Set var2=%var1%
    Echo var2=%var2% [in block]
    Echo ----
    ::notice var1 changed inside block but valid after block so initial var1 unchanged inside block and Call :VarTest runs.
    If [%var1%]==[abc] Call :VarTest
    Echo var1=%var1% [in block out Call]
    Echo var2=%var2% [in block out Call]
)
Echo var1=%var1% [out block out Call]
Echo var2=%var2% [out block out Call]
Echo ----

Echo ----------------
Echo ----------------
::pause



Set InternetConnection=False
Echo InternetConnection=%InternetConnection% [start]
Echo ErrorLevel=%ErrorLevel% [start]
If Not [0]==[1] (
   ping.exe 999.999 -w 1
   If %ErrorLevel% EQU 0 Set InternetConnection=True
   Echo InternetConnection=%InternetConnection% [in block]
   ::here %ErrorLevel% always have [start] value 0 because it is inside block
   :: - As a result %InternetConnection% value always becomes True - Wrong result : use of %errorlevel% in block carelessly.
)
Echo InternetConnection=%InternetConnection% [out block]

Echo.
Echo.
Echo.
::pause

Echo ErrorLevel 'cmd' ability to handle ErrorLevel specially without variable workaruond this case
::https://www.robvanderwoude.com/errorlevel.php
:: ErrorLevel return TRUE if the return code was equal to or higher
::https://ss64.com/nt/errorlevel.html
::IF NOT ERRORLEVEL 1 means if ERRORLEVEL is less than 1
::IF ERRORLEVEL 1 will return TRUE whether the errorlevel is 1 or 5 or 64
::
::since ms tools do not have negative errorlevels we can use IF NOT ERRORLEVEL 1
::https://docs.microsoft.com/en-us/windows/desktop/Debug/system-error-codes
::
Set InternetConnection=False
Echo InternetConnection=%InternetConnection% [start]
Echo ErrorLevel=%ErrorLevel% [start]
If Not [0]==[1] (
   ping.exe 999.999 -w 1
   If Not ErrorLevel 1 Set InternetConnection=True
   Echo InternetConnection=%InternetConnection% [in block]
)
Echo InternetConnection=%InternetConnection% [out block]

Echo.
Echo.
Echo.
::pause


::SetLocal https://www.robvanderwoude.com/local.php
::Set /a https://www.robvanderwoude.com/battech_math.php
::For /L https://www.robvanderwoude.com/ntfor.php
::FOR /L %variable IN (start,step,end) DO command [command-parameters]


Echo sum 1 to 10
Set /a sum=0
For /l %%i In (1,1,10) Do (
    Set /a sum+=%%i
    Echo %sum%
)
Echo %sum% [out block]

Echo.
Echo.
Echo.
::pause

SetLocal ENABLEDELAYEDEXPANSION
echo use DELAYEDEXPANSION
Set /a sum=0
For /l %%i In (1,1,10) Do (
    Set /a sum+=%%i
    Echo !sum!
)
Echo %sum% [out block]
SetLocal DISABLEDELAYEDEXPANSION

Echo.
Echo.
Echo.
::pause

::also %errorlevel% variable works with ENABLEDELAYEDEXPANSION
Set InternetConnection=False
Echo InternetConnection=%InternetConnection% [start]
SetLocal ENABLEDELAYEDEXPANSION
If Not [o]==[1] (
   ping.exe 999.999 -w 1
   If !errorlevel! EQU 0  =True
   Echo InternetConnection=%InternetConnection% [in block]
)
Echo InternetConnection=%InternetConnection% [out block]
SetLocal DISABLEDELAYEDEXPANSION

Echo.
Echo.
Echo.
::pause

::slore There are some other tricks without enable DELAYEDEXPANSION.
::a)call

Set /a sum=0
For /l %%i In (1,1,10) Do (
    Set /a sum+=%%i
    ::here Call helps to show changing sum
    Call Echo %%sum%%
    Echo %sum% [in block]
    ::did not work Call Set sum=%%sum%%
    ::did not work Echo %sum% [in block]
)
Echo %sum% [out block]

Echo.
Echo.
Echo.
::pause

::b)Call :label_function
Set /a sum=0
For /l %%i In (1,1,10) Do (
    Set /a sum+=%%i
    Rem Call :label_function %sum%
    Call :label_function
    :: Calling label function do not effect in block result
    Echo %sum% [in block]
)
Echo %sum% [out block]

Echo.
Echo.
Echo.
pause


::@ means hiderun
::@cmd.exe /c pause

Echo.
Echo.
Echo.
Echo.
pause
cmd
pause
exit

:label_function
Rem echo %1 always 0, because this will be [Call :label_function 0] at input time

Rem show_value_in_label_function_without_block
Echo %sum% [label_function]
Goto :EOF


:VarTest
Set var1=xyz
Echo var1=%var1% [in Call]
Set var2=%var1%
Echo var2=%var2% [in Call]
Echo ----
Goto:EOF

Edit:
updated codebox ping fix following slore replies...
« Last Edit: December 29, 2018, 08:01:06 AM by Lancelot »

Re: something about batch syntax and cmd.exe
« Reply #31 on: December 26, 2018, 01:33:18 AM »

slore

  • WimBuilder
  • Sr. Chef
  • ****
  • Date Registered: Jun 2016
  • Posts: 664
Hi, Lancelot

Code: [Select]
If not ErrorLevel 0 Set InternetConnection=True
=>
if not %errorlevel% GEQ 0 ....
=>
if not (%errorlevel% >= 0) ...
=>
if (%errorlevel% < 0) Set InternetConnection=True
=>
only -1,-2,..,-255 make InternetConnection be True.

this is wrong "condition expression".

normally use If [not] ErrorLevel 1 ...
(also not in good design)

see details with if /?
« Last Edit: December 26, 2018, 01:34:11 AM by slore »

Re: something about batch syntax and cmd.exe
« Reply #32 on: December 26, 2018, 05:29:20 AM »

Lancelot

  • Gena Baker
  • Grand Chef
  • *****
  • Date Registered: Sep 2010
  • Posts: 10350
Hi slore,

only -1,-2,..,-255 make InternetConnection be True.
With some google I guess this is special case with ping

normally use If [not] ErrorLevel 1 ...
(also not in good design)
I normally (nearly all the time) use
If not ErrorLevel 0
to control an application (eg. 7z.exe) works as expected.

I feel
>=
GEQ
<
not good for 3rd party !

and better check and learn errorlevels of frequently used cmd, and mostly use
if (%errorlevel% < 0)
on such cases.

I hope above is a right conclusion....

:turtle:

Re: something about batch syntax and cmd.exe
« Reply #33 on: December 26, 2018, 07:04:44 AM »

slore

  • WimBuilder
  • Sr. Chef
  • ****
  • Date Registered: Jun 2016
  • Posts: 664
Quote
If not ErrorLevel 0
to control an application (eg. 7z.exe) works as expected.

should not as expected...

Code: [Select]
7z.exe
If ErrorLevel 0 echo TEST
8z.exe
If ErrorLevel 0 echo TEST

always output:TEST

as %errorlevel% = 1(not exists),  %errorlevel% = 0(exist), both greater than 0.
« Last Edit: December 26, 2018, 07:05:28 AM by slore »

Re: something about batch syntax and cmd.exe
« Reply #34 on: December 26, 2018, 08:22:51 AM »

Lancelot

  • Gena Baker
  • Grand Chef
  • *****
  • Date Registered: Sep 2010
  • Posts: 10350
 :thumbsup:

ok I understand ErrorLevel (without variable).  :great:

It seems on most cases I will use %ErrorLevel%
Code: [Select]
8z.exe
If not %ErrorLevel% Equ 0 echo There is a kind of failure

ps: there is no 8z.exe
ps: not exists 9009


++ I have to be very careful when using ErrorLevel without variable, which I guess will be for special cases... :turtle:


Re: something about batch syntax and cmd.exe
« Reply #35 on: December 26, 2018, 11:11:31 AM »

Lancelot

  • Gena Baker
  • Grand Chef
  • *****
  • Date Registered: Sep 2010
  • Posts: 10350
Hi slore,

Thanks again for binmay, I just could find time to add to projects.  :great:

:turtle:

Re: something about batch syntax and cmd.exe
« Reply #36 on: December 29, 2018, 08:39:01 AM »

Lancelot

  • Gena Baker
  • Grand Chef
  • *****
  • Date Registered: Sep 2010
  • Posts: 10350
Hi slore,

updated reply 30 ...

So far I understand,
windows cmd apps like ping always have >=0  ....
so we can check success with If Not ErrorLevel 1 with such cases....  :cool:

*
Here is the new cmd I use to check internet connection
Code: [Select]
::https://stackoverflow.com/questions/27589925/simple-batch-for-checking-internet-connectivity-and-setting-environment-variable
::slore reply20 http://TheOven.org/index.php?topic=2669.msg30034#msg30034
::-
::cd /d "%~dp0"
::@Echo Off
Call :CallC_InternetConnection_CMD
::Call :CallC_InternetConnection_CMD_Test_Failure
::Echo CallInternetConnection:%CallInternetConnection%
::Set CallInternetConnection=False
::cmd.exe
If %CallInternetConnection% EQU True Exit 0
If %CallInternetConnection% EQU False Exit 1
cmd.exe
Exit
:CallC_InternetConnection_CMD
Echo Checking Internet Connection
Set CallInternetConnection=False
::Google DNS Primary 8.8.8.8
Ping 8.8.8.8 -n 1 -w 1000
If Not ErrorLevel 1 Set CallInternetConnection=True
If %CallInternetConnection% EQU True GOTO:EOF
Ping 8.8.8.4 -n 1 -w 1000
If Not ErrorLevel 1 Set CallInternetConnection=True
If %CallInternetConnection% EQU True GOTO:EOF
Ping google.com -n 1 -w 1000
If Not ErrorLevel 1 Set CallInternetConnection=True
If %CallInternetConnection% EQU True GOTO:EOF
Ping google.nl -n 1 -w 1000
If Not ErrorLevel 1 Set CallInternetConnection=True
If %CallInternetConnection% EQU True GOTO:EOF
GOTO:EOF
:CallC_InternetConnection_CMD_Test_Failure
Echo Checking Internet Connection
Set CallInternetConnection=False
Echo CallInternetConnection:%CallInternetConnection%
Ping 999.999 -n 1 -w 1000
If Not ErrorLevel 1 Set CallInternetConnection=True
Echo CallInternetConnection:%CallInternetConnection%
If %CallInternetConnection% EQU True GOTO:EOF
Ping 999.999 -n 1 -w 1000
If Not ErrorLevel 1 Set CallInternetConnection=True
If %CallInternetConnection% EQU True GOTO:EOF
GOTO:EOF

further on proxy topic...
http://TheOven.org/index.php?topic=2660.0

:turtle:

Re: something about batch syntax and cmd.exe
« Reply #37 on: January 01, 2019, 03:16:09 PM »

Lancelot

  • Gena Baker
  • Grand Chef
  • *****
  • Date Registered: Sep 2010
  • Posts: 10350
Hi slore,

back to the initial reason of current topic:

I was trying to create a cmd to find a shortcut (.lnk) target,
Today at last I found time to work on it, It seems now all working fine,
Still better if your advanced eyes take a look:

FindLnkTarget.cmd
Code: [Select]
::Lancelot 2019.01.01 - Created cmd to find Target of shortcut (.lnk)
::Usage: Drag and Drop a shortcut file to FindLnkTarget.cmd
::Shortcut Tests made on Host Win10 using Following Shortcut Types:
::-Shortcut File with Extension Test
::-Shortcut Folder Test
::-Shortcut File with NO Extension Test
::-Network shortcut test
::Credits:
::slore Reply 5 Way2 http://TheOven.org/index.php?topic=2669.msg29916#msg29916
::Reno Reply 10: https://www.computerhope.com/forum/index.php/topic,80659.msg531837.html#msg531837
::Dias de verano Reply 11 : https://www.computerhope.com/forum/index.php/topic,80659.msg531843.html#msg531843
::Requires:
::cmd.exe find.exe findstr.exe
@Echo Off
cls
cd /d "%~dp0"
::%1 with quotes if there is space without quotes if there is not space
Set LnkFile=%1
Call :LinkFile_Target
Echo LNKTarget: %LNKTarget%

pause
cmd
pause
exit


:LinkFile_Target
Call :Calculate_Skip
:: LnkFile value with quotes
::Type %LnkFile%
::Following finds too much lines - Reno Reply 10: https://www.computerhope.com/forum/index.php/topic,80659.msg531837.html#msg531837
::Type %LnkFile%|find /i "\"
::Following works fine to find shortcut with extension BUT fails BAD (Result Unwanted) with shortcuts with NO extension: - Dias de verano Reply 11 : https://www.computerhope.com/forum/index.php/topic,80659.msg531843.html#msg531843
::Type %LnkFile%|find /i "\"|find /i "."
::Following works fine with shortcuts having path a-z BUT fails (NOTHING FOUND) with network shortcut - Reno Reply 10: https://www.computerhope.com/forum/index.php/topic,80659.msg531837.html#msg531837
::Type %LnkFile%|find /i "\"|findstr /b "[a-z][:][\\]"
::Following works fine with network shortcuts
::Type %LnkFile%|find /i "\\"|findstr /b "\\"
For /f "%SKIP_OPT%delims=" %%A In ('Type %LnkFile% ^|find /i "\" ^|findstr /b "[a-z][:][\\]"') Do Set LNKTarget="%%A"
If [%LNKTarget%]==[] For /f "%SKIP_OPT%delims=" %%A In ('Type %LnkFile% ^|find /i "\\" ^|findstr /b "\\"') Do Set LNKTarget="%%A"
Goto:EOF


:Calculate_Skip
rem calculate the skip line(s) of AutoRun output slore Reply 5 Way2 http://TheOven.org/index.php?topic=2669.msg29916#msg29916
Set /a SKIP_N=-1
For /f "delims=" %%A In ( 'Echo Test' ) Do set /a SKIP_N+=1
if %SKIP_N% NEQ 0 (set SKIP_OPT=skip=%SKIP_N% )
Goto:EOF
:::Calculate_Skip Tests:
:: SKIP_N becomes 0 with default windows cmd settings
:: SKIP_N becomes 1 if windows cmd produce 1 additional line (via Autorun etc.)
Echo SKIP_N %SKIP_N%
:: SKIP_OPT have no value with default windows cmd settings
:: SKIP_OPT becomes skip=1 if windows cmd produce 1 additional line (via Autorun etc.)
Echo SKIP_OPT %SKIP_OPT%
::Further tests with simple echo:
For /f "%SKIP_OPT% delims=" %%A In ( 'Echo Test' ) Do Echo Result: "%%A"
For /f "%SKIP_OPT% delims=" %%A In ( 'cmd.exe /D /C Echo Test' ) Do Echo Result: "%%A"
For /f "%SKIP_OPT% usebackq delims=" %%A In ( `Echo Test` ) Do Echo Result: "%%A"
For /f "%SKIP_OPT% usebackq delims=" %%A In ( `cmd.exe /D /C Echo Test` ) Do Echo Result: "%%A"

Here are shortcuts I test
* Tested_Lnk.7z (1.61 kB - downloaded 61 times.)

+ I may miss shortcut types to test, let me know..

:turtle:
« Last Edit: January 01, 2019, 06:02:01 PM by Lancelot »

Re: something about batch syntax and cmd.exe
« Reply #38 on: January 01, 2019, 06:02:24 PM »

Lancelot

  • Gena Baker
  • Grand Chef
  • *****
  • Date Registered: Sep 2010
  • Posts: 10350
fixed previous post:
--> Set LnkFile=%1
 :embarrassed:
:turtle:

Re: something about batch syntax and cmd.exe
« Reply #39 on: January 02, 2019, 02:27:13 AM »

slore

  • WimBuilder
  • Sr. Chef
  • ****
  • Date Registered: Jun 2016
  • Posts: 664

The type+find trick works for most shortcuts(95%?), but the text search in binary file is not correct way,
so some case it will failed.

I created two shortcuts for the next folder and file:
Quote
\\192.168.25.60\CDImage\Thin PC\Font for Win7thinpc_x64
\\192.168.25.60\CDImage\Thin PC\Font for Win7thinpc_x64\amd64~winemb-font-bitmap-miscellaneous~~~~6.1.7600.16385~1.0\WinEmb-Font-Bitmap-Miscellaneous.cab

your test.cmd got only:
\\192.168.25.60\CDImage

Code: [Select]
D:\Users\Slore\Desktop>type a.lnk|find "\\"
\\192.168.25.60\CDIMAGE
\\192.168.25.60\CDImage
\\192.168.25.60\CDImage\Thin PC\Font for Win7thinpc_x64

D:\Users\Slore\Desktop>type b.lnk|find "\\"
\\192.168.25.60\CDIMAGE
\\192.168.25.60\CDImage
\\192.168.25.60\CDImage\Thin PC\Font for Win7thinpc_x64\amd64~winemb-font-bitmap-miscellaneous~~~~6
.1.7600.16385~1.0\WinEmb-Font-Bitmap-Miscellaneous.cab


if you want to use script for that, vbs is a good choose for it.
Code: [Select]
Set wshShell = CreateObject("WScript.Shell")
Set wshLink = wshShell.CreateShortcut("a.lnk")
MsgBox wshLink.TargetPath
Set wshLink = wshShell.CreateShortcut("b.lnk")
MsgBox wshLink.TargetPath

also VBS(wshLink object) can create shortcuts.


* reply later about the proxy thing.

 

Powered by EzPortal