MoveFile procedure

Share your advanced PureBasic knowledge/code with the community.
wallgod
User
User
Posts: 48
Joined: Wed Oct 06, 2010 2:03 pm

MoveFile procedure

Post by wallgod »

Code: Select all

Procedure.i MoveFile(file1.s, file2.s)
    If Not CopyFile(file1, file2)
        ProcedureReturn 0
    EndIf
    ProcedureReturn DeleteFile(file1)
EndProcedure

Debug MoveFile("c:\users\name\desktop\1.txt", "d:\backup\1.txt")
Procrastinators unite... tomorrow!
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: MoveFile procedure

Post by ts-soft »

You can use "RenameFile()", to move a file :roll:

Greetings
Thomas
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: MoveFile procedure

Post by PB »

The problem with using CopyFile() is that it'll take forever with large files.
For example, your procedure with my AVI file of "Lord of the Rings" would
take about 3 minutes.

Now, as ts-soft said, if you use RenameFile() and both the source and
target files are on the SAME drive, the move is basically instant. My
"Lord of the Rings" file above moves in one second, but using CopyFile()
still takes about 3 minutes, both to the same or different drive.

Oh, and CopyFile() requires that the target drive has free disk space
that is the same size (or more) of the filed to be copied. RenameFile()
doesn't for same drives.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
wallgod
User
User
Posts: 48
Joined: Wed Oct 06, 2010 2:03 pm

Re: MoveFile procedure

Post by wallgod »

Alright then! How about something like this (for Windows only)?

Code: Select all

Procedure MoveFile(file1.s, file2.s)
    If Left(file1, FindString(file1, "\", 3)) = Left(file2, FindString(file2, "\", 3))
        ProcedureReturn RenameFile(file1, file2)
    EndIf
    If Not CopyFile(file1, file2)
        ProcedureReturn 0
    EndIf
    ProcedureReturn DeleteFile(file1)
EndProcedure
...or is there a better way to check the drive of a file specification?
Procrastinators unite... tomorrow!
Post Reply