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 2,433 other followers

Archive for the ‘Batch-Files’ Category

Windows: starting Chrome in full-screen kiosk mode from a batch file

Posted by jpluimers on 2021/08/03

When configuring a web-based kiosk for someone with Alzheimer’s disease, I wanted to start Chrome in full-screen kiosk mode.

Chrome full-screen kiosk mode

The secret for full-screen kiosk mode is to pass the -start-fullscreen command-line option. Thanks [WayBack] User ginomay89 – Super User for answering that in [WayBack] tablet – How to set Google Chrome to automatically open up and in full screen – Super User.

Finding chrome

At first I thought about differentiating on the chrome.exe location that you can find in the registry. This turned out to be depending on how you install Chrome:

  • locally for the current user by a non-local-administrator user (by default the location is under %LocalAppData%)
  • globally for all users by a local-administratator user (by default is under %ProgramFiles(x86)%)

Oddly, there is no way (not even by denying UAC elevation!) for a local administrator to install Chrome for only the current user.

This is odd, as when non-local-administrator denies UAC, the installation is locally to the user.

Then I remembered there are two ways for Windows to find an application

The cool thing is that the start command does the latter, so I came up with this batch file that starts chrome with the -start-fullscreen parameter that will initiate kiosk mode with the default chrome settings:

start "Chrome Kiosk Mode" chrome --start-fullscreen

In case I want to compare the registry settings

Basically sorting out the registry settings would mean parsing the references to chrome.exe (often with extra parameters) in the below registry key/value-name pairs.

One day I might need to do this for different reasons, but currently the start trick suffices.

Read the rest of this entry »

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

Windows: batch file to logoff all other users (run as Administrator)

Posted by jpluimers on 2021/07/22

Based on zzz but filters current user, and listener session.

:: https://stackoverflow.com/questions/36715033/how-to-logoff-all-users-on-windows-from-command-line-as-a-domain-administrator
:: The findstr bit filters out the current session (starts with ">") and session 65536 (which is the listener)
for /f "skip=2 tokens=2,3 delims= " %%a in ('query session ^| findstr /v /b ">" ^| findstr /v "65536  Listen"') DO (
    echo %%a|findstr /xr "[1-9][0-9]* 0" >nul && (
      logoff %%a 
    )
    echo %%b|findstr /xr "[1-9][0-9]* 0" >nul && (
      logoff %%b 
    )
)
goto :eof

–jeroen

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

How do I get the application exit code from a Windows command line? – Stack Overflow

Posted by jpluimers on 2021/07/15

[WayBack] How do I get the application exit code from a Windows command line? – Stack Overflow solutions below.

Note they ONLY work when nobody sets the ERRORLEVEL environment variable.

  • You can quickly see what app returns: app.exe & echo %errorlevel% – [WayBack] marbel82
  • something.exe
    echo Exit Code is %errorlevel%

    – [WayBack] Samuel Renkert

  • start /wait something.exe
    echo %errorlevel%

    – [WayBack] Gary

  • @echo off
    my_nify_exe.exe
    if %ERRORLEVEL% EQU 0 (
       echo Success
    ) else (
       echo Failure Reason Given is %errorlevel%
       exit /b %errorlevel%
    )

    – [WayBack] Curtis Yallop

  • It’s worth noting that .BAT and .CMD files operate differently.Reading https://ss64.com/nt/errorlevel.html it notes the following:

    There is a key difference between the way .CMD and .BAT batch files set errorlevels:

    An old .BAT batch script running the ‘new’ internal commands: APPEND, ASSOC, PATH, PROMPT, FTYPE and SET will only set ERRORLEVEL if an error occurs. So if you have two commands in the batch script and the first fails, the ERRORLEVEL will remain set even after the second command succeeds.

    This can make debugging a problem BAT script more difficult, a CMD batch script is more consistent and will set ERRORLEVEL after every command that you run .

    This was causing me no end of grief as I was executing successive commands, but the ERRORLEVEL would remain unchanged even in the event of a failure. – [WayBackRockDoctor

–jeroen

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

windows – What is the proper way to test if a parameter is empty in a batch file? – Stack Overflow

Posted by jpluimers on 2021/07/14

You can use:

IF "%~1" == "" GOTO MyLabel

to strip the outer set of quotes. In general, this is a more reliable method than using square brackets because it will work even if the variable has spaces in it.

Source: [WayBack] jamesdlin answering on [WayBack] windows – What is the proper way to test if a parameter is empty in a batch file? – Stack Overflow

The tilde (~) strips out double quotes from the command as per

C:\>help for | findstr "~"
    %~I         - expands %I removing any surrounding quotes (")
    %~fI        - expands %I to a fully qualified path name
    %~dI        - expands %I to a drive letter only
    %~pI        - expands %I to a path only
    %~nI        - expands %I to a file name only
    %~xI        - expands %I to a file extension only
    %~sI        - expanded path contains short names only
    %~aI        - expands %I to file attributes of file
    %~tI        - expands %I to date/time of file
    %~zI        - expands %I to size of file
    %~$PATH:I   - searches the directories listed in the PATH
    %~dpI       - expands %I to a drive letter and path only
    %~nxI       - expands %I to a file name and extension only
    %~fsI       - expands %I to a full path name with short names only
    %~dp$PATH:I - searches the directories listed in the PATH
    %~ftzaI     - expands %I to a DIR like output line
values.  The %~ syntax is terminated by a valid FOR variable name.

–jeroen

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

File Line Count: “built-in” line count for Windows

Posted by jpluimers on 2021/06/22

Windows if full of undocumented gizmo’s, like find alternative for wc -l counting all lines in a file: [WayBack] File Line Count

Use FIND command to count file lines, store line count into a variable.

Description: Running the FIND command with option /v and empty search string will find all lines
Running the FIND command with option /c will output the line count only.
The FOR command with option /f will parse the output, the line count in this case, and the set command put the line number into the cnt variable.
Script:
1.
2.
3.
4.
set file=textfile.txt
set /a cnt=0
for /f %%a in ('type "%file%"^|find "" /v /c') do set /a cnt=%%a
echo %file% has %cnt% lines
Script Output:
 DOS Script Output
textfile.txt has 50 lines

[WayBack] Stupid command-line trick: Counting the number of lines in stdin | The Old New Thing

Windows doesn’t come with wc,
but there’s a sneaky way to count the number of lines anyway:

some-command-that-generates-output | find /c /v ""

It is a special quirk of the find command
that the null string is treated as never matching.
The /v flag reverses the sense of the test,
so now it matches everything.
And the /c flag returns the count.

The reason dates back to the original MS-DOS
version of find.exe,
which according to the comments appears to have been written
in 1982.
And back then, pretty much all of MS-DOS was written in assembly
language.

Via: batch file line count – Google Search and [WayBack] windows – How to count no of lines in text file and store the value into a variable using batch script? – Stack Overflow

–jeroen

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

 
%d bloggers like this: