How do I hold a file open (locked) from a Windows command-line shell script?

Sopalajo de Arrierez

I have been reading this thread about detecting a locked file, and now I would like to implement it in my scripts, so I will need a way to programatically detect it in Windows shell scripting.
The accepted solution gives a way to hold a file open/locked:

    (
      >&2 pause
    ) >> test.txt

but this solution needs a key pressing.
I would like to create a Windows shell script similar, like this:

[Create and locking of c:\Temp\ProgramRunning.lck]
--------------- Rest of the Script ---------------
[Blah blah blah]
---------- End of the Main Body the Script ----------
[Unlocking and deletion of c:\Temp\ProgramRunning.lck]

So, when the scripts finishes, the lock file gets deleted (and unlocked, of course). If this script gets stopped, Ctrl+C, window closed, hanging of the system or whatever, the lockfile is not deleted, but is unlocked.

How could I do that?

Harry Johnston

You can use call combined with output redirection to hold a file open until the call returns:

call :main 3>myopenfile.txt
goto :eof

:main
rem do whatever here
goto :eof

If needed, you can write to the file from within the call like this:

>>&3 echo hi

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I know which file is open in Windows from command line?

From Dev

How do I open a file with Chrome from the command line?

From Dev

How do I get XDG_CONFIG_HOME from a shell script / command line?

From Dev

How do I open a new command line window from an sh script and then pass in multiple commands

From Dev

How do I open a script file in RStudio using an R command?

From Dev

How do I launch `spark-shell` from the command line?

From Dev

Open file from the command line on Windows

From Dev

How do I open a WinSCP GUI session from command line?

From Dev

How do I open a WinSCP GUI session from command line?

From Dev

How do I open VM from command line with virsh

From Dev

How do I safely kill xscreensaver from a script/the command line?

From Dev

How do I run this script with sudo from the command-line?

From Dev

How do I export the history from the Windows command line to a text file?

From Dev

Delete locked file/folder from command line

From Dev

How do you create an empty file from the command line in Windows

From Dev

How do I add a line to the shell script?

From Dev

How do I open a file from inside a bash script?

From Dev

How do I search a PDF file from command line?

From Dev

How do I send a file with FileZilla from the command line?

From Dev

How do I run .c file from the command line

From Dev

How do I change file headers from the command line?

From Dev

How to open cygwin from python script, execute commands and take data from command line in file

From Dev

How to open cygwin from python script, execute commands and take data from command line in file

From Dev

How to pass a command from Shell script to command line

From Dev

How to suspend from command line such that screen is locked?

From Dev

How do I search a txt file for names from command line or interactive command line

From Dev

How do I wait for a file in the shell script?

From Dev

How I can I start a powershell command line from a batch file and enforce it to stay open?

From Dev

Why does this script work from the command line, but not when I put it in a windows batch file?

Related Related

  1. 1

    How do I know which file is open in Windows from command line?

  2. 2

    How do I open a file with Chrome from the command line?

  3. 3

    How do I get XDG_CONFIG_HOME from a shell script / command line?

  4. 4

    How do I open a new command line window from an sh script and then pass in multiple commands

  5. 5

    How do I open a script file in RStudio using an R command?

  6. 6

    How do I launch `spark-shell` from the command line?

  7. 7

    Open file from the command line on Windows

  8. 8

    How do I open a WinSCP GUI session from command line?

  9. 9

    How do I open a WinSCP GUI session from command line?

  10. 10

    How do I open VM from command line with virsh

  11. 11

    How do I safely kill xscreensaver from a script/the command line?

  12. 12

    How do I run this script with sudo from the command-line?

  13. 13

    How do I export the history from the Windows command line to a text file?

  14. 14

    Delete locked file/folder from command line

  15. 15

    How do you create an empty file from the command line in Windows

  16. 16

    How do I add a line to the shell script?

  17. 17

    How do I open a file from inside a bash script?

  18. 18

    How do I search a PDF file from command line?

  19. 19

    How do I send a file with FileZilla from the command line?

  20. 20

    How do I run .c file from the command line

  21. 21

    How do I change file headers from the command line?

  22. 22

    How to open cygwin from python script, execute commands and take data from command line in file

  23. 23

    How to open cygwin from python script, execute commands and take data from command line in file

  24. 24

    How to pass a command from Shell script to command line

  25. 25

    How to suspend from command line such that screen is locked?

  26. 26

    How do I search a txt file for names from command line or interactive command line

  27. 27

    How do I wait for a file in the shell script?

  28. 28

    How I can I start a powershell command line from a batch file and enforce it to stay open?

  29. 29

    Why does this script work from the command line, but not when I put it in a windows batch file?

HotTag

Archive