Archive: Suggestion: IfFileInUse


Suggestion: IfFileInUse
Hi there!

Above you see the thing I'm really missing from NSIS. I know I could do it with a little *hard* coding (at least for me), but it would be much better if I could use this function like this:

IfFileInUse file_to_check_for jump_if_inuse [jump_otherwise]

Just like the IfFileExists function. Actually the two function should be combined. I imagine the code (the *real* code like this):

function IfFileInUse(F: PChar): Boolean;
begin
Result := False; //Initial result
if FileExists(F) then //Check if file exists, and if so, then check whether it's in use
Result := CreateFile(F, GENERIC_WRITE, 0, nil, OPEN_EXISTING, 0, 0) = INVALID_HANDLE_VALUE;
end;


Sorry, but since I only know Delphi Pascal my imagination can't do better either ;)

Best regards,
gLes

It's not hard coding at all:

ClearErrors
FileOpen $0 "path" "a"
FileClose $0
IfErrors locked notlocked

Doh! But wouldn't it be easier to do it my way?:)


That will increase the overhead. This is easy enough (if you want to make it even easier, create a macro).


How that look like?:)


!macro IfFileInUse FILE GOTONOTLOCKED GOTOLOCKED

ClearErrors

Push $R0

FileOpen $R0 "${FILE}" "a"
FileClose $R0

Pop $R0

IfErrors "${GOTONOTLOCKED}" "${GOTOLOCKED}"

!macroend

Usage:

!insertmacro IfFileInUse "c:\bla.txt" notlocked locked

Maybe I'll be cool if there is an instruction like the one:


IfFileExists

Simple, easy and handy :)

Thx. :)


Lobo Lunar, that will increase the installer overhead. I think a macro is simple enough.


I don't think that's a reasonable solution to the problem at all. What happens if the file is a 30 MB data file?


How does the size affect anything?

And with LogicLib this is even easier.

!macro _FileInUse _a _b _t _f
!insertmacro _LOGICLIB_TEMP
ClearErrors
FileOpen $_LOGICLIB_TEMP `${_b}` "a"
FileClose $_LOGICLIB_TEMP
IfErrors `${_t}` `${_f}`
!macroend

!define FileInUse `"" FileInUse`

${If} ${FileInUse} $INSTDIR\file
DetailPrint "in use"
${EndIf}

It all depends on the system and file but in my experience the larger the file the longer fileopen takes, I just think there could be a better way.


oh I didn't see the "a" my bad