Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

Help with a batch file


  • Please log in to reply

#1
diggeryo

diggeryo

    Member

  • Member
  • PipPipPip
  • 349 posts
I am running Windows 7 Enterprise 64-bit. For about five hours now, I've been trying to get a batch file to work, but I can't seem to figure it out completely.

I have a Truecrypt file (called Work.tc) that contains a Microsoft Word document (called Work.docx). What I am trying to do is create a batch file to do the following: It attempts to mount the Truecrypt file Work.tc to drive letter W:. When prompted to enter my password, if I click cancel to exit Truecrypt, the batch file should simply exit. However, if I enter the correct password, the batch file should then open the Work.docx document. Once I exit out of the Work.docx document, the batch file should then dismount my Truecrypt file from drive W:.

I have gotten it to work *most* of the time. However, if I attempt to run this batch file when I already have another Word document open, the batch file attempts to dismount the Truecrypt volume as soon as it open the Work.docx document.

So, in other words, if I don't already have any Word documents open, the batch file works as intended. If I do already have any Word documents open, it does *not* work properly.

Any help would be *extremely* appreciated!!!! Here are the contents of the batch file:


"C:\Program Files\TrueCrypt\TrueCrypt.exe" /v "C:\Users\**USER NAME**\Documents\Work.tc" /lw /b /m ts /q
If Exist W:\Work.docx. GOTO Open_Work
GOTO End
:Open_Work
W:\Work.docx
:Wait_til_done
Tasklist /FI "WINDOWTITLE eq WORK.DOCX [Compatibility Mode] - MICROSOFT WORD"
SET WWLevel=%ERRORLEVEL%
If %WWLevel%==1 GOTO Wait_til_done
"C:\Program Files\TrueCrypt\TrueCrypt.exe" /d w /q
:End
  • 0

Advertisements


#2
Ehsanit

Ehsanit

    Member

  • Member
  • PipPipPip
  • 163 posts
What about checking for the presence of the temporary file created by word (W:\~$rd.docx) which it should delete when it closes.
If exist W:\~$rd.docx GOTO Wait_til_done

I would also point out that running the check on such a tight loop would degrade the performance of your machine. You can easily get away with only checking once per second. Unfortunately Batch doesn't have a sleep or wait command, but a common hack is to use the ping command.
PING 1.1.1.1 -n 1 -w 1000 > nul

Edited by Ehsanit, 03 December 2010 - 04:39 AM.

  • 0

#3
diggeryo

diggeryo

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 349 posts
Hey Ehsanit--

Thanks for the reply.

In one of my earlier versions of this batch file, I did check for the temporary file created by Word. However, it occurred to me that due to any number of reasons (loss of power, Windows shutting down without shutting down Word first, etc.), that temporary file might not be deleted. This would then cause it to always be found, whether Work.docx is open or not. So I went the route of trying to find the task in the tasklist.

Thanks for the FYI about using a "wait" command. I will add that to my next version.

Still, though, I can't find any reason why it doesn't want to work correctly if a Word document if already open.
  • 0

#4
Ehsanit

Ehsanit

    Member

  • Member
  • PipPipPip
  • 163 posts

Still, though, I can't find any reason why it doesn't want to work correctly if a Word document if already open.

Probably related to the fact that there is only one winword.exe task running, which oversees all the documents. I don't know what tasklist queries for the title, but I'd guess that it isn't cleared until the process is closed.


Your concern about the file not being deleted on an unexpected shutdown is valid. I did mull over it before posting the suggestion, but it seems to me that the solution was equally obvious.

:Open_Work
if exist w:\~$rd.docx del W:\~$rd.docx
W:\Work.docx

Edited by Ehsanit, 03 December 2010 - 10:31 AM.

  • 0

#5
diggeryo

diggeryo

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 349 posts
It worked!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! You are a life saver!! Not only was I working on this for about 5 hours last night, but I also spent many days some months ago trying to get it to work before getting aggravated with it and stopping. Thank you, thank you, thank you!!!

For your information, and in case anyone else wants to do something similar, here are the final contents of the batch file:
(by the way, I used the choice command to get the batch file to wait. And I have it waiting 5 seconds, not 1.)

"C:\Program Files\TrueCrypt\TrueCrypt.exe" /v "C:\Users\**USER NAME**\Documents\Work.tc" /lw /b /m ts /q
If Exist W:\Work.docx. GOTO Open_Work
GOTO End
:Open_Work
if exist w:\~$Work.docx del W:\~$Work.docx
W:\Work.docx
:Wait_til_done
choice /d y /t 5 > nul
If exist W:\~$Work.docx GOTO Wait_til_done
"C:\Program Files\TrueCrypt\TrueCrypt.exe" /d w /q
:End
  • 0

#6
Ehsanit

Ehsanit

    Member

  • Member
  • PipPipPip
  • 163 posts
I had never come across the choice command. Thank you. I like learning.

Glad your sorted. I know how annoying it is to have something of this nature bugging you, and I wouldn't wish it on anyone.
  • 0

#7
diggeryo

diggeryo

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 349 posts
Hi Ehsanit--

(I know this is a lot to read, but I wanted to make sure I was as detailed as possible with my new issue here.)

I hope you (or perhaps someone else in the know) come across this thread again, because I am now having another small problem with this batch file, and I could use some advice.

First, here is the batch file in its current version (NOTE: I placed numbers at the beginning of each line only for easy reference in this thread; they do not appear in the actual batch file):

01-"C:\Program Files\TrueCrypt\TrueCrypt.exe" /v "C:\Users\**USER NAME**\Documents\Work.tc" /lw /b /m ts /q
02-If Exist W:\Work.docx. GOTO Open_Work
03-GOTO End
04-:Open_Work
05-if exist w:\~$Work.docx del W:\~$Work.docx
06-W:\Work.docx
07-:Wait_til_done
08-choice /d y /t 2 > nul
09-If exist W:\~$Work.docx GOTO Wait_til_done
10-"C:\Program Files\TrueCrypt\TrueCrypt.exe" /d w /q
11-:End

SCENARIO 1: If I run the batch file when I have no other Word documents already open, it gets to line 06, opens the Work.docx document, and stays there until I actually close the Work.docx document, then exits. So in this instance when Work.docx is the only Word file open, it doesn't even need to execute the loop in lines 07-09. No problem here.

SCENARIO 2: If I run the batch file when I already have another Word document open, it gets to line 06, opens the Word document, then continues executing the batch file. This is where the loop in lines 07-09 help me in that it will not exit the loop until I exit the Work.docx document. No problem here.

SCENARIO 3: Here is where I *am* running into issues. If I run the batch file when I have no other Word documents already open, it gets to line 06, opens the Work.docx document, and stays there. It is waiting for me to exit the Work.docx document before continuing to execute the batch file. However, let's say with Work.docx already open, I then open up another Word document (for argument's sake, let's call it Second.docx). I open Second.docx as I would a normal Word document (using the OPEN icon), *not* by using a batch file. Now I have Second.docx open but want to exit Work.docx. So I exit Work.docx. However, the batch file still stays on line 06. It won't leave that line (and thus exit) until I actually close Second.docx.

So, in a nutshell, if I execute the batch file without ever having another Word document open, it works fine. If I execute the batch file when I already have a Word document open, it works fine. But if I execute the batch file when no Word document is open, but then open a second Word document, it does *not* work fine.

I am having sort of the opposite problem I was yesterday. Yesterday, when I was having a problem, the batch file would exit prematurely. Today's problem is that the batch file is not exiting when desired.

I was thinking I could change line 06 to say the following instead:

"C:\Program Files\Microsoft Office\Office14\WinWord.EXE" W:\Work.docx

and then use a switch to force the batch file to continue, but after looking at a list of switches available ( http://support.microsoft.com/kb/210565 ), I don't think any would help me.

If you have any ideas regarding the switches or any other suggestions, your help would be extremely appreciated.

thanks,
mike.
  • 0

#8
diggeryo

diggeryo

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 349 posts
I think I've figured it out!!!!

I've used the START command in line 06: Start W:\Work.docx

When I use this, no matter whether another Word document is open or not, it will execute line 06 and move on to the next line of code in the batch file.
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP