1.

Solve : Open A specified File if a now > Certain File's Modified stamp?

Answer»

Hi

I am rather shamelessly requesting a batch file to do the following -

I have an executable file 'XYZ.exe' that can be accessed by multiple users over a LAN system. It seems that when this file is opened, it creates a clone of itself to a file with another name with .TMP extension and then the Rxyz.tmp file is destroyed and the executable opens. All this takes a fraction of a second. If, however, another user in the LAN attempts to open 'XYZ.exe' before this conversion re-conversion process is complete, the executable 'XYZ.exe' gets left with the .tmp extension and is essentially corrupted showing 0B.

To circumvent this (workaround), the user could be asked to click on the batch file I am requesting instead of on the executable 'XYZ.exe'.

The batch file will reside in the same folder as 'XYZ.exe'.
When opened, it should ..
1. Check for text file 'CheckTime.txt' in the same folder
2. If not existing, it should create it and immediately open the file 'XYZ.exe'
3. If 'CheckTime.txt' is found to be already existing, then the date+time modified should be compared with now and if, say, 6 seconds have not elapsed, the batch should wait until 6 seconds after 'CheckTime.txt' was last modified and then open 'XYZ.exe'
4. Batch file closes, LEAVING only the opened 'XYZ.exe'

The above may be RELATIVELY easy. The twist is ---what if a third user tries to open 'XYZ.exe' via this batch at around the same time?

Any help would be appreciated.

Regards
Look into the commands
attrib
set /p _=<_
goto :_
start
The file XYZ.exe - is that a compiled batch file?Quote from: foxidrive on September 30, 2013, 05:54:05 PM

The file XYZ.exe - is that a compiled batch file?

No, it is a third party software.If the users launch this then it could be all you need.

Code: [Select]@echo off
:loop
If exist Rxyz.tmp echo waiting & timeout 1 & :goto loop
start "" xyz.exe
Another possible solution is to create a unique random folder, copy xyz.exe into it and launch it. Delete the file and folder when it is done.
Everyone would get a separate copy.Quote from: foxidrive on October 01, 2013, 12:00:45 AM
If the users launch this then it could be all you need.

Code: [Select]@echo off
:loop
If exist Rxyz.tmp echo waiting & timeout 1 & :goto loop
start "" xyz.exe

Thank you, makes sense but the .tmp file name is not always consistent. Therefore, either *.tmp or R*.tmp or ??.tmp if I was sure about the temp file name's length or the original request for a batch file.

Also, please confirm that if say, there are 5 PCS on the LAN and more than one user clicks on the short-cut to the new batch file at practically the same time, the batch file will do the needful?Quote from: jds on October 01, 2013, 12:09:20 AM
Thank you, makes sense but the .tmp file name is not always consistent. Therefore, either *.tmp or R*.tmp or ??.tmp if I was sure about the temp file name's length or the original request for a batch file.

You could use *.tmp if there are no other tmp files in the folder.

Quote from:
Also, please confirm that if say, there are 5 PCS on the LAN and more than one user clicks on the short-cut to the new batch file at practically the same time, the batch file will do the needful?

There is a one second delay. In that time frame there could be a clash with multiple users.

The other suggestion I made would solve that for any number of users, if the file can be executed in another folder.Quote from: foxidrive on October 01, 2013, 08:32:33 AM

The other suggestion I made would solve that for any number of users, if the file can be executed in another folder.

While trying to find a solution by code such as requested here, the workaround I use is to have the executable itself saved with different names -
'XYZ.exe', "XYZ1.exe', 'XYZ2.exe' etc. and have different computer's short-cuts point to unique software executables. But this is a workaround. Would dearly like to find a code solution to this problem.In that case it's a little simpler. This is meant to

A) generate a random number,
B) copy the XYZ executable to that new name and launch it,
C) wait until the program is closed and then delete the temporary copy.

A drawback is that an extra cmd window will be on the desktop while the XYZ program is active.

Code: [Select]@echo off
:loop
set "num=xyz-temp-%random%%random%%random%.exe"
If exist "%num%" goto :loop
copy "xyz.exe" "%num%" >nul
echo This window will automatically close when XYZ is ended - don't close it manually.
start "" /w "%num%"
del "%num%"

One workaround for the cmd window is to launch the files normally - and in a scheduled task remove all the temporary executables once a day.

Code: [Select]@echo off
:loop
set "num=xyz-temp-%random%%random%%random%.exe"
If exist "%num%" goto :loop
copy "xyz.exe" "%num%" >nul
start "" "%num%"

This batch file will remove all the temporary executables.

Code: [Select]@echo off
del "d:\folder\xyz-temp-*"
Quote from: foxidrive on October 01, 2013, 09:26:37 AM
In that case it's a little simpler. This is meant to

A) generate a random number,
B) copy the XYZ executable to that new name and launch it,
C) wait until the program is closed and then delete the temporary copy.

A drawback is that an extra cmd window will be on the desktop while the XYZ program is active.


One workaround for the cmd window is to launch the files normally - and in a scheduled task remove all the temporary executables once a day.


This batch file will remove all the temporary executables.

Code: [Select]@echo off
del "d:\folder\xyz-temp-*"


Not sure if Solution #1 will work when USED by multiple users at the same time (beside being ugly).
With Solution #2, can it be modified to remove previously created temporary exes whenever the batch file is run from any LAN PC? Sort of checking for temp files and if they are not open to delete them.

The question still not answered adequately is, "Will the batch file code work when executed by more than one user at precisely the same time?" Say PC1 executes 'openfile.bat' and at the same time user on PC2 double-clicks the short-cut to 'openfile.bat' - it nothing happens (as in nothing adverse) when PC2 user double-clicks, it is OK with me. He will simply try again after a few seconds thinking that he has not double-clicked properly.

Solution #3 ??
Is there any code that can execute this way -
When the 'Openfile.bat' file is executed, a run on time (now+2 seconds = x) is executed to open 'XYZ.exe' at time x and 'Openfile.bat' closes.

If 'Openfile.bat' is accessed again, it fetches x and executes a run on time command (now + 2 seconds or x+2 seconds, whichever is later) and 'Openfile.bat' closes. I imagine that 'Openfile.bat' will open, run and close in milliseconds, even over a LAN.

In this way there is a pre-defined settable minimum interval between the open attempts of 'XYZ.exe'


Discussion

No Comment Found