Help with .bat file

Soldato
Joined
7 Jan 2003
Posts
4,458
Location
Gold Coast, Australia
Hi Guys,

I would like to create a .bat file that runs when the user selects shutdown on the computer.

I would like the bat file to do the following.

-Close Microsoft Outlook 2010 (if it has been left open)
-Copy Outlook Data file from Location on C drive
-Paste Outlook Data file to Location on D drive (overwriting the file already there)
-Shutdown

Is this possible and if so would one of you mind explaining how I can do this and writing the code for me please?

Thanks in advance.
 
I'm probably wrong but I don't think a batch is going to help here. Once shutdown is selected, Windows will go about closing stuff down (Inc Outlook, if it's still running) and the will proceed to shutdown entirely. IIRC file copy procedures will just be cancelled, so your bat doesn't have the chance to finish.

Attacking this from a different angle, what are you looking to achieve? Just PST backup?
 
You can add a custom task to task scheduler to run a a script on shutdown but its a bit unreliable if it will complete before the system shuts down.

You could try setting it up to abort shutdown, use some kind of flag so that it doesn't abort the next shutdown and do its thing then shutdown for real but thats also asking for trouble. (and needs a fair bit of OS hacking to accomplish).
 
Last edited:
I'm probably wrong but I don't think a batch is going to help here. Once shutdown is selected, Windows will go about closing stuff down (Inc Outlook, if it's still running) and the will proceed to shutdown entirely. IIRC file copy procedures will just be cancelled, so your bat doesn't have the chance to finish.

Attacking this from a different angle, what are you looking to achieve? Just PST backup?

I am essentially looking to auto backup the PST file, the user isn't particularly the savvy and probably won't want to back it up manually.

It is a very small business and the user relies on their email for their orders etc. so having their entire email stored on one SSD isn't the best idea.

The reason I want it to happen when the computer is shutting down is because it needs to happen when outlook isn't in use.
 
Ah small business email, such fun. I guess they are using free POP accounts too, right?

They are using POP email but they are paying for web hosting and the company provides them with POP email.

Have a read here and see how far it gets you.

http://highaspirationsinc.com/webde...-automatically-backup-outlook-2010-pst-files/

Or push for them to shell out a whopping £3 /month /mailbox or whatever for hosted Exchange with a reputable firm

Excellent thanks for that, looks like what I am after I shall give it a go next time I am round there.
 
POP isn't really suitable for business use, at least once a business is beyond one person in their bedroom. Throw in several PCs/laptops, smartphones and tablets, the potential for missing/losing email is pretty real. I'd still explore a more robust solution, if anything to give you some piece of mind. Reading between the lines it seems like it will fall at your feet should anything go wrong! I've been there and done that too many times...
Believe it or not I'm fairly anti Microsoft in many respects but Exchange/Office365 email really makes sense for business use.

As always, create a backup before installing anything :)
 
If these PCs are networked, you would be better off backing up to a mapped drive if they have one? Do they log on to a domain or is it just a workgroup?

Do users hotdesk? If so, you might have User A's pst overwrite User B's pst unless you copy to a unique name such as UserA.pst UserB.pst...
You might also need to think about how many backup versions you want. Knowing users, they'll delete emails and want to be able to get some emails back etc...

You could use taskkill /F /IM outlook.exe to close Outlook and then xcopy to copy the pst file.

Not sure how well the batch file would run through task scheduler, but you could try running the batch file using task scheduler on one PC and as a logoff script (see gpedit.msc) on another PC and seeing which works for your needs.

Running via task scheduler allows you to run with admin rights, but I think running batch file via logoff script will run as the user.

Alternatively, if they have a logon script as part of a domain, why not backup at logon before Outlook starts?
 
As a couple of posts up Exchange starts from £2.50 user/month, its going to be so much better for them than pop/imap especially if they rely on it to run their business.
 
I think it could be done with Robocopy,

robocopy runs from the command prompt, and shows the current copying process in the command prompt, so it delays the next command until the copy has finished.

Its pretty old, but does the job well. Its part of the 2003 resource tools.

I'd say something like this.

Code:
taskkill /im outlook.exe

Robocopy "C:\Folder Location" "D:\Folder Location" /XO

Shutdown

I popped the XO on the end, as it will only copy if the destination is older than the source, so if no changes have been made to the PST on C:\ drive it won't bother copying. Which might save some time.

Here is a full list of Robocopy Flags. http://ss64.com/nt/robocopy.html


Looks like it could be done at shutdown via group policy. Or maybe task scheduler when a shutdown event id is triggered.
 
Last edited:
Interfering with shutdown, closing Outlook and copying PSTs sets my spidey sense tingling. If there is a tool for a specific job why bodge it? I'd question the worth of offering support to a company who isn't using proper email tools in the first place too, but I'm old and jaded :p
 
Looks like it could be done at shutdown via group policy. Or maybe task scheduler when a shutdown event id is triggered.

Its a bit of a bodge and unreliable if it actually completes or not if triggered as a shutdown task - some more advanced OS hacking and/or GP setup can be used to interfere with how shutdown works to ensure it happens but can potentially lead to other problems.
 
Interfering with shutdown, closing Outlook and copying PSTs sets my spidey sense tingling. If there is a tool for a specific job why bodge it? I'd question the worth of offering support to a company who isn't using proper email tools in the first place too, but I'm old and jaded :p

You forgot to add 10GB+ PSTs to your list of things to make you shiver :D Gotta love trying to fix a self-destructing PST...
 
Back
Top Bottom