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,860 other subscribers

Archive for the ‘Batch-Files’ Category

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 »

Formatted sourcecode in WordPress: uses SyntaxHighlighter 3.0; complete list of supported languages

Posted by jpluimers on 2011/01/18

In the past I wrote a few blog posts on posting sourcecode in WordPress.

Nick Hodges‘ last Flotsam and Jetsam blog post pointed me to the SyntaxHighlighter JavaScript that is used by WordPress and many other engines/sites.

Their site contains an even more elaborate list of supported languages.

I had the basic list right in my last post, but was missing all the aliases (which often are easier than the longer proper names).

This is the new table adapted from their list: Read the rest of this entry »

Posted in .NET, Batch-Files, C#, CSS, Database Development, Delphi, Delphi for PHP, Development, HTML, HTML5, Java, PowerShell, RegEx, Scripting, SQL, VBS, Web Development, WordPress, XML, XML/XSD, XSD | 5 Comments »

batch file scripts to get current date and current time in sortable ISO 8601 format

Posted by jpluimers on 2011/01/10

ISO 8601 is a great format for date and time (and combined) values.
It allows for both interchange of information, and ease of sorting values.

Recently, I had to create some backup and logging scripts for a 3rd party turn-key installation at a client.
You know: the kind of installation where the 3rd party manages to break their own scripts, but at the mean time close the system so much, that you cannot do anything but standard batch-file scripts.

The system runs partially on a Workstation that is based on a Dutch version of Windows XP, and a server that runs an English version of Windows Server 2008.
Recipe for some twiddling in order to keep the scripts working on both systems, and not to get bitten by localization.

This answer to a StackOverflow question got me a nice head-start: it was said to work in both the English and Portugese versions of Windows.
This post is the process to get correctly function batch-files towards the end of the post.

Of course, we Dutch are persistent enough to have yet different output for the %date% pseudo variable and the date and date /t commands.
The same holds for the %time% pseudo variable and the time and time /t commands.
Read the rest of this entry »

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

Batch file tricks – double quotes splitting and downloading latest 7-zip

Posted by jpluimers on 2010/09/02

I needed a quick means to download the latest 7-zip from the command-line in Windows.

This batchfile makes use of these tools:

7-zip has a download page that contains lines like these:

    <TD class="Item" align="center"><A href="http://downloads.sourceforge.net/sevenzip/7z465.exe">Download</A></TD>

Read the rest of this entry »

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