Archive for the ‘Batch-Files’ Category
Posted by jpluimers on 2018/03/21
About a year ago, [WayBack] Rumors 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: [WayBack] Windows 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.is] windows 10 powershell version – Google Search and [WayBack] PowerShell 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 »
Posted by jpluimers on 2018/02/06
Thanks [WayBack] Mr. 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
[WayBack] Removing 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 »
Posted by jpluimers on 2017/10/24
The first trick works in Windowa and nx (thanks [WayBack] pvandenberk):
curl -s -o /dev/null -I -w "%{http_code}" http://www.example.org/
Inside a Windows batch file you need to escape the % to %% so you get this:
curl -s -o /dev/null -I -w "%%{http_code}" http://www.example.org/
The second is slick but only works on nx (thanks [WayBack] Heath Borders):
#creates a new file descriptor 3 that redirects to 1 (STDOUT)
exec 3>&1
# Run curl in a separate command, capturing output of -w "%{http_code}" into HTTP_STATUS
# and sending the content to this command's STDOUT with -o >(cat >&3)
HTTP_STATUS=$(curl -w "%{http_code}" -o >(cat >&3) 'http://example.com')
[WayBack] Getting curl to output HTTP status code? – Super User
–jeroen
Posted in *nix, *nix-tools, bash, Batch-Files, cURL, Development, Power User, Scripting, Software Development | Leave a Comment »
Posted by jpluimers on 2017/06/27
Since I always forget:
for /l %1 in (1,2,200) do echo %1 >> text-%1.txt
This creates files with incremental filenames like this:
text-1.txt
text-3.txt
...
text-199.txt
–jeroen
via: For – Loop through a range of numbers | Windows CMD | SS64.com [WayBack]
Posted in Batch-Files, Development, Scripting, Software Development | 4 Comments »
Posted by jpluimers on 2017/04/13
How bad is the Windows command line really?
The Windows command line is bad. Very bad.
But it took until recently for old Windows versions – that out of the box had either no or poor PowerShell versions – to have slowly died.
So only now PowerShell finally has become an option that really works across all Windows versions I use. Go PowerShell!
–jeroen
Posted in Batch-Files, Development, PowerShell, Scripting, Software Development | Leave a Comment »
Posted by jpluimers on 2017/04/04
I wanted to compress a few files from the current directory in a zip file using the 7z.exe command-line version of 7zip.
The trick is about finding where 7z.exe is as the directory containing 7z.exe is not in the Windows PATH.
Notes:
setlocal
call SortDateTime.bat
echo %SortDateTime% %SortDate%-%SortTime%
for /f "usebackq tokens=2* delims= " %%c in (`reg query "HKEY_LOCAL_MACHINE\SOFTWARE\7-Zip" /v Path`) do (
set sevenzExe="%%d7z.exe"
)
endlocal & if exist %sevenzExe% %sevenzExe% a -tzip _my-build.%SortDate%-%SortTime%.zip MyServer.exe MyServer.map MyClient.exe MyClient.map
pause
–jeroen
Posted in Batch-Files, Development, Power User, Scripting, Software Development | Leave a Comment »
Posted by jpluimers on 2017/03/17
When you want to defrag.exe (the built-in Windows one, not the SysInternals Windows NT4 one, so make sure SysInternals comes last in your path) a volume, you have to run it with an elevated UAC Admin token.
But I just found out that you can do this without an Admin token:
C:\Windows\System32>Defrag.exe C: /t /v /u
Microsoft Drive Optimizer
Copyright (c) 2013 Microsoft Corp.
Tracking operation on (C:)...
Performing pass 2:
Free Space Consolidation: 31% complete...
This makes it much easier to separate monitoring scripting from execution.
–jeroen
Posted in Batch-Files, Development, Scripting, Software Development | Leave a Comment »
Posted by jpluimers on 2017/02/22
A while ago, I bitched that Microsoft moved away the Windows Update out of the Control panel into a language depended place (in Windows 10 1511 update broke the Hyper-V networking – Fix network connection issues).
Since then I had to maintain too many locales running Windows 10. So here is the batch file:
for /f "delims=" %%A in ('PowerShell -Command "(Get-Culture).Name"') do explorer "%LocalAppData%\Packages\windows.immersivecontrolpanel_cw5n1h2txyewy\LocalState\Indexed\Settings\%%A\AAA_SystemSettings_MusUpdate_UpdateActionButton.settingcontent-ms"
It uses these tricks:
- Set output of a command as a variable (in this case a for loop variable)
- Execute PowerShell script in a .bat file
- PowerShell Get-Culture (which gets a .NET CultureInfo instance)
- CultureInfo.Name property (which has the nl-NL, en-US, etc codes in it)
It replaced this simple batch-file which has worked for like 10 years:
%windir%\System32\rundll32.exe url.dll,FileProtocolHandler wuapp.exe
–jeroen
via: Windows Update Shortcut – Create in Windows 10 – Windows 10 Forums
Posted in .NET, .NET 1.x, .NET 2.0, .NET 3.0, .NET 3.5, .NET 4.0, .NET 4.5, Batch-Files, CommandLine, Development, Power User, PowerShell, Scripting, Software Development, Windows, Windows 10 | Leave a Comment »
Posted by jpluimers on 2017/01/04
for /f "tokens=* delims= " %%f in (myfile) do
If you put delims as the last parameter, then an ending space will be included as delimiter (at the start or in the middle it won’t).
A great tip by jeb and Joey in an answer for windows – Batch file FOR /f tokens – Stack Overflow
–jeroen
Posted in Batch-Files, Pingback, Scripting, Software Development, Stackoverflow | Leave a Comment »
Posted by jpluimers on 2016/11/22
A while ago, I needed to get the various date, time and week values from WMIC to environment variables with pre-padded zeros. I thought: easy job, just write a batch file.
Tough luck: I couldn’t get the values to expand properly. Which in the end was caused by WMIC emitting UTF-16 and the command-interpreter not expecting double-byte character sets which messed up my original batch file.
| What I wanted |
What I got |
wmic_Day=21
wmic_DayOfWeek=04
wmic_Hour=15
wmic_Milliseconds=00
wmic_Minute=02
wmic_Month=05
wmic_Quarter=02
wmic_Second=22
wmic_WeekInMonth=04
wmic_Year=2015
|
Day=21
wmic_DayOfWeek=4
wmic_Hour=15
wmic_Milliseconds=
wmic_Minute=4
wmic_Month=5
wmic_Quarter=2
wmic_Second=22
wmic_WeekInMonth=4
wmic_Year=2015
|
WMIC uses this encoding because the Wide versions of Windows API calls use UTF-16 (sometimes called UCS-2 as that is where UTF-16 evolved from).
As Windows uses little-endian encoding by default, the high byte (which is zero) of a UTF-16 code point with ASCII characters comes first. That messes up the command interpreter.
Lucikly rojo was of great help solving this.
His solution is centered around set /A, which:
- handles integer numbers and calls them “numeric” (hinting floating point, but those are truncated to integer; one of the tricks rojo uses)
- and (be careful with this as 08 and 09 are not octal numbers) uses these prefixes:
- 0 for Octal
- 0x for hexadecimal
Enjoy and shiver with the online help extract:
Read the rest of this entry »
Posted in Algorithms, Batch-Files, Development, Encoding, Floating point handling, Scripting, Software Development, UCS-2, UTF-16, UTF16 | Leave a Comment »