Hi.
Could you please tell me the syntax of the movefile and movefileex command?
Thank you
Hi Nicola,
I've never used them.
MoveFile takes 2 strings
MoveFileEx takes 2 strings and an int
They return 0 if they don't succeed.
uses corewin
https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-movefilea
https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-movefileexa
Thank you Charles.
... What do you do if you need to move files?
I have just searched all the examples and could not find any instances where we have used MoveFile, CopyFile, or DeleteFile. So you will be the first :)
When self-compiling and installing oxygen updates, I rely on batch files as it is unsafe to have a fully automated system.
I tried to run this command, but the result is 0, i.e. no move is made.
print movefile("c:\temp\1\*.jpg","c:\temp\1\newdir\")
So I tried it this way and it worked very well.
string tomove= "move /Y c:\temp\1\*.jpg " + "c:\temp\1\newdir\"
Dos(tomove,1)
MoveFile only works for a specific file/directory.
Quick demo:
uses corewin
putfile "t.tmp","helo"
movefile "t.tmp","s.tmp" 'could also use copyfile
print getfile "s.tmp"
deletefile "s.tmp"
deletefile "t.tmp"
That is, if I want to move a file from one directory to another, I can't do it?
So let's try to do it with copyfiles. A sub that copies the file and then copied deletes the source.
According to the docs, you can move a file anywhere within the same volume, as long as the target file does not exist already.
From the way you write I understand that you would not accept the movement of files based on a filter... in fact, in the example I posted, it didn't do it for me.
uses corewin
putfile "c:\temp\1\t.tmp","helo"
movefile "c:\temp\1\t.tmp","c:\temp\1\n\s.tmp" 'could also use copyfile
print getfile "c:\temp\1\n\s.tmp"
deletefile "c:\temp\1\n\s.tmp"
deletefile "c:\temp\1\t.tmp"
Works
uses corewin
putfile "c:\temp\1\t.tmp","helo"
putfile "c:\temp\1\w.tmp","whelo"
movefile "c:\temp\1\*.tmp","c:\temp\1\n\*.tmp" 'not works
movefile "c:\temp\1\*.tmp","c:\temp\1\n\*.*" 'not works
movefile "c:\temp\1\*.tmp","c:\temp\1\n\" 'not works
print getfile "c:\temp\1\n\t.tmp"
'deletefile "c:\temp\1\n\s.tmp"
deletefile "c:\temp\1\t.tmp"
deletefile "c:\temp\1\*.tmp" 'not works
not works
MoveFile doesn't support wildcards but you could move a whole subdirectory, which might be useful.
ok thanks, I've also seen it in Microsoft's directives. :-)
Hi charles,
I took the liberty of borrowing one of your functions...
It works the way I did. I've inserted a couple of lines. (to be fixed well anyway)
Tell me what you think.
Cheers
uses sysutil
function _CopyFile(string source, string dest, optional dirq) as string
======================================================================
'
macro appendList(s,w,i, ls,lw) '(string *s,w, int *i)
int ls=len s
int lw=len w
if i+lw>ls
s+=nuls 1024 '+lw
end if
mid s,i+1,w
i+=lw
end macro
'
WIN32_FIND_DATA f
int a,e,fi
sys h
string flist
string cr=chr(13,10)
string dirsource=extractdirname(source)
'
'h=FindFirstFile filter, @f
h=FindFirstFile source, @f
int count=0
if h then
do
a=0
if f.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY
if dirq then a=1
byte b at @f.cFileName
if b=46 then a=0 '.' ' ..'
else 'NOT A DIRECTORY
if not dirq then a=1
end if
if a then
count++
appendlist flist, f.cFileName+cr, fi
copyfile(dirsource+"\"+f.cFileName, dest+f.cFileName)
end if
e=FindNextFile h, @f
if e=0 then
e=GetLastError() 'should be 18 when no more files
exit do
end if
'print dirsource+"\"+f.cFileName
copyfile(dirsource+"\"+f.cFileName, dest+f.cFileName)
end do
FindClose h
end if
return left (flist,fi)
'
end function
[font=Verdana, Arial, Helvetica, sans-serif]putfile "c:\temp\1\t.tmp","helo"[/font]
putfile "c:\temp\1\w.tmp","whelo"
print _copyFile "c:\temp\1\*.tmp","c:\temp\1\n\" '*****WORKS
print getfile "c:\temp\1\n\t.tmp"
Hi Nicola,
Not sure what it is intended to do, but you might need to trap errors. I don't have a tmp directory, so the file is not created, and the content displayed is therefore blank.
Hi Charles,
I'm trying to do this feature, but it doesn't copy all the files. I've tried doing a for... next and copies only 10 of them. I tried col do.. end do and even here it doesn't copy all the files it finds...
I think there's something that dirties the variables...
Can you take a look at it?
uses sysutil
'uses parseutil
function xxCopyFile(string source, string dest, int muove=0, optional dirq) as string
==========================================================================
string cr=chr(13,10)
int i, max, h
redim string d[100]
string lista, w, sorgente, destinazione, dirsource
int c 'conteggio dei file
dirsource=extractdirname(source)
lista = getfilelist source,c
print lista cr c
max=c
'd è il vettore dove vanno i dati splittati, c è il numero di dati trovati
split(lista,d,max,h,w)
string listone=d[1]
for i=2 to c
listone = listone+ ";" + d[i]
next
print listone+"#"
print "c=" c "- " max
int ix=0
do
ix++ : if ix>max then exit do
sorgente=dirsource + "\" + d[ix]
'print dirsource + "\" + d[ix]
destinazione=dest + d[ix]
copyfile(sorgente,destinazione)
'copyfile(dirsource + "\" + d[ix],dest + d[ix])
if muove<>0 then
deletefile(sorgente)
end if
end do
return ix
'
end function
'-------------------------------------------
string ff
string sorg=curdir+"\*.tmp"
string desti=curdir+"\n\"
createdirectory(desti)
'print sorg cr desti
int g
for g=1 to 30
ff=g+".tmp"
putfile ff,"helo"
next
print "copie fatte= " XXcopyFile(sorg,desti,0)
Maybe I'm not that good, but it drives me crazy...
I try not to get involved with Microsoft issues. I am the least qualified coder to do so, but I would use DOS commands (xcopy etc) to perform file processing. Otherwise you are faced with the task of emulating DOS.
PS:
Instantiating strings inside a loop is best avoided.
The problem is not emulating DOS, the problem is because the variables behave outlily. If I run a cycle from 1 to 30, why does it get stuck at a certain point?
Why if I use copyfile(sorgente,destinazione) program crashes?
Why if I use copyfile(dirsource + "\" + d[ix],dest + d[ix]) copy only 10 of 30 files?
Hi Nicola,
I found that CopyFile takes 3 parameters. The last param would normally be set to 0.
https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-copyfile
BOOL CopyFile(
[in] LPCTSTR lpExistingFileName,
[in] LPCTSTR lpNewFileName,
[in] BOOL bFailIfExists
);
Also: Redim string d[100] should be placed in global space outside the xxcopy function
Everything else looks ok. But it took me a few hours to isolate these bugs, so my mental health also needs restoration :)
Here's how I handled a quirk in the backup process years ago...
how to keep file creation date when doing backups (https://forum.powerbasic.com/forum/user-to-user-discussions/source-code/40426-preserve-sourcefile-creationtime-on-backup)
I hope this is helpful!
-John
Well done Charles,
how did you figure out that Redim string d[100] had to be put out of function even though I only use array d in the function?
Anyway, I was sure you had found the catch.
Now it seems to be working well.
uses sysutil
redim string d[100]
function xxCopyFile(string source, string dest, int muove=0) as int
===================================================================
string cr=chr(13,10)
int i, max, h
string lista, w, sorgente, destinazione, dirsource
int c 'files count
dirsource=extractdirname(source)
lista = getfilelist source,c
'print lista cr c
max=c
'd è il vettore dove vanno i dati splittati, c è il numero di dati trovati
split(lista,d,max,h,w)
'string listone=d[1]
'for i=2 to c
' listone = listone+ ";" + d[i]
'next
'print listone+"#"
'print "c=" c "- " max
int ix=0
do
ix++ : if ix>max then exit do
sorgente=dirsource + "\" + d[ix]
'print dirsource + "\" + d[ix]
destinazione=dest + d[ix]
copyfile sorgente,destinazione,0
if muove<>0 then
deletefile(sorgente)
end if
end do
return ix-1
'
end function
'-------------------------------------------
string ff
string sorg=curdir+"\*.tmp"
string desti=curdir+"\n\"
createdirectory(desti)
'print sorg cr desti
int g
for g=1 to 30
ff=g+".tmp"
putfile ff,"helo"
next
print XXcopyFile(sorg,desti,1) 'if the third element is non-zero make the move otherwise only copy
p.s. Charles
... A good beer can be useful :-)
Thanks for the link John.
I found that with the code we are testing here, the date modified is conserved but the date created is the date of copying.
Hi Nicola,,
I need to investigate the redim problem further.. It should create a static array when defined within a procedure. But in general, the array should have a unique name in the program,, and if it is globally defined it can be redimmed from any procedure.
Great Charles, thank you.
Thanks for the link John.