Archive for the ‘Batch-Files’ Category
Posted by jpluimers on 2013/08/13
Getting substrings in a batchfile requires you to use the %…:~…% syntax as explained by Rob van der Woude.
Note this only works on batch file variables, so not on batch/function arguments (%1, %2, …)and for-loop indexes (%%f, etc).
This code gets the leftmost three characters of var:
set var=%var:~3%
echo %var%
–jeroen
via Remove the first 3 characters in var in batch file? – Stack Overflow.
Posted in Batch-Files, Development, Scripting, Software Development | Leave a Comment »
Posted by jpluimers on 2013/06/12
Slightly more than a month ago, Thomas Müller release the experimental GExperts + code formatter for Delphi XE4.
Since there is no official GExperts for Delphi XE4 build yet, his installation instructions include this:
Use the ExpertManager tool to register GExperts to Delphi XE4.
The thing is: ExpertManager is part of GExperts, so this introduces a “chichen-and-egg” situation.
So I wrote a small batch file and a bit of documentation to install it.
Just in case you cannot download that SVN changeset easily, here are the documentation and batch-file:
Documentation
Steps to install:
0- Quit Delphi XE4
1- Download the GExperts XE4 experimental from http://blog.dummzeuch.de/2013/04/28/experimental-gexperts-code-formatter-for-delphi-xe4/
2- Recursively unpack that download
3- Copy the batch file Install-GExperts-XE4-experimental–run-as-administrator-from-ExpertManager.exe-directory.bat into that directory
4- From that directory, run the copied Install-GExperts-XE4-experimental–run-as-administrator-from-ExpertManager.exe-directory.bat
5- Run Delphi XE4
6- Check if GExperts is installed
Batch file
:: check if user is administrator
"C:\Windows\system32\cacls.exe" "C:\Windows\system32\config\system" 1>nul 2>&1 && (goto :isAdmin)
:isNoAdmin
echo you need to be Administrator, and (when under Vista or higher) run this using using UAC
goto :exit
:isAdmin
:: Gets the right link for x86 (32-bit) program files
IF /I %PROCESSOR_ARCHITECTURE% == amd64 goto :x64
IF /I %PROCESSOR_ARCHITEW6432% == amd64 goto :x64
goto :x86
:x64
:: OS is 64bit
set ProgramFilesX86=%ProgramFiles(x86)%
goto :continue
:x86
:: OS is 32bit
set ProgramFilesX86=%ProgramFiles%
goto :continue
:continue
:: create the right directory and copy the files
setlocal
set TargetDirectory=%ProgramFilesX86%\GExperts for RAD Studio XE4
mkdir "%TargetDirectory%"
:: set FilesToCopy=DbugIntf.pas ExpertManager.exe GExperts.chm GExpertsDebugWindow.exe GExpertsGrep.exe regularexpert\GExpertsRSXE4.dll Readme.txt preview.pas
set FilesToCopy=ExpertManager.exe GExperts.chm regularexpert\GExpertsRSXE4.dll preview.pas
for %%f in (%FilesToCopy%) do copy /y %%f "%TargetDirectory%\"
set RegFile="%TargetDirectory%\ExpertsXE4.reg"
:: explorer /select,"%TargetDirectory%\GExpertsRSXE4.dll"
:: expand backslash into double backslash for .REG file
set ExpertTarget="%TargetDirectory%\GExpertsRSXE4.dll"
set ExpertTarget=%ExpertTarget:\=\\%
::Windows Registry Editor Version 5.00
::
::[HKEY_CURRENT_USER\Software\Embarcadero\BDS\11.0\Experts]
::"GExperts"="C:\\Program Files (x86)\\GExperts for RAD Studio XE4\\GExpertsRSXE4.dll"
::
echo Windows Registry Editor Version 5.00 >%RegFile%
echo. >>%RegFile%
echo [HKEY_CURRENT_USER\Software\Embarcadero\BDS\11.0\Experts] >>%RegFile%
echo "GExperts"=%ExpertTarget% >>%RegFile%
echo. >>%RegFile%
:: explorer /select,%RegFile%
regedit /S %RegFile%
endlocal
goto :exit
:exit
–jeroen
via: experimental GExperts + code formatter for Delphi XE4 « twm’s blog.
Posted in Batch-Files, Delphi, Delphi XE4, Development, Scripting, Software Development | 2 Comments »
Posted by jpluimers on 2013/06/05
AACPlus allows for a many combinations of encoding flags.
Finding out whick allows to encode a mono audio stream is a bit time consuming.
Luckily, I found this post:
If you want encode in mono make this:
enc_aacPlus test.wav test.aac --br (max 256000) --mono
or for streaming:
enc_aacplus - - --br (max 256000) --silent --rawpcm 44100 2 16 --mono
Note that the various versions of enc_aacPlus.exe requires the enc_aacPlus.dll from WinAmp.
--jeroen
via: AACPlus v2 Encoder Problem..
Posted in BASS.NET, Batch-Files, Development, Media Streaming, Power User, Scripting, Software Development, Un4seen BASS Audio Library | Leave a Comment »
Posted by jpluimers on 2013/04/04
Thanks Jeb for answering this:
You can solve it with delayed expansion, because delayed expansion works different than percent expansion.
:START
setlocal EnableDelayedExpansion
@echo What folder do you want to process? (Provide a path without a closing backslash)
set /p datapath=
::Is string empty?
IF X!datapath! == X GOTO:START
::Does string have a trailing slash? if so remove it
IF !datapath:~-1!==\ SET "datapath=!datapath:~0,-1!"
echo !datapath!
It expands later than the percent expansion, and after the delayed expansion no more parsing is done, so even spaces or special characters have any effect.
–jeroen
via:
Posted in Batch-Files, Development, Power User, Scripting, Software Development | Leave a Comment »
Posted by jpluimers on 2013/02/28
Posted in .NET, .NET 2.0, .NET 3.0, .NET 3.5, .NET 4.0, .NET 4.5, Batch-Files, C#, C# 2.0, C# 3.0, C# 4.0, C# 5.0, Development, Scripting, Software Development | Leave a Comment »
Posted by jpluimers on 2013/01/29
A while ago, I had to adapt a DOS app that used one specific version of Excel to do some batch processing so it would support multiple versions of Excel on multiple versions of Windows.
One of the big drawbacks of DOS applications is that the command lines you can use are even shorter than Windows applications, which depending you how you call an application are:
This is how the DOS app written in Clipper (those were the days, it was even linked with Blinker :) started Excel:
c:\progra~1\micros~2\office11\excel.exe parameters
01234567890123456789012345678901234567890
1 2 3 4
The above depends on 8.3 short file names that in turn depend on the order in which similar named files and directories have been created.
The trick around this, and around different locations/versions of an application, is to use START to find the right version of Excel.
The reason it works is because in addition to PATH, it checks the App Paths portions in the registry in this order to find an executable: Read the rest of this entry »
Posted in Batch-Files, Development, Encoding, Power User, Scripting, Software Development, Unicode, Windows, Windows 7, Windows 8, Windows Server 2000, Windows Server 2003, Windows Server 2003 R2, Windows Server 2008, Windows Server 2008 R2, Windows Vista, Windows XP | Leave a Comment »
Posted by jpluimers on 2013/01/02
On development machines it can be comfortable to add the local Administrators group to SQL Server, and make them equivalent to SA (SQL Server Administrator).
This especially as SA is disabled by default when you install using Windows Authentication mode (which is the default). You can Change Server Authentication Mode to include SQL Server mode, but then you still have to enable SA (you can even rename SA)
This is basically what you want to do:
CREATE LOGIN [BUILTIN\Administrators] FROM WINDOWS WITH DEFAULT_DATABASE=[master];
EXEC master..sp_addsrvrolemember @loginame = N'BUILTIN\Administrators', @rolename = N'sysadmin';
GRANT CONTROL SERVER TO [BUILTIN\Administrators];
There are a few gotchas here:
- The name of the group BUILTIN\Administrators depends on the localization of your system.
This is one of the reasons I usually advise clients to have server systems run on the most common ENU (English USA) localization of Windows.
Another reason is that it is far easier to find information ENU English Messages back on the internet.
- You need to be SQL Server Administrator to begin with.
There is a little trick to get this done: you can stop the SQL Server service, then restart SQL Server it in single-user mode.
In single-user mode, members from the BUILTIN\Administrators group can connect as a member of the sysadmin fixed server role.
- If you want to access SQL Server as member from BUILTIN\Administrators, you need to run your SQL client tools with the UAC elevated permission (otherwise the Administrative bit is not set, and the BUILTIN\Administrators is not recognized).
That’s what the batch file below solves.
You need to start it as member of BUILTIN\Administrators with elevated privilege (the easiest way is to run it from an elevated command prompt).
It will: Read the rest of this entry »
Posted in Batch-Files, Database Development, Development, Scripting, Software Development, SQL Server, SQL Server 2008, SQL Server 2008 R2, SQL Server 2012 | Leave a Comment »
Posted by jpluimers on 2012/12/31
A lot of scripts you find on the internet have hardcoded Windows account names or groups, for instance BUILTIN\Administrators
Those don’t work on many localized Windows versions, as part of the account names have been translated as well. Not only Administrators is translated, but BUILTIN can be translated too. Basically, expect everything in Windows to be translated as part of the localization process.
Some people keep translations lists, but that is not the real solution.
The real solution is that each such group, account or other identifier stems from a SID or Security ID.
Many of those SIDs are the same on any machine, or structured the same within a domain.
Microsoft has a list of these called Well-known security identifiers in Windows operating systems.
That list isn’t in a format most Windows tools use it, so I generated the list below that is more suitable.
The list below is based on a Windows 7 machine. Other versions or editions give slightly different results, but it is a good start.
At the bottom is the batch file that I used to generate this table. That file is adapted from the Microsoft list above.
The batch file depends on a few tools and tricks: Read the rest of this entry »
Posted in Batch-Files, Development, Power User, Scripting, Software Development, Windows, Windows 7, Windows 8, Windows Server 2000, Windows Server 2003, Windows Server 2003 R2, Windows Server 2008, Windows Server 2008 R2, Windows Vista, Windows XP | 1 Comment »
Posted by jpluimers on 2012/12/19
I knew that 2>&1 was needed to redirect both stderr and stdout, but for piping, it cannot be at the end of the line. This works in the categories shown at the bottom of the post.
Rob van der Woude again to the rescue in redirection:
(4) Redirecting both standard output and standard error to the same file or device is done by adding 2>&1 to the command line. This will only work in OS/2 and NT, not in MS-DOS.
Where you put 2>&1 is rather critical. It will only do what it is supposed to do when placed at the end of the command line (as Jennie Walker pointed out to me) or right before the next pipe ( | ).
Example: batch file that checks if a few NarrowCast machines are indeed on-line and logged on with the right user.
It uses PsLoggedOn to verify who is logged on, and Explorer to show a hidden share.
The pipe is needed to verify there is indeed a domain user logged on.
@echo off
for %%m in (Machine1 Machine2 Machine3) do call :show %%m
goto :pause
:show
echo %1
%~dp0PsLoggedOn -L \\%1 2>&1 | find /I "MYDOMAIN\"
start explorer /e,\\%1\NarrowCast$
goto :end
:pause
pause
:end
–jeroen
Posted in Batch-Files, Development, Power User, Scripting, Software Development, Windows, Windows 7, Windows Server 2003, Windows Server 2008, Windows Vista, Windows XP | 4 Comments »
Posted by jpluimers on 2012/10/08
Most console applications return 0 (zero) as success.
But sometimes there are multiple success result codes, and the success depends on what you want to do with them.
One example is RoboCopy.
The zero result code means that nothing happened: no error occurred and nothing was copied, because there was no need to.
But for most RoboCopy scenario’s result code 1 (one) is also success. It means that no error occurred and that one ore more files were copied.
In fact the RoboCopy result codes form a bitmap explained on ss64.com.
Most RoboCopy use cases will have [0,1] as the set off success result codes.
–jeroen
via: Robocopy Exit Codes.
Posted in Batch-Files, Development, Power User, RoboCopy, Scripting, Software Development, Windows, Windows 7, Windows 8, Windows Server 2000, Windows Server 2003, Windows Server 2003 R2, Windows Server 2008, Windows Server 2008 R2, Windows Vista, Windows XP | 3 Comments »