mersenneforum.org

mersenneforum.org (https://www.mersenneforum.org/index.php)
-   Programming (https://www.mersenneforum.org/forumdisplay.php?f=29)
-   -   Batch File Issue, Perhaps (https://www.mersenneforum.org/showthread.php?t=22940)

storm5510 2018-01-17 18:26

Batch File Issue, Perhaps
 
I have written a batch file to assist a local mom-and-pop shop automate a data gathering process. It goes as below:

[CODE]@echo off
:Top
cls
if not exist c:\data\inventory.bat goto wait
move c:\data\inventory.bat c:\temp\inventory.bat
call c:\temp\inventory.bat
:wait
timeout 1800
goto top[/CODE]Inside the batch file is archiving command(s) to add to an existing archive which is stored offline at the end of business each day.

He's my question: What happens if the 'move' command happens at the same instant that the original batch file is open for a write operation?

I suspect this would raise some sort of error. I know a tiny bit about 'errorlevel' statements. This one, I am not sure how to handle.

chalsall 2018-01-17 19:13

[QUOTE=storm5510;477767]What happens if the 'move' command happens at the same instant that the original batch file is open for a write operation?[/QUOTE]

Under Unix, the "writer(s)" will continue to write to the file under the new name, so long as it is on the same partition/filesystem. I believe the behaviour is the same under Winblows.

To avoid race conditions, it's a good idea to wait for a period of time after moving a file which might have writers before doing any work on the file (such as compressing it). Or, even better, make system calls to determine how many write locks are on the file.

potonono 2018-01-18 00:00

If the file is locked, the move command will give you an error indicating, "The process cannot access the file because it is being used by another process" and indicate that zero files were moved. You would need to test if the indicated 'write operation' locks the file or not. For example Excel locks a spreadsheet file when opening, but Notepad doesn't lock a text file when opening.

storm5510 2018-01-18 04:44

I made a modification. Not sure if it will work or not.

[CODE]@echo off
:Top
cls
if not exist c:\data\inventory.bat goto wait
move c:\data\inventory.bat c:\temp\inventory.bat
[B]if not errorlevel 0 goto wait[/B]
call c:\temp\inventory.bat
:wait
timeout 1800
goto top[/CODE]Based on the various sites I visited, the batch language has really evolved a lot since I learned it in college!


All times are UTC. The time now is 22:20.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.