Why do .bat scripts get locked after execution in Windows 7?

4

1

Ever since I moved to Windows 7, whenever I am am developing a .bat script I have exactly one chance to get it right. As soon as I execute the script, it becomes locked by an unknown process even if the execution appears to have completed, and I have to force quit explorer or restart to be able to edit the .bat file again.

For example, if I create a .bat file,

@echo off
@start prog\ConsolePortable\ConsolePortable.exe

then realize after executing it that I needed to change some aspect of the script,

@echo off
@cd prog\ConsolePortable
@start ConsolePortable.exe

Then I will not be able to save my changes. For example, in gvim I get the message, "'console.bat' is read-only (add ! to override)," and then when I try to override, I get the message, "E212: Can't open file for writing."

Surely there must be some way of scripting without requiring ProcessExplorer on every computer I use. How does one find the process of a .bat file to stop it, anyway? I tried ProcessExplorer and BSOD'd the compy trying to stop it. I have also tried using cmd /c in place of start.

EDIT: ConsolePortable starts fine here and appears to exit fine, however the following test setup does not freeze the editor:

// Program.java
class Program {public static void main(String[] args) {System.exit(0);}}

// startProgram.bat 
echo Starting Program in Java ...
start java Program
echo exiting bat file ...

EDIT 2: Perhaps I should mention that I created the file by using the touch command in cygwin. After restarting the computer, I can't duplicate this problem, though I have encountered it on other computers before.

Peter Nore

Posted 2011-06-13T10:21:14.147

Reputation: 1 642

Do you test the batch file in a fresh cmd window? I'm not exactly sure what the syntax would be, but you could add a command at the end of the .bat to spawn a fresh cmd window (for the next run) and kill the old one, which should free up the file. – jonsca – 2011-06-13T10:31:30.997

I had not tried that; I had been double clicking on it. I will try that ... – Peter Nore – 2011-06-13T10:34:14.113

3

I would be looking at your "ConsolePortable.exe" program and make sure that it is exiting properly. Also, have a look at Why won't cmd exit after execution of batch file?

– Mokubai – 2011-06-13T10:35:52.610

1Where are you saving the .bat files to? If it's in a sub-folder of Program Files, Win7 doesn't allow editing by default. I have had it, on occasion, allow me to add new files, but not save or edit existing files in those folders. – BBlake – 2011-06-13T11:53:56.173

@Mokubai I have had a look at that link; misterjaytee recommends using start as above. How do I make sure an .exe is exiting properly? Is there an error log somewhere? – Peter Nore – 2011-06-13T12:04:10.357

That's what I thought start should be doing, but it appears that in this case there is something weird going on and that program is the only thing that could be doing it. – Mokubai – 2011-06-13T12:08:12.380

You can use aforementioned Process Explorer to find out which process locks your .bat file (Menu|Find|Find a Handle or DLL command or simply press Ctrl+F and type in the name of your script). Then it should be clearer what to do. One thing I can tell at the moment is that cmd.exe itself does not lock .bat/.cmd files it executes. – Igor Korkhov – 2011-06-13T12:22:29.630

Answers

3

I would say this is possibly an error with your editor, or you have weird permissions set up.

I quite often edit batch files with notepad (whilst running) and have not had a problem.

However, I am not sure you can just inject commands - it remembers the line number (I think) that it was on.

e.g. in your bat file, after starting, it will have finished processing.

Place a pause at the third line, then run but do not press a key after the pause. Edit the original bat file, place a command in the fourth line - and you will see that the fourth line will get processed.

William Hilsum

Posted 2011-06-13T10:21:14.147

Reputation: 111 572

You think this is a problem with vanilla win32 gvim? What do you mean by "inject commands ... in your bat file, after starting, it will of finished processing?" – Peter Nore – 2011-06-13T12:00:06.053

1I know this is OLD and this is not the English site, but I have to point out "it will of" is probably meant to be "it will've" or "it will have". – TecBrat – 2013-07-02T12:55:04.950

2

I would try changing the line

@start ConsolePortable.exe

To

@start "myProgram" ConsolePortable.exe

As from http://ss64.com/nt/start.html:

START "title" [/Dpath] [options] "command" [parameters]

Notes:

Always include a TITLE this can be a simple string like "My Script" or just a pair of empty quotes "" According to the Microsoft documentation, the title is optional, but you may have problems if it is omitted.

Document files may be invoked through their file association just by typing the name of the file as a command. e.g. START "" WORD.DOC would launch the application associated with the .DOC file extension

This one caught me off guard when I tried to script VLC some time ago and putting a title in there fixed a seemingly related issue for me.

Mokubai

Posted 2011-06-13T10:21:14.147

Reputation: 64 434

2You should always pass a title (empty or otherwise) to the start command unless you are absolutely, positively, 100% certain that the resulting command line will not contain any quotes. As the start help mentions: The first quoted parameter is assumed to be the title, no matter where it appears. – afrazier – 2011-06-13T20:47:04.230