The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,762 other followers

Archive for the ‘Batch-Files’ Category

When an installer errors out with “Please re-run this installer as a normal user instead of”…

Posted by jpluimers on 2019/08/12

Via [WayBack] Anyone with a hint on how to work around this: … “Please re-run this installer as a normal user instead of”… – Jeroen Wiert Pluimers – Google+

This happened for instance when trying to install Source Tree 2.x on Windows (1.9.x works fine):

[Window Title]
SourceTreeSetup-2.3.1.0.exe

[Main Instruction]
Installation has failed

[Content]
Please re-run this installer as a normal user instead of “Run as Administrator”.

[Close]

The problem was by accident the machine got in a state to run commands without UAC approval, so the run dialog would already look have “This task will be created with administrative privileges”:

It was odd, as the machine didn’t have it enabled in the security policy (secpo.msc):

So I did a bit more digging, bumped into [WayBack] Why does my Run dialog say that tasks will created with administrative privileges? – The Old New Thing and had one of those #facepalm moments: Explorer had crashed, and I had started it from Process Explorer, forgetting Process Explorer had an UAC token.

The solution is easy:

  1. Logoff / Logon
  2. Verify the Windows-R shows a “normal” run:

Then you can just run the installer:

–jeroen

Posted in Batch-Files, Console (command prompt window), Development, Power User, Scripting, Software Development, The Old New Thing, Windows, Windows Development | Leave a Comment »

Windows: running a batch file during logon of a single or all users

Posted by jpluimers on 2019/07/01

You can automatically start processes during logon in a lot of ways (Trojans/Viruses find new ways all of the time).

The easiest way is to create a shortcut in one of the Startup folders. There are two of them: one for all the users, and one for the current user. Depending on your locale, Explorer can show a translated name, but the actual folder is named either of these:

  • "%AllUsersProfile%/Start Menu\Programs\Startup"
  • "%AppData%\Microsoft\Windows\Start Menu\Programs\Startup"

The folders do not exist at first, but are created when software starts putting shortcuts in them.

For a manual process, I created the two batch files below that create, then go to them (in both the console and explorer).

From there you can add shortcuts to things you want to run during logon.

They are based on:

I have successfully tested them in various Windows versions up until 10.

–jeroen

Batch files:

 

Posted in Batch-Files, Development, Power User, Scripting, Software Development, Windows | Leave a Comment »

Binding git diff to Beyond Compare

Posted by jpluimers on 2019/06/26

This became a huge batch-file which I need to refactor into smaller bits.

:: based on bc.bat
:: needs to be refactored into find-bc.bat
:: assumes git is on the path

:begin
@echo off

:checkGit
:: https://stackoverflow.com/questions/4781772/how-to-test-if-an-executable-exists-in-the-path-from-a-windows-batch-file/25696405#25696405
  where /q git || echo Cound not find git on the PATH %PATH%. && goto :eof
:: for now, the above is good enough as git installs itself on the path, but Beyond Compare does not.

:findBeyondCompare
  setlocal EnableExtensions EnableDelayedExpansion
  IF /I [%PROCESSOR_ARCHITECTURE%] == [amd64] goto :x64
  IF /I [%PROCESSOR_ARCHITEW6432%] == [amd64] goto :x64
  goto :x86
:x64
  :: OS is 64bit
  set hkcuBaseKey=HKEY_CURRENT_USER\Software\Scooter Software\Beyond Compare
  set hklmBaseKey=HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Scooter Software\Beyond Compare
  
  goto :findBC
:x86
  :: OS is 32bit
  set hkcuBaseKey=HKEY_CURRENT_USER\Software\Scooter Software\Beyond Compare
  set hklmBaseKey=HKEY_LOCAL_MACHINE\SOFTWARE\Scooter Software\Beyond Compare
  goto :findBC
:findBC
  :: https://gist.github.com/rojepp/634908
  :: http://stackoverflow.com/questions/5369528/windows-batch-reg-query-key-value-to-a-variable-but-do-not-display-error-if-key
  set SupportedBeyondCompareVersions=3, 4
  for %%v in (%SupportedBeyondCompareVersions%) do (
    for /f "usebackq tokens=2* delims= " %%c in (`reg query "%hkcuBaseKey% %%v" /v ExePath 2^>NUL`) do (
      call :do set bcExe="%%d"
    )
  )
  if not [%bcExe%]==[] goto :foundBC
    for /f "usebackq tokens=2* delims= " %%c in (`reg query "%hkcuBaseKey%" /v ExePath 2^>NUL`) do (
      call :do set bcExe="%%d"
    )
  if not [%bcExe%]==[] goto :foundBC
  for %%v in (%SupportedBeyondCompareVersions%) do (
    for /f "usebackq tokens=2* delims= " %%c in (`reg query "%hklmBaseKey% %%v" /v ExePath 2^>NUL`) do (
      call :do set bcExe="%%d"
    )
  )
  if not [%bcExe%]==[] goto :foundBC
    for /f "usebackq tokens=2* delims= " %%c in (`reg query "%hklmBaseKey%" /v ExePath 2^>NUL`) do (
      call :do set bcExe="%%d"
    )
  :: note that FOR /R needs a wildcard!
  if not [%bcExe%]==[] goto :foundBC
    for /r . %%d in (bcompare*.exe) do (
      call :do set bcExe="%%d"
    )
:foundBC
:: https://stackoverflow.com/questions/2772456/string-replacement-in-batch-file
:: note the BCompExe assignment requires at least https://ss64.com/nt/setlocal.html to have EnableDelayedExpansion and likely EnableExtensions 
:: see https://ss64.com/nt/delayedexpansion.html for ! expansion
  if [%bcExe%]==[] ( echo no bc.exe found in registry or relative to batch file) else (
    echo bcExe=%bcExe%
    if exist %bcExe% (
      call :do set bcCompExe=%bcExe:BCompare=BComp%
      :: echo bcCompExe=!bcCompExe!
      echo "Beyond Compare" %bcExe:\=/%
      echo "BComp" !bcCompExe:\=/!
      call :do git config --global diff.tool bc
      call :do git config --global difftool.bc.path !bcCompExe:\=/!
      call :do git config --global merge.tool bc
      call :do git config --global mergetool.bc.path !bcCompExe:\=/!
    )
    if not exist %bcExe% echo not found: [%bcExe%]
  )
:exit
  endlocal
:end
  goto :eof
:do
  echo %*
  call %*
  goto :eof

–jeroen

Posted in Batch-Files, Development, Scripting, Software Development | Leave a Comment »

How to test if an executable exists in the %PATH% from a windows batch file? – Stack Overflow

Posted by jpluimers on 2019/06/20

I needed a solution inside a batch file for git similar to [WayBack] How to test if an executable exists in the %PATH% from a windows batch file? – Stack Overflow which became this:

where /q git || echo Cound not find git on the PATH %PATH%. && goto :eof

I could have expanded this to find the install location, but for now this is sufficient.

When it is needed, I should read [WayBack] Programmatically (not manually) finding the path where Git is installed on a Windows system – Stack Overflow

–jeroen

Posted in Batch-Files, Development, DVCS - Distributed Version Control, git, Scripting, Software Development, Source Code Management | Leave a Comment »

batch file – SHIFT doesn’t affect %* – Stack Overflow

Posted by jpluimers on 2019/05/30

Quoting the answer in full because it so tremendously useful [WayBack] batch file – SHIFT doesn’t affect %* – Stack Overflow.

Especially the quoting/dequoting bits and the clever trick reconstructing %* into a batch file variable (minus double spaces).

Thanks so much James-K!

As you know, shift has no effect on %*, but you can construct a %* equivalent.

We’ll call the following line.bat :

@echo off
set line=%1
:loop
shift
if not "%1"=="" (
  set line=%line% %1
  goto :loop
)

echo   %%* = %*
echo line = %line%

If you type in the following command (Notice the double space between 3 and 4) :

line 1 2 3  4 bla dee dah

You will get the following output :

  %* = 1 2 3  4 bla dee dah
line = 1 2 3 4 bla dee dah

Note that %* retains multiple spaces, while using the %n notation does not.


Using something like this, you can allow your users to put their parameters in any order.

:loop
  :: Single variable parameters
  if "%1"=="something" set something=true
  :: Multi variable parameters 
  if "%~1"=="/source" shift & set source=%1
  shift
if not "%~1"=="" goto :loop

Notice that in the Multi-variable parameter statement I include one shift statement and one setstatement separated by an ampersand (&). The & tells the command processor that a separate command to be executed follows.


EDIT:

FYI: I recommend double quotes when checking the contents of variables. Usually you can use anycharacter, and you don’t even need to use two because they are just there to insure that an empty variable does not cause an error. For instance, when %1 is empty and you do if not hello==%1 call :sub the command processor will see this if not hello== call :sub and compare hello to call then try to execute :sub, and throw an error. In that specific case if not xhello==x%1 call :sub is just as good as if not "hello"=="%1" call :sub, because an empty %1 will cause the command processor to see if not xhello==x call :sub.

BUT using characters other than double-quotes will cause problems if the variable contains any special characters.

Using brackets as variable delimiters like (%1) can cause problems. For instance, the (special) piping characters don’t play nice inside brackets, and the escape character just seems to disappear, neither acting as a normal character, nor as the escape-character.

Also brackets are special characters in and of themselves designed to group and/or separate different lines of code and may not always act as anticipated.

Lastly, double quotes themselves are special characters specifically designed to surround other special characters, allowing them to act as normal characters. This is why you may see variables unquoted, then quoted again, like so.

set var="%~1"  & REM This sort of thing is used to insure that a variable is quoted.
                 REM %~1 unquotes %1 if it is already quoted, and leaves it alone if
                 REM %1 is not quoted.

set "var=%~1"  & REM This code assumes that `%1` contains special characters and
                 REM like before unquotes a quoted %1, but leaves the variable itself
                 REM unquoted. The double-quotes surrounding the variable and data
                 REM protects the command processor from any special characters that
                 REM exist in the data. Remember that anytime you reference `%var%`,
                 REM you will need to also surround the variable and data with
                 REM double-quotes.

A quick check for quotes is if exist %1 if %1==%~1 echo Unquoted.

–jeroen

Posted in Batch-Files, Development, Scripting, Software Development | Leave a Comment »

 
%d bloggers like this: