Computer Science Canada

[Tutorial] File Manipulation

Author:  gevorgkhc [ Sun May 16, 2004 11:03 pm ]
Post subject:  [Tutorial] File Manipulation

File Copying
Use the FileCopy command for this. The syntax is:
code:
FileCopy "source", "destination"

For example, if I wanted to copy yo.txt to blah.exe, I'll do:
code:
Private Sub Command1_Click()
On Error Resume Next  'Error Handling
FileCopy "C:\yofolder\yo.txt", "C:\blahfolder\blah.exe"  'Command
End Sub

Yes, you can copy other file formats to another!

File Renaming
If we wanted to rename a file, we would use the Name command. The syntax is:
code:
Name "oldname" As "newname"

For example, if I wanted to rename sup.txt to hello.html, I'll do:
code:
Private Sub Command1_Click()
On Error Resume Next  'Error Handling
Name "c:\supfolder\sup.txt" As "c:\hellofolder\hello.html"  'command
End Sub

You can always rename or copy any formats to another!

Deleting Files
If we wanted to delete a file, we would use the Kill command. The syntax is:
code:
Kill "file"

For example, if I wanted to delete alpha.exe, I'll do:
code:
Kill "C:\Blah\alphafolder\alpha.exe"

I don't have to tell you about the formats again.....

Moving Files
To move a file or files, you first use the copy command and then the kill command. I hope I don't have to give a basic syntax of the two line. See "Copying File" and "Deleting File"
Ok this is an edit. If you want to check that the file was succefully copy is complete to prevent data loss, use the checking file section I just add to this tutorial as an edit and since I'm in a REAL hurry I can just give a quick example.

Checking Files
code:
Private Sub Command1_Click()
On Error Resume Next
FileCopy "C:\oldyofolder\yo.exe", "c:\yofolder\yobolh.ran" 'Copy Command
Dim fexists As Variant 'Create fexists variable
fexists = Dir$("c:\yofolder\yobolh.ran") 'Gets the file name if it exists
If fexists = yobolh.ran Then 'checks if the filename is the file that you want and it exists
Kill "C:\oldyofolder\yo.exe" 'Delete the file
End Sub

You'll understand it.


I'll create a folder manipulation and further file manipulation in another tutorial at another time(part 2).

Author:  Tony [ Mon May 17, 2004 11:01 am ]
Post subject: 

When moving the file, it might be a good idea to check if the file was copied successfuly before deleting the original, to prevent accidental data lose.

Thx for the tutorial +25Bits

Author:  TheXploder [ Thu Feb 17, 2005 10:26 pm ]
Post subject: 

how would I create a Folder?

Author:  Tony [ Fri Feb 18, 2005 10:16 am ]
Post subject: 

you could use a shell command Very Happy
visualbasic:

Shell("mkdir c:\folder")

Author:  Brightguy [ Fri Feb 18, 2005 10:45 am ]
Post subject:  Re: [Tutorial] File Manipulation

But wouldn't you need a program named mkdir to run?
Heh, there is actually a mkdir statement:
code:
MkDir "C:\Folder"


: