Thursday, February 18, 2010

Script: Some useful batch file commands

Get directory where batch file is located

Use %~dp0 to get the directory of the batch file. Example, to change the current directory to that of the batch file: CD /d "%~dp0"

Redirect stdouput and stderror to single file

e.g. auditpol.exe /backup /file:c:\nofolder\auditbackup.csv > c:\abc.txt 2>&1

Wednesday, February 17, 2010

Script: Print file versions

Following is the VBScript for printing the versions for all the files in a directory:


' Pass the folder path as commanline argument.
' e.g. cscript D:\Temp > c:\files.csv
' Use the above format to run the script otherwise each
' and every file will b displayed in a messagebox

' Should do argument validation !!!!
strFolderName = WScript.Arguments.Item(0)

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject ("Shell.Application")
Set objFolder = objShell.Namespace (strFolderName)

For Each folderItem in objFolder.Items
        If "Application Extension" = folderItem.Type Or "Application" = folderItem.Type then
              Wscript.Echo folderItem.Path & "," & objFSO.GetFileVersion(folderItem.Path)
        End If
Next


PrintFileVersions.vbs is also available on google doc.