CJSmith dot me

Just another blog

Category: Batch File Scripting

Display advanced properties of scheduled tasks in command line

Published / by Chris Smith / Leave a Comment

The following command requests a detailed display of the tasks on the local computer. It uses the /v parameter to request a detailed (verbose) display and the /fo LIST parameter to format the display as a list for easy reading. You can use this command to verify that a task you created has the intended recurrence pattern.
Before running increase the command prompts screen buffer (Right click the Window, go to properties, go to Layout and set the Screen Buffer Height to 9999). If this is not done you will find due to the amount of Windows scheduled tasks, that the tasks you want are cleared from thje screen buffer.

 
SchTasks.exe displays a detailed property list for all tasks. I normally will then copy everything into the clipboard (Ctrl + A) and pout it into a text editor, I then delete anything after “Folder: \Microsoft” so I then only have my list of scheduled tasks.

Credit: Technet

A simple batch file which compresses every folder into a 7z Archive

Published / by Chris Smith / Leave a Comment

For backing up old files I tend to sort into lots of different folders, anything I am finished with I put in a 7zip Archive
The following batch makes use of a For loop in the batch, uses 7-zip with Ultra Compression and names the archive the same as the folder.

Credits: SS64

Rebuild a V1 DBCapture Database Queue

Published / by Chris Smith / Leave a Comment

We have an issue where on a rare occasion a scan from V1 DBScanner to DBCapture can corrupt the queue and cause DBCapture Admin to state it is unable to open a document rendering the whole application unusable.
I have a batch script to perform some maintenance. Over time I have commented out lines which are not required. I have left my change drive to d script as this is normally where server applications are installed. I have replaced our port with as this can be different per install (As well as having more than once instance of the service on different ports)
After the queue is rebuilt the program actually starts the server as normal, in my batch the program is executed in a seperate process and then killed off after 400 seconds (As long as the rebuild is complete then V1 is fine with this, if running line by line press Ctrl + C to close the program instead). Depending on the amount of documents in the queue it may need to be increased.

Credits:
SS64 for recommending Ping instead of Wait – SS64
Datel Support for providing the command to rebuild the queue – Datel

Simple error checking when running batch files from T-SQL in SQL Server

Published / by Chris Smith / Leave a Comment

A while ago we added error trapping to the batch file called by a SQL script, this was to ensure if a program failed the rest of the process would not run:

After ensuring the program would return an error level if something went wrong we then I then used the ErrorLevel and an if statement. If the error level is equal to or greater than 1, I make it go to the end of the file and exit with code 1 (By default its zero which normally means the batch or program executed as expected)

We then modified our existing code in SQL to ensure the batch program is run and the exit code is captured in a variable. It can then be dealt with and the example below can easily be changed to suit whatever is required.
 

Credits:
SS64
Microsoft MSDN (Example Set C)

Within a batch file run a program for a certain length of time before killing it

Published / by Chris Smith / Leave a Comment

I needed to run a scheduled batch job and kill it off after a certain amount of time, this was because the program would run what it needed and then run as normal (If it exists then there is no reason to use start or timeout). As it actually ran as a service I didn’t want to leave it running in this way.
As I use Taskkill on the programs exe name, obviously don’t use it if it will be run on a server where it is running other executables with the same program name.
I replaced timeout with ping as the process launched fully utilises the CPU cores.

Credits: I replaced my use of timeout based on a suggestion on SS64.com

Synctoy 2.1 setup batch script

Published / by Chris Smith / Leave a Comment

This code was used to speed up the setup of SyncToy 2.1 (I see no reason to backup the entire user profile folder, but I hate having to setup multiple sync jobs). As Microsoft has not updated SyncToy it may not work on anything other than Windows 7.
The script expects SyncToy 2.1 is installed and that the destination is always the H drive (For whatever reason I am not using a variable for the main location, I may update this at some point as code to select another path is already in).

Simple backup and restore scripts

Published / by Chris Smith / Leave a Comment

Back when my workplace migrated from Windows XP to Windows 7, I became lazy and automated backing up and restoring a users profiles important files in a batch script.
Here I backup to the drive H, this drive is pretty much our standard user drive. If the user does not have a H drive some pretty neat code from https://groups.google.com/forum/#!topic/alt.msdos.batch.nt/kOsN-QIOYEY displays a folder selection box (As annoyingly some users didn’t have their home drive set as H). Although I have set the filepath as LaptopBackup I have used it on Desktops as well.
The code deliberately terminates if it detects it isn’t on Windows 7, this is purely because I haven’t tested this in a network environment on other OS’s!
Known faults are pinned shortcuts in the taskbar don’t appear and printer settings don’t migrate (I never got around to correcting it).

This section of code is to backup to %backupdir%\LaptopBackup\

This section of code is to restore from %backupdir%\LaptopBackup\

Update August 2017. I have found Windows 10 Sticky Notes needs to be moved to a different location so data can be imported, the below has been added as where I work is currently planning to migrate from Windows 7 to Windows 10.
Further amendments may be needed and obviously the backup script above doesn’t account for the new data location for Sticky Notes