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

05
2013-12
  • Peter Nore

    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.

  • Answers
  • William Hilsum

    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 of finished processing.

    If you were to 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.

  • Mokubai

    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.


  • Related Question

    windows - Batch script halts after Thunderbird is opened
  • Željko Filipin

    I want to be able to open Thunderbird from a batch script in Windows. I can do it just fine from command line:

    C:\>"C:\Program Files\Mozilla Thunderbird\thunderbird.exe"
    

    If I create a batch script that looks like this:

    "C:\Program Files\Mozilla Thunderbird\thunderbird.exe"
    

    and execute it from the command line:

    C:\>t.cmd
    C:\>"C:\Program Files\Mozilla Thunderbird\thunderbird.exe"
    

    Thunderbird opens, but the script halts (it does not exit).

    If I close Thunderbird by hand, the script exits.

    It has been a while since I have used batch scripts. Am I doing something wrong?

    Edit: script exits just fine if Thunderbird is already opened. Strange.


  • Related Answers
  • admintech

    Try

    start /d "C:\Program Files\Mozilla Thunderbird" thunderbird.exe

  • David Spillett

    As Systech suggests, the start command is what you are looking for to run the program and not wait for it to terminate before continuing.

    The reason you see different behaviour when Thunderbird is already running is that it is only allowing one copy to run. The second copy detects the first and gives it focus before going away (so effectively immediately returning control to your batch script). If you were calling an application that allows multiple instances of itself to run (something as simple as notepad would do if you want to test this to see for yourself) you would not see that difference in behaviour.

  • ChrisF

    The reason the script stalls is because it's waiting for Thunderbird to complete and return control to the batch file.

    You need to use the "/d" option as Systech suggests.

  • daddy-o

    FWIW this doesn't work properly in windows7. Even with start /b, Thunderbird terminates when the controlling shell window terminates.

    That said, once you've got T'bird running, you can open other subwindows properly (e.g., thunderbird -addressbook) from the command line.

  • daddy-o

    So, if you want to run T'bird from a windows command line or batch file, I've figured out how to do it. As you probably have noticed, using the start command with any combination of parameters doesn't do what you really want.

    The first step is to create a Windows Scripting Host command file. Put this in the same directory as your T'bird executable, can call it something like tbird.wsf:

    <job>
    <script language="VBScript">
    Set WshShell = CreateObject("WScript.Shell")
    cmds=WshShell.RUN("cmd /c thunderbird.exe", 0, False)
    </script>
    </job>
    

    once you have that file, then invoke it from command line or batch file like this:

    cscript tbird.wsf
    

    Of course, this will leave an extra process hanging around your system that won't die until you close T'bird down. But this does achieve what I think lots of people want.

    davodavo -- check me out on www.saleslogistix.com