Movefile

Started by Nicola, October 23, 2023, 09:40:17 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Nicola

Hi.
Could you please tell me the syntax of the movefile and movefileex command?
Thank you

Charles Pegge

#1
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

Nicola

Thank you Charles.

... What do you do if you need to move files?

Charles Pegge

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.

Nicola

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)


Charles Pegge

#5
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"

Nicola

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.

Charles Pegge

According to the docs, you can move a file anywhere within the same volume, as long as the target file does not exist already.

Nicola

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

Charles Pegge

MoveFile doesn't support wildcards but you could move a whole subdirectory, which might be useful.

Nicola

ok thanks, I've also seen it in Microsoft's directives. :-)

Nicola

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"

Charles Pegge

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.

Nicola

#13
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...

Charles Pegge

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.