Page 1 of 1

TouchFile() for windows

Posted: Fri Sep 19, 2014 1:00 pm
by infratec
Hi,

I needed to 'touch' a file in windows for a C header file, since CopyFile() does not change the timestamp.

I thought OpenFile() and CloseFile() would be enough, but .... nothing changed.
Than I tried to append an empty string with WriteString(), but this trick didn't work either.

But as usual there was a way:

Code: Select all

Procedure.i TouchFile(Filename$)
  
  Protected File.i
  
  File = OpenFile(#PB_Any, Filename$, #PB_File_Append)
  If File
    TruncateFile(File)
    CloseFile(File)
  EndIf
  
  ProcedureReturn File
  
EndProcedure
Maybe it saves someone a bit of time.

Bernd

Re: Touch() for windows

Posted: Fri Sep 19, 2014 1:26 pm
by IdeasVacuum
I'm really surprised that works! 8)

The post title is misleading btw. Without the word 'file', most people will likely assume the subject is 'touch screen'.

Re: Touch() for windows

Posted: Fri Sep 19, 2014 1:28 pm
by infratec
I was surprised too :mrgreen:

But if I have a target, I'll try all possibilities.

Hm, touch is the standard unix command since decades.
But I changed it.

Bernd

Re: Touch() for windows

Posted: Fri Sep 19, 2014 1:42 pm
by PB
What's wrong with the SetFileDate() command?

http://www.purebasic.com/documentation/ ... edate.html

Re: TouchFile() for windows

Posted: Fri Sep 19, 2014 1:48 pm
by infratec
:oops: :oops: :oops:

I looked only in 'PureBasic - File' section.

But now you know an alternative :mrgreen:

Re: TouchFile() for windows

Posted: Fri Sep 19, 2014 1:52 pm
by PB
:)

Re: TouchFile() for windows

Posted: Fri Sep 19, 2014 1:54 pm
by infratec
And the exe file is 1.4k larger with SetFileDate().
Maybe because I didn't use any time procedure like Date() or ElapsedMilliseconds() before.

Re: TouchFile() for windows

Posted: Fri Sep 19, 2014 2:05 pm
by PB
> the exe file is 1.4k larger with SetFileDate()

Larger? :shock: That's impossible. Or did you mean smaller?

Re: TouchFile() for windows

Posted: Fri Sep 19, 2014 2:15 pm
by infratec
In my case it is smaller.
As I told you, I use alreday other file commands, and so this library part is already included.
But I never used Date().

But if I build a file with only this procedures inside, than the SetFileDate() version is smaller :cry:

But I think it's academic, when peoble debate about 4GB or more. :mrgreen:

Re: TouchFile() for windows

Posted: Fri Sep 19, 2014 2:57 pm
by skywalk
This is narrow scope since I always use SetFileDate()/touch with a specific datetime in mind. Like when I unpack files in a new location. I want the file datetime to retain the value when it was packed.

Re: TouchFile() for windows

Posted: Fri Sep 19, 2014 3:13 pm
by PB
Oh, now I get it: you meant the exe that does the touching is smaller.
At first i thought you meant the touched file somehow got smaller. :)