Linux-like touch command in IDL [message #90183] |
Sat, 07 February 2015 11:33  |
PMan
Messages: 61 Registered: January 2011
|
Member |
|
|
I am looking to do a linux style 'touch' from IDL. There is not a similar command in IDL, but any ideas on a work around?
Thanks
|
|
|
|
Re: Linux-like touch command in IDL [message #90209 is a reply to message #90184] |
Tue, 10 February 2015 19:02   |
PMan
Messages: 61 Registered: January 2011
|
Member |
|
|
On Saturday, February 7, 2015 at 5:05:48 PM UTC-5, Jim P wrote:
> If you're on Linux, you can execute
> IDL> spawn,'touch ' + filepath
True, I know, but I operate on Windows and Linux. So, I thought there might be something more IDL-ic (as in 'python-ic').
Perhaps, idyllic is too lofty a goal?
|
|
|
Re: Linux-like touch command in IDL [message #90220 is a reply to message #90183] |
Thu, 12 February 2015 06:51   |
Matthew Argall
Messages: 286 Registered: October 2011
|
Senior Member |
|
|
> I am looking to do a linux style 'touch' from IDL.
Does this do the same thing?
pro touch, path
if file_test(path, /DIRECTORY, /WRITE) then begin
temp_file = filepath('touch_temp_file.txt', ROOT_DIR=path)
openw, lun, filename, /GET_LUN
free_lun, lun
file_delete, temp_file
endif else if file_test(path, /WRITE) then begin
openw, lun, filename, /GET_LUN
free_lun, lun
endif else begin
message, 'PATH must be a file or directory with write priveleges.'
endelse
end
|
|
|
Re: Linux-like touch command in IDL [message #90221 is a reply to message #90220] |
Thu, 12 February 2015 07:09   |
Matthew Argall
Messages: 286 Registered: October 2011
|
Senior Member |
|
|
> pro touch, path
> if file_test(path, /DIRECTORY, /WRITE) then begin
> temp_file = filepath('touch_temp_file.txt', ROOT_DIR=path)
> openw, lun, temp_file, /GET_LUN
> free_lun, lun
> file_delete, temp_file
> endif else if file_test(path, /WRITE) then begin
> openw, lun, path, /GET_LUN
> free_lun, lun
> endif else begin
> message, 'PATH must be a file or directory with write privileges.'
> endelse
> end
Fixed a few typos.
This will update time time stamps of a file (and create it, if need be), or a directory (so long as the directory already exists). Write privileges are required.
|
|
|
|
Re: Linux-like touch command in IDL [message #90223 is a reply to message #90222] |
Thu, 12 February 2015 08:50   |
Lajos Foldy
Messages: 176 Registered: December 2011
|
Senior Member |
|
|
On Thursday, February 12, 2015 at 5:31:49 PM UTC+1, Heinz Stege wrote:
> On Thu, 12 Feb 2015 07:09:50 -0800 (PST), Matthew Argall wrote:
>
>>> openw, lun, path, /GET_LUN
>>> free_lun, lun
>
> Oh my goodness, don't do this! On Windows this deletes the contents of
> an existing file.
>
> Cheers, Heinz
Does /APPEND solve this?
regards,
Lajos
|
|
|
|
Re: Linux-like touch command in IDL [message #90232 is a reply to message #90223] |
Thu, 12 February 2015 13:14   |
Matthew Argall
Messages: 286 Registered: October 2011
|
Senior Member |
|
|
> Does /APPEND solve this?
No, adding /APPEND does not change [ACM]TIME (see below). The following also do not change [ACM]TIME.
File_Move, path, path, /ALLOW_SAME
File_Copy, path, path, /ALLOW_SAME
OpenR, lun, filename, /GET_LUN
free_lun, lun
I thought at least the last operation would change ATIME, but it doesn't. On the otherhand, the following changes ATIME
nLines = File_Lines(path)
So, I thought I maybe the file pointer needed to be moved, but this did not change ATIME either
openr, lun, path, /GET_LUN
point_lun, lun, 1
free_lun, lun
Finally, I tried reading on byte of data, which did change ATIME.
var = 0B
openr, lun, path, /GET_LUN
readu, lun, var
Below is an updated version of touch.pro. Since the same technique does not work with directories, I am left with creating a temporary file and deleting it. This changes all three of [ACM]TIME.
---------------------------------------------
pro touch, path
on_error, 2
;Directory -- Updates access, status change, and modified times.
if file_test(path, /DIRECTORY, /WRITE) then begin
temp_file = filepath('touch_temp_file.txt', ROOT_DIR=path)
openw, lun, temp_file, /GET_LUN
free_lun, lun
file_delete, temp_file
;File -- updates access time
endif else if file_test(path, /READ) then begin
var = 0B
openr, lun, path, /GET_LUN
readu, lun, var
;Other
endif else begin
message, 'PATH must be a file with read privileges or a directory with write privileges.'
endelse
end
|
|
|
Re: Linux-like touch command in IDL [message #90233 is a reply to message #90232] |
Thu, 12 February 2015 13:17   |
Matthew Argall
Messages: 286 Registered: October 2011
|
Senior Member |
|
|
> pro touch, path
> on_error, 2
>
> ;Directory -- Updates access, status change, and modified times.
> if file_test(path, /DIRECTORY, /WRITE) then begin
> temp_file = filepath('touch_temp_file.txt', ROOT_DIR=path)
> openw, lun, temp_file, /GET_LUN
> free_lun, lun
> file_delete, temp_file
>
> ;File -- updates access time
> endif else if file_test(path, /READ) then begin
> var = 0B
> openr, lun, path, /GET_LUN
> readu, lun, var
free_lun, lun
>
> ;Other
> endif else begin
> message, 'PATH must be a file with read privileges or a directory with write privileges.'
> endelse
> end
And, of course, I forget to close the file...
|
|
|
Re: Linux-like touch command in IDL [message #90234 is a reply to message #90233] |
Thu, 12 February 2015 13:49  |
Matthew Argall
Messages: 286 Registered: October 2011
|
Senior Member |
|
|
Ok. Last reply. I found the Truncate_Lun procedure, so thought I would add another feature. For files, you can update ATIME only (by reading) or all three [ACM]TIME (by writing, then deleting). For directories, only the latter option is available.
----------------------------------
pro touch, path, $
ACCESS=access
on_error, 2
;Information about the path provided.
info = file_info(path)
if info.exists eq 0 then message, 'PATH does not exist: "' + path + '".'
;Defaults
access = keyword_set(access)
modify = ~access
;Directory
if info.directory then begin
;Modified Time (also updates Access and Status Change times)
if modify then begin
if info.write eq 0 then message, 'PATH must have write privileges.'
;Create a temporary file then delete it.
temp_file = filepath('touch_temp_file.txt', ROOT_DIR=path)
openw, lun, temp_file, /GET_LUN
free_lun, lun
file_delete, temp_file
endif else begin
message, 'The ACCESS option is not available for directories.'
endelse
;File
endif else begin
;Access Time
if access then begin
if info.read eq 0 then message, 'PATH must have read privileges.'
;Read one byte of data
var = 0B
openr, lun, path, /GET_LUN
readu, lun, var
free_lun, lun
;Modified Time (also updates Access and Status Change times)
endif else begin
if info.write eq 0 then message, 'PATH must have write privileges.'
;Open to the end of the file, and get the file position
openw, lun, path, /GET_LUN, /APPEND
point_lun, -lun, end_of_file
;Write one byte of data
writeu, lun, 0B
;Truncate the contents just written & close the file.
point_lun, lun, end_of_file
truncate_lun, lun
free_lun, lun
;Other
endelse
endelse
end
|
|
|