Test if Directory exists in Batch file (.cmd)

February 16, 2012

My previous post on testing network drives led me to further research the topic, and I came to quite surprising (at least for me) results: the result if a check with IF EXIST depend on whether

  • the drive is a local drive or a mapped network drive or a UNC path
  • the path contains spaces or not
  • the path is quoted or not
  • cmd runs in administrator mode or user mode

I wrote a small batch file that contains a couple of assignments of the form

set dir=c:\temp
set dir=c:\temp\with spaces
etc.

and executed these tests on each value

if exist %dir% echo exists
if exist %dir%\nul echo exists
if exist %dir%\. echo exists
if exist "%dir%" echo exists
if exist "%dir%\nul" echo exists
if exist "%dir%\." echo exists

These are the results

directory %dir% %dir%\nul %dir\. “%dir%” “%dir%\nul” “%dir%\.”
local x x x x x
local (spaces) x x
mapped (non-admin) x x x x x
mapped (non-admin, spaces) x x
UNC x x x x x x
UNC (spaces) x x x

Comments:

Testing directory path containing spaces can only be performed using the quoted notation.

Mapped network drives can only be access in non-administrator mode (see these threads).

The only reliable way to test for directory existence is therefore to use the quoted “%dir%\.” notation.

To check whether cmd runs in administrator mode or not, use an admin statement such as ‘at’:

at >nul 2>nul
if errorlevel 1 echo you are not in administrator mode

Test if Network Directory exists in Batch file (.cmd)

February 16, 2012

The default way to check whether a directory exists in a Windows batch file (.cmd) is

if not exist "%directory%\nul" (
   echo %directory% does not exist
)

However, as this MS KB explains, the check for the NUL file does not work with directories on network drives mapped to a drive letter.

A working solution I found is to process the ERRORLEVEL value a DIR command sets

dir %directory% >nul 2>nul
if errorlevel 1 (
   echo %directory does not exist
)

or

dir %directory% >nul 2>nul
if not errorlevel 1 (
    echo %directory exists
)

Also note that mapped network drives are not available in administrator mode, as is discussed in these threads.


New (empty) Window in IE9

January 25, 2012

If you press Ctrl-N in Internet Explorer (e.g. IE9), it opens a new window with the same web page. So how do you open a new empty window?

This thread lists 3 methods to open a new IE window from the taskbar:

  • Right-click an IE icon, and select “Internet Explorer”
  • Shift + Left-click on an IE icon
  • Middle-click on an IE icon

Of course, you can also select Internet Explorer from the Program Menu to get a new window.


xcopy to c:\inetpub\wwwroot fails with “Access denied”

January 25, 2012

Rather than manually copying files from \\tsclient\some\path to c:\inetpub\wwwroot\webdir I wanted to write a small batch file using xcopy and /exclude to deploy web application files.

However, even when starting the .cmd from a command line in administrator mode, I received an “Access denied” message for each file to be copied due to User Account Control prohibiting write access.

As it turns out, the task can be successfully performed using robocopy with the /zb (or /b ?) switch. (Use robocopy /? to find the huge collection of switches.)

Consequently, the feature known as xcopy deployment should therefore be renamed to robocopy deployment 😉


Extracting .msi Files without Installation

September 19, 2011

Occasionally you need to access the contents of an .msi file, but you are sitting on a PC where you do not administrative rights to run the installer.

I noticed 7-Zip adds itself to the context menu of .msi files, but it extracts the whole contents of the msi, and the output is somewhat confusing to me (admittedly, I do not use the latest version of 7-Zip).

Fortunately I came across lessmsi which opens an .msi file, allows you to select the desired files, and extracts them into the file’s sub-directory as found in the Directory column of each of the contained files.

Simple and effective!


cmd.net: uniq

June 27, 2011

Finally the command that prompted the creation of cmd.net in the first place: uniq.

Before implementing the command, I had to find out which options the default Linux implementations provided.

I used these pages as guideline:

The resulting command filters unique lines from stdin (or a piped input file) to stdout, and supports these options:

uniq [parameters]

displays unique lines in sorted file

-u      unique lines only
-d      repeated lines only
-c      display line count
-i      ignore case
-f      [ignore fields]         ignore number of fields
-s      [skip characters]       skip number of characters
-w      [compare characters]    compare number of characters
-in     [encoding]      input encoding
-ci     [culture]       culture info
-out    [encoding]      output encoding

uniq 0.10.4117.37457
cmd.net 0.10.0.0 (c) by devio.at 2011

Note that uniq only analyzes subsequent lines of text in the file. Thus it only operates correctly on a pre-sorted file. If the input is not sorted, equal lines may be output more than once (but not subsequently).

The first version of cmd.net is available for download here, and contains the commands described in this post.


cmd.net: tee, xslt

April 18, 2011

The tee command of the cmd.net collection is a .Net implementation of the tee command available on other platforms.

As usual, the -in and -out switches define the encodings of stdin and stdout.

The filename parameter is given directly (without a leading switch), and the file’s encoding can be defined using the -enc switch.

The -a switch appends to an existing file instead of creating a new file.

The xslt command is a translation of the PowerShell xslt script I posted some time ago.

The xml file is provided on stdin, and the xslt file is defined via the -xslt switch (along with -xsltenc for the encoding).

Again, the -in and -out switches define the encodings of stdin and stdout.

The current version of cmd.net is available for download here.