Forum > Windows

Test if I could write to a certain file

<< < (3/4) > >>

dietmar:
I tried it with C:\Program Files\Test.dat.
A random file name C:\Program Files\32Afk4jf...$$$ was generated, but I could not write to this file. When choosing another folder like C:\ProgramData, it works...

--Dietmar

lucamar:

--- Quote from: dietmar on August 04, 2021, 02:51:03 pm ---A random file name C:\Program Files\32Afk4jf...$$$ was generated, but I could not write to this file.
--- End quote ---

So FileCreate() didn't fail, after all ... but if you try a FileWrite() it fails, is that it? Now, that's certainly strange but it also means that to check if you can write to a file you'll have to ... try to write to it.

Apparently nothing else works ... unless, now I think about it, maybe trying to change its timestamp or its attributes would also work. Just remeber to store the previous vales, to restore them in case it does work after all.

In the meantime I'll see if I can mount and fire on a Windows machine to make some tests of my own.

dietmar:
In my function, I added


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---    For I:=0 to 100 do      FileWrite(h,I,SizeOf(i)); 
just before the FileClose. It worked (in ProgramData) and didn't work in C:\Program Files - as it should. But nevertheless, both tests result in TRUE...

--Dietmar

lucamar:
You forgot to check the result of FileWrite. This should work:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function FileIsWriteable(fn: String) : Boolean;var  s: String;  h: THandle;begin  // try to write random file in same directory  repeat    s := ExtractFilePath(fn) + MakeRandomString(16) + '.$$$';  until not FileExists(s);  { Note that this only tests if you can create a file and write    to it in that directory, not the writeability of the file "fn"}  h := FileCreate(s,fmShareExclusive)  Result := h <> -1;  if Result then begin    Result := FileWrite(h, s[1], Lentgh(s));    FileClose(h);  end;end;

dietmar:
Hi,

your code was a bit erroneous. I have corrected that (see below).
Nevertheless: Thanks very much, because now it WORKS ;-))

Just for the records or for anyone who can need this:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function MakeRandomString(l: Byte): String;var i: Integer;    s: String;begin  s := '';  for i:=1 to l do    case random(3) of      0: s := s + Chr(65+Random(26));      1: s := s + Chr(97+Random(26));      2: s := s + Chr(48+Random(10));    end;  exit(s);end; function FileIsWriteable(fn: String) : Boolean;var  s: String;  h: THandle;begin  repeat    s := ExtractFilePath(fn) + MakeRandomString(16) + '.$$$';  until not FileExists(s);  h := FileCreate(s,fmShareExclusive);  Result := h<>-1;  if Result then begin    Result := FileWrite(h,s[1],Length(s))<>-1;    FileClose(h);    DeleteFile(PChar(s));  end;end; 
Greetz
--Dietmar

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version