Search This Blog

Tuesday, April 26, 2011

Running a batch file from Service Recovery

We had an issue with a third party program that every now and again would suddenly fail. It was running as a service, now we wanted to be alerted to this so I thought I could just include a previously written batch file (slightly modified for the new purpose) in the service recovery “Run a program” section and have it alert us using email.

However I soon discovered that running a batch file from the “Run a Program” option in the service recovery tab does not work.

See here…. (This behaviour is by design…. Smile super…)

http://support.microsoft.com/kb/247929

Anyway so I thought, no worries I will use the work around forcedos…. However there is a character limit on the command, which pretty much rules this out for my purpose.

FORCEDOS: Command line too long
The command line cannot be longer than 125 characters.

So I struggled to see how I was going to get round this….

Then I thought about “scheduled tasks”… and this is how I solved the problem. The batch file is setup to run as a scheduled task with no schedule.

Then in the Service Recovery tab for the service, the program is set to

“schtasks”

and the command line parameters set to

“/RUN /TN "<taskname>"”

This now works a treat… A pain but I think a nice work around, as most of the other batch files I have are scheduled in the scheduled task manager the finished work around actually fits in quite nicely.


Share/Bookmark

Sunday, April 03, 2011

Batch File: Split Compressed Archive based on Folders not size

I have had a batch file that would use 7zip to compress and encrypt an archive. The folders that were compressed contained millions of files, and I had already moved to the 64 bit version of 7zip to get me around a previous limit. Whilst this has been working fine for months, the folders and files are continuously growing and the other day it encountered an error stating

“System error:
Not enough storage is available to process this command.”

Now this error is not related to disk space but I believe its related to server resources. I did some initial research and I found an article about IRPstack size which looks interesting as to potentially fixing this, however the archive itself was becoming unmanageable so I decided to create a split archive to create smaller archives.

Now normally you can just use the split archive functionality built into 7zip, however this works on size and creates 1 archive split into smaller files. This was not what I wanted. I wanted to split the archive based on folder structure.

C:\rootfolder

\folder1
\folder2
\folder3

I wanted to create archive for each of the folders within the rootfolder.

So I came up with a batch file that loops through the rootfolder and takes each folder and runs the archive on that folder.

Before getting into the code I have this batch called from another batch file that sets up some standard variables, like dates and program locations.

%c_DateYYYYMMDD%
%c_TimeHHMM%
"%c_7zipFolder%

@echo on
SETLOCAL ENABLEDELAYEDEXPANSION
title %c_jobName%

SET sSourceFolder=C:\rootfolder
SET sZipFilesArchiveFolder=archive\files
SET sZipFilesArchiveFileName=archivefilename

SET sRemoteServer=remoteserver1
SET sRemoteDrive=D

echo on

REM Remove the double quotes from the front and end of the root path
REM REMOVE quotes using string substiution
SET sSourceFolder=%sSourceFolder:"=%

FOR /F "DELIMS==" %%d in ('DIR "%sSourceFolder%" /AD /B') DO (

REM Set up filename for archive

SET c_ZipFilesArchive=%c_DateYYYYMMDD%_%c_TimeHHMM%_%ComputerName%_%sZipFilesArchiveFileName%_%%d_X64.7z

REM Run 7zip

"%c_7zipFolder%\7z" a -t7z -mhe=on -mhc=on -w"\\%sRemoteServer%\%sRemoteDrive%$\%sZipFilesArchiveFolder%" -p"password1" "\\%sRemoteServer%\%sRemoteDrive%$\%sZipFilesArchiveFolder%\!c_ZipFilesArchive!" "%sSourceFolder%\%%d"> “.\7zipoutput.log"

)
ENDLOCAL

So the batch file is pretty simple, but things to note are you have to use delayed expansion with the for loop to set the variable contents (hence the variable surrounded in !! and not %%, in addition the SET ENABLEDELAYEDEXPANSION at the beginning.

The script loops through the folders returned by the DIR /AD /B command, sets %%d to the folder name and that is used within the for loop to name the archive and archive the contents.

The script sets the working file for the compression to the remote location.

References

Error message: "Not enough server storage is available to process this command"

Not enough server storage


Share/Bookmark