TouchFile() for windows

Share your advanced PureBasic knowledge/code with the community.
infratec
Always Here
Always Here
Posts: 7622
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

TouchFile() for windows

Post 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
Last edited by infratec on Fri Sep 19, 2014 1:30 pm, edited 1 time in total.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Touch() for windows

Post 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'.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
infratec
Always Here
Always Here
Posts: 7622
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Touch() for windows

Post 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
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Touch() for windows

Post by PB »

What's wrong with the SetFileDate() command?

http://www.purebasic.com/documentation/ ... edate.html
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
infratec
Always Here
Always Here
Posts: 7622
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: TouchFile() for windows

Post by infratec »

:oops: :oops: :oops:

I looked only in 'PureBasic - File' section.

But now you know an alternative :mrgreen:
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: TouchFile() for windows

Post by PB »

:)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
infratec
Always Here
Always Here
Posts: 7622
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: TouchFile() for windows

Post 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.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: TouchFile() for windows

Post by PB »

> the exe file is 1.4k larger with SetFileDate()

Larger? :shock: That's impossible. Or did you mean smaller?
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
infratec
Always Here
Always Here
Posts: 7622
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: TouchFile() for windows

Post 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:
User avatar
skywalk
Addict
Addict
Posts: 4219
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: TouchFile() for windows

Post 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.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: TouchFile() for windows

Post 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. :)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Post Reply