5

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.

7
  • 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
    Commented Jun 13, 2011 at 10:31
  • I had not tried that; I had been double clicking on it. I will try that ...
    – Myer
    Commented Jun 13, 2011 at 10:34
  • 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
    Commented Jun 13, 2011 at 10:35
  • 1
    Where 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
    Commented Jun 13, 2011 at 11:53
  • @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?
    – Myer
    Commented Jun 13, 2011 at 12:04

2 Answers 2

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.

2
  • 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?"
    – Myer
    Commented Jun 13, 2011 at 12:00
  • 1
    I 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
    Commented Jul 2, 2013 at 12:55
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.

1
  • 2
    You 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
    Commented Jun 13, 2011 at 20:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .