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

Archive for the ‘Batch-Files’ Category

Locating the 7z.exe command-line tool on Windows

Posted by jpluimers on 2018/09/18

From one of my scripts: it will find a  64-bit 7z.exe if it was installed as part of the 7-zip installer, then run it with the parameters provided to the batch file.

  setlocal
  
:verify7zip
:: registry trick from http://www.robvanderwoude.com/files/sortdate2_nt.txt
:: extra trick: tokens=2* allows to get the  3rd (and beyond: space delimited!) value in one variable %%b
  for /F "tokens=2*" %%a IN ('REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\7-Zip" /v Path64 2^>nul') do set sevenZipDirectoryPath=%%b
  call :checkMissingSetting sevenZipDirectoryPath     || goto :help
  
  set sevenZipExeFilePath=%sevenZipDirectoryPath%7z.exe
  if not exist "%sevenZipExeFilePath%" call :showError "No 7-zip executable at %sevenZipExeFilePath%" || goto :help
  
:run7zip
  "%sevenZipExeFilePath%" %*

  endlocal
  goto :end
  
:checkMissingSetting
  if not defined %1 call :notifyMissingSetting %1 && exit /b 1
  call :showSetting %1
  exit /b 0
  goto :end

:notifyMissingSetting
  echo Registry didn't provide the environment variable "%1"
  goto :end
  
:showError
  :: remove double quotes using tilde trick:
  echo %~1
  
:help
  echo Syntax: %0 7z.exe-commandline-parameters
  goto :end

:end

–jeroen

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

windows – How to list all files in directory/subdirectory without path name CMD? – Stack Overflow

Posted by jpluimers on 2018/07/30

I needed all .dproj files in all subdirectories, but only their filenames without any directory names.

With directory names, this is easy:

dir /s /b *.dproj

The answers at [WayBack] windows – How to list all files in directory/subdirectory without path name CMD? – Stack Overflow give the below kind of output.

[WayBack] forfiles embeds all filenames within quotes:

forfiles /m *.dproj /s

"Foo.dproj"
"Bar.dproj"

A more convoluted [WayBack] for loop gives them without quotes where n stands for name and x for extension including .:

for /r %a in (*.dproj) do @echo %~nxa
Foo.dproj
Bar.dproj

–jeroen

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

PowerShell – query reboot/shutdown events

Posted by jpluimers on 2018/06/19

Thanks [WayBackgbabu for the below PowerShell ide

As PowerShell command:

Get-EventLog System | Where-Object {$_.EventID -eq "1074" -or $_.EventID -eq "6008" -or $_.EventID -eq "1076"} | ft Machinename, TimeWritten, UserName, EventID, Message -AutoSize -Wrap

Based on it and my own experience, thse Event IDs can be interesting:

  • 41 – The system has rebooted without cleanly shutting down first
  • 109 – The kernel power manager has initiated a shutdown transition.
  • 1073 – The attempt by user [domain]\[username] to restart/shutdown computer [computername] failed.
  • 1074 – The process [filename].[extension] has initiated the restart of computer [computername] on behalf of user [domain]\[username\ for the
  • 1076 – ???
  • 6008 – The previous system shutdown at [time-in-local-format] on [date-in-local-format] was unexpected.

You can also run this as a batch file, but not you need to escape the pipe | into ^| like this:

PowerShell Get-EventLog System ^| Where-Object {$_.EventID -eq "1074" -or $_.EventID -eq "6008" -or $_.EventID -eq "1076"} ^| ft Machinename, TimeWritten, UserName, EventID, Message -AutoSize -Wrap

If you have PowerShell 3.0 or greater, then you can use the [Archive.is-In operator:

PowerShell Get-EventLog System ^| Where-Object {$_.EventID -in "41", "109", "1074", "6008", "1076"} ^| ft Machinename, TimeWritten, UserName, EventID, Message -AutoSize -Wrap

–jeroen

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

Rumors of Cmd’s death have been greatly exaggerated – but it still pays to switch to PowerShell

Posted by jpluimers on 2018/03/21

About a year ago, [WayBackRumors of Cmd’s death have been greatly exaggerated – Windows Command Line Tools For Developers got published as a response to confusing posts like these:

But I still think it’s a wise idea to switch away from the Cmd and to PowerShell as with PowerShell you get way more consistent language features, far better documentation, truckloads of new features (of which I like the object pipeline and .NET interoperability most) and far fewer quirks.

It’s time as well, as by now, Windows 7 has been EOL for a while, and Windows 8.x is in extended support: [WayBackWindows lifecycle fact sheet – Windows Help:

Client operating systems  Latest update or service pack  End of mainstream support  End of extended support
  Windows XP  Service Pack 3  April 14, 2009  April 8, 2014
  Windows Vista  Service Pack 2  April 10, 2012  April 11, 2017
  Windows 7*  Service Pack 1  January 13, 2015  January 14, 2020
  Windows 8  Windows 8.1  January 9, 2018  January 10, 2023
Windows 10, released in July 2015**  N/A  October 13, 2020  October 14, 2025

Which means the PowerShell version baseline on supported Windows versions is at least 4.0: [Archive.iswindows 10 powershell version – Google Search and [WayBackPowerShell versions and their Windows version – 4sysops

PowerShell and Windows versions ^
PowerShell Version Release Date Default Windows Versions
PowerShell 2.0 October 2009 Windows 7 Windows Server 2008 R2 (**)
PowerShell 3.0 September 2012 Windows 8 Windows Server 2012
PowerShell 4.0 October 2013 Windows 8.1 Windows Server 2012 R2
PowerShell 5.0 April 2014 (***) Windows 10

So try PowerShell now. You won’t regret it.

–jeroen

via: [WayBack] Very interesting clear-up post and comments on CMD, command.com, PowerShell in past and future DOS/Windows versions and Unix shells altogether. – Ilya S – Google+

Posted in Batch-Files, CommandLine, Development, Power User, PowerShell, Scripting, Software Development, Windows, Windows 10, Windows 7, Windows 8, Windows 8.1, Windows Server 2012, Windows Server 2012 R2, Windows Server 2016 | Leave a Comment »

Removing double quotes from variables in batch file creates problems with CMD environment – Stack Overflow

Posted by jpluimers on 2018/02/06

Thanks [WayBackMr. Rick for the answer as this is exactly the bit I needed:

Input:

set widget="a very useful item"
set widget
set widget=%widget:"=%
set widget

Output:

widget="a very useful item"
widget=a very useful item

[WayBackRemoving double quotes from variables in batch file creates problems with CMD environment – Stack Overflow

This trick is convenient in cases like this:

set LocalHostsFile="%windir%\System32\drivers\etc\hosts"
set LocalHostsTemplate="%LocalHostsFile:"=%.template"

The above replaces ALL double quotes with nothing.

If you want to smart replace (like done when de-quoting CSV), you need a bit more complex code like described in [WayBack] batch file – Remove quotes from named environment variables in Windows scripts – Stack Overflow, where you basically have two options:

  • assigning inside a for loop
  • assigning inside a subroutine

Both work because parameters used like %~x do get their quotes removed; you cannot use that syntax on plain variables.

–jeroen

Posted in Batch-Files, Development, Scripting, Software Development | 3 Comments »

 
%d bloggers like this: