The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,854 other subscribers

Archive for the ‘Scripting’ Category

Get last command line argument in windows batch file – via: Stack Overflow

Posted by jpluimers on 2011/12/29

Sometimes you want to parse commandline arguments in batch files starting at the last one.

For parsing them left to right, the shift command comes in handy.

But there is no “shift-reverse” command, so you need some trick.

StackOverflow to the rescue: user Joey provides this really nice answer:

The easiest and perhaps most reliable way would be to just use cmds own parsing for arguments and shift then until no more are there.Since this destroys the use of %1, etc. you can do it in a subroutine

@echo off
call :lastarg %*
echo Last argument: %LAST_ARG%
goto :eof
:lastarg
set "LAST_ARG=%~1"
shift if not "%~1"=="" goto :lastarg
goto :eof
:eof

–jeroen

via: Get last command line argument in windows batch file – Stack Overflow.

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

Windows: killing the Zone.Identifier NTFS alternate data stream from a file to prevent security warning popup

Posted by jpluimers on 2011/11/25

The Zone.Identifier NTFS alternate data stream (ADS) is appended to Internet downloads by browsers, and inserted by most decompressors when expanding such downloads.

NTFS alternate data streams are a perfect way to hide data, support Mac OS data forks (which used them to support resource fork meta data tagging long before NTFS alternate data streams were introduced), or to append meta-data to files.

It is a known ADS used to show a security warning when you run executable content that has been downloaded.
That warning can be annoying, or hang your application which it is started from a service, so further below is a batch file that kills the stream.

You cannot use type for displaying NTFS alternate data streams, but redirection through more or using notepad is fine.

This shows the Zone.Identifier NTFS alternate data stream for a single file:

more < %1:Zone.Identifier

When you want to see the ADS of many files, then just use NirSoft’s AlternateDateStreams utility.

You should only kill an Zone.Identifier NTFS alternate data stream when you have verified that the downloaded executable content (which nowadays is much more than just an executable) is verified to be safe.

An easy way to kill any NTFS alternate data stream is to copy it to a FAT32 device and back: FAT does not support alternate data streams. Drawback: it modifies the timestamp of your file as FAT has a smaller time resolution than NTFS has.

This batch file kills  the Zone.Identifier NTFS alternate data stream using the SysInternals streams tool:

@echo off
  if !%1!==!! goto :end
  :: use caret before pipe to hide the pipe from the outermost command in the batch file
  for /f "usebackq tokens=1" %%d in (`streams.exe %1 ^| find "Zone.Identifier:$DATA"`) do (
    goto :kill
  )
  goto :end
:kill
  streams -d %1
:end

and this batch file lists the Zone.Identifier NTFS alternate data streams:

@echo off
  if !%1!==!! goto :end
  :: use caret before pipe to hide the pipe from the outermost command in the batch file
  for /f "usebackq tokens=1" %%d in (`streams.exe %1 ^| find "Zone.Identifier:$DATA"`) do (
    goto :list
  )
  goto :end
:list
  streams.exe %1 | find ":"
:end

Note that the $DATA in the above batch files is not part of the NTFS alternate data stream name, but explains what kind of data is in the stream.
I have not found other types yet, but if you do, please leave a comment (preferably with a link).

–jeroen

Posted in Batch-Files, Development, Power User, Scripting, Software Development, Windows, Windows 7, Windows Vista, Windows XP | 2 Comments »

Zipping .lnk files

Posted by jpluimers on 2011/11/04

.lnk files are very usefull as they can link to both files and directories.

However, somehow the GUI tools for zipping .lnk files tend to compress the .lnk target, not the .lnk itself.
I tried the Windows Explorer, WinZIP and 7zip GUIs to no avail.

The 7za command-line to the rescue:

7za a -tzip lnk-files.zip *.lnk

Unpacking the zip using the GUI works fine though.

You can get the latest 7za here.

–jeroen

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

TODO: batch file to properly backup Skype history

Posted by jpluimers on 2011/10/27

This is on my todo list: Smarter Skype Backups | Universe’s Blog.

–jeroen

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

batch files: getting information from your Windows AD

Posted by jpluimers on 2011/10/13

If you have the Windows Server Resource Kit tools installed, then dsget and dsquery can get you some valuable information about the current user and computer.

Below is a sample batch file that shows that dsquery gets you [Wayback/Archive] DNs, and dsget gets you detailed information for a certain type of DN.

You can use pipes (and sometimes you need to [Wayback/Archive] escape the pipes) to pass information from dsquery to dsget.
dsget will happily accept multiple DNs (each on a new line), so you can use text-files with DNs too.

@echo on
    :: this assumes that dsquery, dsget, etc are in the same directory as the batch file
    ::escape pipe with caret
    for /f "tokens=* delims= " %%a in ('%~dp0dsquery user -samid %USERNAME% ^| %~dp0dsget user -desc ^| find /V "dsget succeeded"') do (
    set description=%%a
    )
    ::trim last two spaces
    if "%description:~-2%"=="  " set description=%description:~0,-2%
    echo !%description%!

–jeroen

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

Batch file to get parent directory (not the directory of the batch file, but the parent of that directory)

Posted by jpluimers on 2011/08/30

The problem solved here is two-fold:

  1. The ~dp syntax for getting the directory/path part only works for parameters and loop indexes, not on variables.
    You can work around this by having a 1-iteration for-loop.
  2. The :~ syntax for getting the substring works only for variables, not for parameters and loop indexes.
    You can work around this by assigning to a temporary variable.

Example:

  echo batchfile=%0
  echo full=%~f0
setlocal
::http://stackoverflow.com/questions/636381/what-is-the-best-way-to-do-a-substring-in-a-batch-file
  set Directory=%~dp0
echo Directory=%Directory%
:: strip trailing backslash
  set Directory=%Directory:~0,-1%
echo %Directory%
::  ~dp does not work for regular environment variables:
::  set ParentDirectory=%Directory:~dp%  set ParentDirectory=%Directory:~dp%
::  ~dp only works for batch file parameters and loop indexes
  for %%d in (%Directory%) do set ParentDirectory=%%~dpd
  echo ParentDirectory=%ParentDirectory%
endlocal

This will show:

  1. The directory of the batch file
  2. The directory but without the trailing backslash
  3. The parent directory of the batch file

Hope you can give this some use.

–jeroen

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

Batch file to get TCP/IP info from local LAN

Posted by jpluimers on 2011/08/24

I needed an automated way of inspecting the local LAN.

The batch files below will on IPv4 networks, with thanks to articles from Windows IT Pro and Rob van der Woude for some ideas:

  • find the TCP/IP gateways/netmasks
  • enumerate all the IP addresses on each subnet (assuming the netmask is 255.255.255.0)
  • ping each IP address and get ARP info, and dump that to the console

There are other tools that can do this too (like Angry IP Scanner), and more but the ones I tried could not copy the output to the clipboard.

find local subnets: Read the rest of this entry »

Posted in Batch-Files, CommandLine, Development, Power User, Scripting, Software Development | 4 Comments »

batch files: getting directory and parent directory

Posted by jpluimers on 2011/08/09

In some situations, batch files are the only thing you have.

In this case, I needed the parent directory of a batchfile.

i.e. not the directory of the batch file itself, but the

  echo batchfile=%0
  echo full=%~f0
setlocal
::http://stackoverflow.com/questions/636381/what-is-the-best-way-to-do-a-substring-in-a-batch-file
  set Directory=%~dp0
echo Directory=%Directory%
:: strip trailing backslash
  set Directory=%Directory:~0,-1%
echo %Directory%
::  ~dp does not work for regular environment variables:
::  set ParentDirectory=%Directory:~dp%  set ParentDirectory=%Directory:~dp%
::  ~dp only works for batch file parameters and loop indexes
  for %%d in (%Directory%) do set ParentDirectory=%%~dpd
  echo ParentDirectory=%ParentDirectory%
endlocal

The point is that the %~dp0 trick as explained in this StackOverflow answer on substrincgs in batch files only works for batch file parameters (starting with a single percentage sign: %0, %1, %4, etc) or for-loop indexes (starting with double percentage signs: %%1, %%d, etc). They don’t work for getting path portions of  regular environment variables.

So I used the substring trick (as explained in the same answer), and then used a for loop (which will have one iteration) to get the path portion.

Note: The substrings trick only works on regular environment variables, not on parameters and loop indexes.

Note 2: I used setlocal/endlocal so the changed environment variables stay local to the batch file and won’t leak out to your command-prompt. If you need the value there, then remove the setlocal/endlocal, or use an “endlocal & set” command on a single line. Read the rest of this entry »

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

Delphi Labs: DataSnap XE and jQueryMobile; a Web Frontend accessing DataSnap through a Delphi WebBroker server

Posted by jpluimers on 2011/03/22

[Wayback] Paweł Głowacki recently released two very interesting blog articles, together with 5 (five!) demonstration videos on how to get your mobile device to talk to a DataSnap backend using [Wayback] jQueryMobile so you get a very native look & feel UI on your mobile device without putting a lot of effort in writing a native device app.

  1. [Wayback] Part 1: Delphi Labs: DataSnap XE – WebBroker jQueryMobile Boilerplate – DelphiFeeds.com.
  2. [Wayback] Part 2: Delphi Labs: DataSnap XE – jQueryMobile Web Frontend – DelphiFeeds.com.

Highly recommended!

–jeroen

Read the rest of this entry »

Posted in Delphi, Development, JavaScript/ECMAScript, jQuery, Scripting, Software Development | Leave a Comment »

They should have pushed object oriented programming a lot more a lot sooner – PHP: Manual Quick Reference

Posted by jpluimers on 2011/03/02

Boy I wished the PHP guys have pushed object oriented programming a lot more a lot sooner.

–jeroen

Via: PHP: Manual Quick Reference.

Posted in PHP, Scripting | Leave a Comment »