Wednesday, September 14, 2011

BatchFile Scripts

Batch Files

*** Note that this material is intended for people who understand it's use. I will not be held responsible for any result nor held liable for any damages resulting from anyone's use or misuse of any of the information gathered from this website or any of it's contents, including these batch files!

All batch is presented in PLAIN TEXT format and is not executable in any way without your efforts. I hope you know how to download these and make them executable; if not, then I can't help you!

Any requirements and instructions for usage will be in the header of the file itself. Most everything is written and tested only on Windows XP and sometimes Windows Server 2003 or Vista. Most of my batch is written with NT based compatibility in mind, so with the exception of the occasional text parsing from command output that Microsoft might have changed, (and none of that batch is included here to date,) all batch should work on Windows 2000 - Windows 7 without issue, but I guarantee nothing!

Unzip_Nirsoft_Apps - Very simple batch to demonstrate FOR loop usage, but it originally had another purpose. Love apps from www.nirsoft.net? Me too. For a time, they were not available as one packaged download. I configured a weekly schedule task to run batch files using the Linux->Windows port of WGET.EXE and InfoZip's UNZIP.EXE to download all the apps (wget checked for newer versions only) and unzip them into one directory, renaming all the readme.txt files to a %zipfilename%_readme.txt format. This is the portion of that batch that does the unzip/rename operations, with the wget/download part excluded. Why only this part? Because currently the apps are available as one packaged download (See Nir Sofer's blog for NirLauncher) however it does not include all of his apps and readme's. I modified this for someone in a forum who needed the functionality as they would download the missing or newer version apps to add to the main NirLauncher package. Hopefully Nir Sofer will release all apps with auto-update functionality soon.

FindQs - A MUST SEE. Please see the file itself for a full explanation. FindQ's (short for Find Question Marks) seeks out malware hiding behind phony names, which may show up in a console window as having a "?" in the name, where in explorer all you will see is the expected letter. Ex: You may note a folder which appears to be "Symantec" but is actually "S?mantec" and contains a malicious executable inside.

DelZBF - Delete Zero-byte files in any given directory with optional recursion for subdirectories and report logging. I admit it is easy enough to sort by size in explorer or your favorite file manager, and delete all files of a certain size, but normally this is part of an automated batch routine and just saves me TIME. Recently I had to use this as a stand-alone batch on a system with 21,000 phony zero-byte .DLL files in both the Windows and System directories. Well, the 21,000 I used DelZBF on took a good 15 minutes. At first I thought, how inefficient! Next I tested the manual way, getting the sort view in explorer and trying to select them, forget waiting on the delete operation to start, after the same 15 minutes I gave up and ran DelZBF on that directory too.

TestBatch - See where your batch is going wrong; generate a detailed log! This batch file will accept your batch file via parameter including Drag and Drop, and generate a detailed report on your batch helping you to troubleshoot any problems you may have. Provides at-a-glance information about the file itself and it's execution environment, in addition to it's actual execution. Read notes in the header!

ArchiveWebLogs - Originally I wrote this in a pinch for a web server with out of control log files, this will archive files older than xx days across multiple folders by using rar.exe (optionally you can replace this with your favorite archive utility without fear of a botched command line parameter, because the date logic is done entirely within the batch, not the archive program!!!) Very organized code with short but meaningful comments, good to learn with!

Start_Services - better than "net start servicename" this quick batch will stop services prior, wait xx time, start and wait xx time before moving on to the next service, retest to see if they failed xx number of times, retry xx number of times to start them if failed. I recently had to write a quick batch to start some services in a particular order and wait a certain amount of time before starting subsequent services. In my allotted time for the job I just decided to make the script a little more flexible. Easy configuration inside the batch itself, separate from the main code.

No comments: