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,230 other followers

Archive for the ‘Batch-Files’ Category

Batch files: deleting first/middle/ending parts of environment variables

Posted by jpluimers on 2021/01/06

Batch files are often a pain to write, but you cannot always rewrite them in PowerShell.

The pain below is about deleting parts of environment variables in batch files.

I’ll just redirect to and quote from posts that can way better describe this than I do:

  • [WayBack] Check if Batch variable starts with “…” – Stack Overflow made me find
  • [WayBack] windows – Batch – Delete Characters in a String – Super User
  • [WayBack] CMD Variable edit replace – Windows CMD – SS64.com

    The variable _test containing 12345abcabc is used for all the following examples:

    ::Replace '12345' with 'Hello '
       SET _test=12345abcabc
       SET _result=%_test:12345=Hello %
       ECHO %_result%          =Hello abcabc
    
    ::Replace the character string 'ab' with 'xy'
       SET _test=12345abcabc
       SET _result=%_test:ab=xy%
       ECHO %_result%          =12345xycxyc
    
    ::Delete the character string 'ab'
       SET _test=12345abcabc
       SET _result=%_test:ab=%
       ECHO %_result%          =12345cc
    
    ::Delete the character string 'ab' and everything before it
       SET _test=12345abcabc
       SET _result=%_test:*ab=% 
       ECHO %_result%          =cabc
    
    ::Replace the character string 'ab' and everything before it with 'XY'
       SET _test=12345abcabc
       SET _result=%_test:*ab=XY% 
       ECHO %_result%          =XYcabc
    
    
    :: To remove characters from the right hand side of a string is 
    :: a two step process and requires the use of a CALL statement
    :: e.g.
    
       SET _test=The quick brown fox jumps over the lazy dog
    
       :: To delete everything after the string 'brown'  
       :: first delete 'brown' and everything before it
       SET _endbit=%_test:*brown=%
       Echo We dont want: [%_endbit%]
    
       ::Now remove this from the original string
       CALL SET _result=%%_test:%_endbit%=%%
       echo %_result%

    All the examples on this page assume the default Expansion of variables, if you are using DelayedExpansion then you can choose to change the variable references to !_variable! instead of %_variable%

    One advantage of DelayedExpansion is that it will allow you to replace the % character, it will still have to be escaped as %% but the replace action will then treat it like any other character:

    Replace the letter P with a percent symbol:
    Setlocal EnableDelayedExpansion
    _demo=somePdemoPtextP
    _demo=!_demo:P=%%!

    Remove spaces from a text string

    To delete space characters use the same syntax as above:

    SET _no_spaces=%_some_var: =%

–jeroen

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

Batch file: check for (non-)existence of registry key

Posted by jpluimers on 2021/01/05

Small batch file that only deletes a registry key if it exists:

:DeleteKeyIfItExists
reg query %1 >nul 2>&1
if %errorlevel% equ 0 reg delete %1 /f
goto :eof

It is based on:

  • redirecting both stderr and stdout to nul (the >nul 2>&1 bit)
  • checking reg query with the appropriate errorlevel value for equality (equ operator) for 0 (existence); you can also use 1 for non-existence.

Based on:

–jeroen

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

Windows command prompt: decrementing loop

Posted by jpluimers on 2020/12/30

I needed a decrementing loop on the Windows command prompt, but that seems very hard from batch files without programming your own kind of while loop:

PowerShell to the rescue to loop back from and including 463 down to and including 290:

PowerShell -Command "for ($i=463; $i -ge 290; $i--) { Write-Host "Value " $i}"

This outputs:

Value 463
Value 462
...
Value 291
Value 290

In a similar way, you can execute a cmd command, but then you need to be careful on how to pass parameters: the \" is important to you can have quotes within quoted strings..

PowerShell -Command "for ($i=463; $i -ge 290; $i--) { & echo \"Value $i\"}"

gives this:

Value 463
Value 462
...
Value 291
Value 290

Read the rest of this entry »

Posted in Batch-Files, CommandLine, Console (command prompt window), Development, PowerShell, PowerShell, Scripting, Software Development, Windows | 1 Comment »

On Windows 7 and 8.x too: Completely disable Windows 10 telemetry collection – twm’s blog

Posted by jpluimers on 2020/12/10

From [WayBack] Completely disable Windows 10 telemetry collection – twm’s blog:

So I don’t forget: According to an article in c’t magazine, disabling the “DiagTrack” service (“Connected User Experience and Telemetry”) will completely disable user tracking in Windows 10. They also say that they did not see any negative effects.

Source: [WayBack] Telefonierverbot in c’t 01/2019 page 172 (in German)

I saw at least one system where the service is not shown when you run Services.msc: it did not list DiagTrack, nor Connected User Experience and Telemetry. How awful is that!

The service can also be installed non older Windows versions: [WayBack] Just found DiagTrack running in Services – Tips and Tricks

Sometimes, it gets re-enabled. I think this happens during major Windows updates.

To inspect, stop and disable

Run all commands from the console the below bold commands. The non-bold text was the output on my system. If instead of the cmd.exe console, you run a PowerShell console, then remove the bits PowerShell -Command " and " at the start and end of each command.

The first command does not require an Administrative (UAC Elevated) command prompt; the last one does.

However, the first command, needs the | Select-Object * bit as otherwise most of the fields will not be displayed, excluding for instance StartType.

powershell -Command "Get-Service -Name DiagTrack | Select-Object *"


Name                : DiagTrack
RequiredServices    : {RpcSs}
CanPauseAndContinue : False
CanShutdown         : True
CanStop             : True
DisplayName         : Connected User Experiences and Telemetry
DependentServices   : {}
MachineName         : .
ServiceName         : DiagTrack
ServicesDependedOn  : {RpcSs}
ServiceHandle       :
Status              : Running
ServiceType         : Win32OwnProcess
StartType           : Automatic
Site                :
Container           :

On an Administrative command-prompt:

powershell -Command "Set-Service -Name DiagTrack -StartUpType Disabled"
powershell -Command "Get-Service -Name DiagTrack | Stop-Service"

Two notes:

Read the rest of this entry »

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

`exit /b #`: set `errorlevel` to `#`, then exit batch file or subroutine – via: Errorlevel – Windows CMD – SS64.com

Posted by jpluimers on 2020/11/25

I seem to always forget how to set an error leve in side a batch file, but [WayBack] Errorlevel – Windows CMD – SS64.com tells how:

  • When ending a [WayBacksubroutine, you can use EXIT /b N to set a specific ERRORLEVEL N.
  • You can make a [WayBackbatch file return a non-zero exit code by using the [WayBackEXIT command.

    Exit 0
    Exit /B 5

    To force an ERRORLEVEL of 1 to be set without exiting, run a small but invalid command like [WayBack]COLOR 00 

    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 [[archive.is]source].

It looks like I already used a bare EXIT /B without explaining it in Source: stop/start IIS.

Further reading, including the difference between subroutines, blocks and batch files:

Finally saving Google Groups messages in the way back machine:

  1. Convert the URL
  2. Save the latter in archive.is

–jeroen

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

 
%d bloggers like this: