mersenneforum.org  

Go Back   mersenneforum.org > Extra Stuff > Programming

Reply
 
Thread Tools
Old 2018-01-17, 18:26   #1
storm5510
Random Account
 
storm5510's Avatar
 
Aug 2009
U.S.A.

32318 Posts
Default 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
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.
storm5510 is offline   Reply With Quote
Old 2018-01-17, 19:13   #2
chalsall
If I May
 
chalsall's Avatar
 
"Chris Halsall"
Sep 2002
Barbados

932510 Posts
Default

Quote:
Originally Posted by storm5510 View Post
What happens if the 'move' command happens at the same instant that the original batch file is open for a write operation?
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.
chalsall is offline   Reply With Quote
Old 2018-01-18, 00:00   #3
potonono
 
potonono's Avatar
 
Jun 2005
USA, IL

3018 Posts
Default

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.
potonono is offline   Reply With Quote
Old 2018-01-18, 04:44   #4
storm5510
Random Account
 
storm5510's Avatar
 
Aug 2009
U.S.A.

3×563 Posts
Default

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
if not errorlevel 0 goto wait
call c:\temp\inventory.bat
:wait
timeout 1800
goto top
Based on the various sites I visited, the batch language has really evolved a lot since I learned it in college!
storm5510 is offline   Reply With Quote
Reply

Thread Tools


Similar Threads
Thread Thread Starter Forum Replies Last Post
Batch jobs using AWS Batch GP2 Cloud Computing 1 2016-12-07 11:31
14e batch of WU's pinhodecarlos NFS@Home 25 2015-07-25 17:50
Got first batch of TF to 75 Chuck GPU to 72 23 2015-05-10 20:34
Windows batch file for NFS - any help? f1pokerspeed Programming 9 2013-12-24 18:21
batch file em99010pepe No Prime Left Behind 7 2008-03-15 10:53

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

Sat Nov 28 22:23:47 UTC 2020 up 79 days, 19:34, 3 users, load averages: 1.95, 1.52, 1.35

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

This forum has received and complied with 0 (zero) government requests for information.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation.
A copy of the license is included in the FAQ.