Alyce
Aug 28, 2006
- "Reformatted, changed "as long" to "as boolean when required, corrected some misspellings"
Copying, moving, delecting and renaming files This article contains four sections. a section on copying files, a section on moving files, a section on deleting
- Table of Contents
Copying a file
Below the method by which a file can be copied using API calls is shown:calldll #kernel132, "CopyFileA",_The currentfilename$ is the string that contains the name of the file you want to make a copy of. The copyfilename$ is the file name for the copy. The bFailIfExists tells the computer what to do if the file name for the copy is already used. If set to 0 the copy will overwrite the current file with the same name. If set to 1 the copy does not overwrite the file. The code
currentfilename$ as ptr,_ 'the string containing the file to be copied
copyfilename$ as ptr,_ 'the string containing the file name of the copied file
bFailIfExists aslong,_boolean,_ '1or0
re aslong 'notboolean'not 0 if copy was successful
let currentfilename$= "c:\text.txt"The code
let copyfilename$= "c\folder\text.txt"
let bFailIfExists= 0
calldll #kernel132, "CopyFileA",_
currentfilename$ as ptr,_
copyfilename$ as ptr,_
bFailIfExists aslong,_boolean,_ '1or0
re aslongboolean
if re=0 then print, "copyfailed"
let currentfilename$= "c:\text.txt"
let copyfilename$= "c\copyoftext.txt"
let bFailIfExists= 0
calldll #kernel132, "CopyFileA",_
currentfilename$ as ptr,_
copyfilename$ as ptr,_
bFailIfExists aslong,_boolean,_
re aslongboolean
if re=0 then print, "copyfailed"
Remember to include the
Moving a file
There are two ways to move aBelow the method by which a file can be moved using API calls is shown:
calldll #kernel132, "MoveFileA",_
currentfilename$ as ptr,_ 'the string containing the file to be copied
movefilename$ as ptr,_ 'the string containing the file name of the file in its new location
re aslongboolean 'not 0 if move was successful
[[code format="vbnet"]]
The currentfilename$ is the string that contains the name of the file you want to move. The movefilename$ is the file name for new location of the file.re willIf the function succeeds, the return value is nonzero. If the function fails, the return value is zero.Points to a null-terminated string that specifies the new name of a file or directory. The new name must not already exist. A new file may beeither 0on a different file system ormore. when re is 0 it means that moving the file has failed.drive. The codebellowbelow shows how to move a file and keep the same file name.
[[code format="vbnet"]]
let currentfilename$= "c:\text.txt"
let movefilename$= "c\folder\text.txt"
calldll #kernel132, "MoveFileA",_
currentfilename$ as ptr,_
movefilename$ as ptr,_
re aslongboolean
if re=0 the print, "move failed"
The code
let currentfilename$= "c:\text.txt"
let movefilename$= "c\folder\textmoved.txt"
calldll #kernel132, "MoveFileA",_
currentfilename$ as ptr,_
movefilename$ as ptr,_
re aslongboolean
if re=0 the print, "move failed"
name filename$ as movefilename$
A working example is shown
let currentfilename$= "c:\text.txt"
let movefilename$= "c\folder\text.txt"
name filename$ as movefilename$
Deleting a file
Files can be deleted using the kill command. The kill comand does not place thefiledialog "select file a file to delete"
if filename$="" then wait
kill filename$
Use the code above wisely and don't just delete any odd file because it is hard to get then back.
Renaming a file
You can use the name command to rename a file. This command can also be used to move a file.filedialog "select file a file to rename"Remember to include file extensions such as .txt on the end of the new file name.
if filename$="" then wait
prompt "enter a new file name"; newfilename$
name filename$ as newfilename$"file name"Filename has been changedfro";filename$;"to";newfilename$from";filename$;"to";newfilename$
Article by Alex Barfoot
alexbarfoot at gmail.com
-