Does anyone have a compact or fairly portable way to rename a file from
within J?
I have the following, which is neither though it appears to work. However,
it uses "winexec" and other Windows-based APIs and jumps through the Windows
hoop of having to change to the disk and directory before running the rename
command. I used this in preference to Oleg's superior "task" library in
order to make the code as standalone as possible.
NB.* fileRename: call Windows API to rename file after minimal checking.
fileRename=: 3 : 0
'dirnm oldnm newnm'=. y
if. fexist newnm do.
0;'Cannot rename "',oldnm,'" to existing name "',newnm,'".'
else.
dirnm=. dirnm,PATHSEP_j_#~PATHSEP_j_~:{:dirnm
NB. Get disk specifier if present; otherwise, hope we are already on it.
dsknm=. (':' e. dirnm)#' & ',~dirnm{.~>:dirnm i. ':'
cmd=. dsknm,'cd "',dirnm,'" & ren "',oldnm,'" "',newnm,'"'
tmpfl=. crTmpFl 'bat'
cmd 1!:2 <tmpfl
wait 1 [ rc=. winexec tmpfl;1
rc [ 1!:55 <tmpfl
end.
NB.EG rc=. fileRename 'C:\Temp\';'log01.dat';'log02.dat'
)
NB.* fexist: 1 if named file exists, 0 otherwise.
fexist=: (1:@(1!:4) ::0:@((([:< 8 u:>) ::])&>)@(<^:(L.=0:)))
NB.* crTmpFl: create new, randomly named file (with optional suffix) in temp
dir.
crTmpFl=: 3 : 0
suff=. ,>(0=#y){y;'tmp'
td=. getTempDir ''
while. fexist flnm=. td,suff,~'.',~ALPHA{~?8$#ALPHA do. end.
flnm
NB.EG batfl=. crTmpFl 'bat'
)
NB. Where
require 'api'
NB.* winexec: Run external command
winexec=: 'WinExec' win32api NB. Run external command
getTempPath=: 'GetTempPath' win32api
NB.* getTempDir: get name of temporary directory assuming it's in
getTempDir=: 3 : 0
NB.* getTempDir: get name of temporary directory assuming it's in
NB. environment variable "TMP".
td=. >2{getTempPath 256;256$' '
td=. td{.~td i. 0{a.
if. 0 do.
td=. 2!:5 'TMP'
end.
td
)
--
Devon McCormick, CFA
^me^ at acm.
org is my
preferred e-mail
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm