Library tutorials & articles
Files and Folders
Basic File Operations
The basic file operations in in VB are :
|
Name |
Function |
|
FileCopy |
Copies a file |
|
Kill |
Deletes a file |
|
Name |
Renames/moves a file |
To copy a file, you use the FileCopy function, which is very simple:
FileCopy SourceFile, DestinationFile
So,
FileCopy "C:\test.txt", "C:\testdir\test2.txt"
copies text.txt to C:\testdir and renames it test2.txt
To delete a file, you use the Kill (!) function.
Kill FilePath
So,
Kill "C:\test folder\test.txt"
deletes text.txt from C:\test folder . This does not send it to the recycle bin! If you want to do this, click here.
To rename/move file:
Name OldNameandPath As NewNameandPath
So,
Name "C:\test.txt" As "C:\test2.txt"
Renames test.txt as test2.txt, and
Name "C:\test.txt" As "C:\testdir\test2.txt"
moves text.txt to C:\testdir and renames is test2.txt
You can also use this function to rename folders.
To see if a file exists, you can use the Dir function. If it returns nothing, then the file does not exist:
If Dir(FileName) = "" Then
'// file does not exist
End If
However, this is not always reliable, especially on a network, and you should really use the WinApi method.
If you want to know how to do these operations using Win API, which allows you to send files to the recycle bin, and display the standard file copying dialog, click here.
Related articles
Related discussion
-
Run-time error '91'
by converter2009 (1 replies)
-
VB6 Runtime error 381 subsript out of range Error
by Uncle (2 replies)
-
passing and reading parameters from using Shell
by jigartoliya (0 replies)
-
Convert C++ code to VB6
by mawcot (4 replies)
-
listbox scrollbar
by Dennijr (10 replies)
Related podcasts
-
Christian Beauclair
14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...
I am a new beginner of Visual basic 6. Even though i read James's article, i don't know how to create a project and add his code as a new module.
Could you please show me how to create 1 project has 1 command button and 1 optionbox only? Also how to add James' s code to that project?Thanks for your time and understanding.
PS: tieungao35@yahoo.com
the article of James is not state of the art.
To import win32 functions is not the way how you should work with the .NET framework. With a little bit pation you can write a own managed code assemby for INI Files.
Tom
I am using deppfreeze in my internet cafe.This program protects hard drive from every changes and does not allow save games.When computer restarted all saves dissapear from hard drive.I know the games save directories and i need visual basic code that copies all files in directory to server.Code must overwrite and must include subdirectories.As msdos command does :
xcopy C:\Program Files\Call of Duty\Main\save*.* \server\saves\call of duty /e/y
(/e/y parameter is for overwrite and include subdirectory)
Can anyone help me.Thnx.
I've been able to follow your examples regarding making directories and renaming.
I would like to know two more things.
One: How do I check for a directory?
Reason: I am developing a database that will need to check for a directory's existance using the current month's name as its lable. If the a directory does not exist with the current month's name - I will have code automatically create a new directory using the current month's name each time a new calendar month begins and identified by the system.
Two: Can I use wild cards? Basically can I do the following in VBA -
FileCopy "C:\Foldername*.", "D:\DifferentFoldername*."
Thanks
Greg
Dim fp ' declare my variable that I want to use for the directory
fp = Dir("C:\work\venturaModify\06144") ' Set the variable equal to "a directory. This is where I am trying to make the user select this path, instead of me hardcoding it in.
...further into the "FileSearch" function I set the directory like this:
.LookIn = Dir(fp)
...I am still struggling with my forms..I went through the tutorial by James, but haven't found anything online about selecting a Path via a form I can create.
.-j
...My issue is two fold,
1. I need direction as to how to create a form in VB that will give users the ability to select a directoy. (I am new to vb, and not sure where to start. I am not asking anyone to do it for me, but if you could point me to a tutorial, example online that would help it'd be great.
2. I am using the ".LookIn = """ command to specify a directory right now. I am having problems specifying a variable (E.g. Dir = "C:/folder/subfolder", then .LookIn = Dir) doesn't seem to work) Any ideas as to why?
Hello there...
I have problems manipulating very big text files (I´m talking about files around 10 MB of plain text!!) and I have to find certain kind of words in the document (parse).
The files have a columns format (like vectors):
Var1 Var2 Var3 .... Var10
Value1 Value1 Value1 .... Value1
Value2 Value2 Value2 .... Value2
Value3 Value3 Value3 .... Value3
... ... ... ...
ValueN ValueN ValueN .... ValueN
///--------Comment line-----///
Var11 Var12 Var13 .... Var20
and so on...
I can calculate the size of the vectors because they have the same size, but I don't know how many "keywords" are because they vary file to file...
Currently I'm reading the files Line by Line... and it's like hell!!
I found in a book that I can open the file using the Random Method and the Get and Seek functions but I don't know exactly how to use them.
Thanks in advance!!
can anyone recommend a book or tutorial about hiding a file and folder..............
i want to have a tutorial of hiding folders or files if there is one...........................
' Returns an array of filenames that match FileSpec
' If no matching files are found, it returns False
Dim FileArray() As Variant
Dim FileCount As Integer
Dim FileName As String
On Error GoTo NoFilesFound
FileCount = 0
FileName = Dir(FileSpec)
If FileName = "" Then GoTo NoFilesFound
' Loop until no more matching files are found
Do While FileName <> ""
FileCount = FileCount + 1
ReDim Preserve FileArray(1 To FileCount)
FileArray(FileCount) = FileName
FileName = Dir()
Loop
GetFileList = FileArray
Exit Function
' Error handler
NoFilesFound:
GetFileList = False
End Function
I'd like to know how to get a list of files that are in a folder and put the file names into an array.
If you really want to hide some files and FOLDERS from your computer, other than do it yourself....you could download a Wonderful program from PCMagic's MAGIC FOLDER....
i am soorya need some API samples in VB to Hide Files and folders for security purpose.
most of the samples i get from pscode.com is useless...
can any one pls. let me some to
soorya@vsnl.com
thanks in advance
soorya
You're right, but it's not what I meant.
I wanted a way of finding what the Filenumber of a particular file was.
Doesn't matter now though, I found a way around it and have finished the program entirely.
lol, thanks anyway
I think nFileNum is just a variable for the file number, for another file you'd have to use something else. I'm probably wrong though.
First you need the filesize, I forget how to find it so we'll assume it's 20 bytes. ^_^; Then you'll need enough memory to hold it all. In this case to make things simple, we'll output the data to another file.
offset = 20
offset2 = 1
do while offset >0
get #1, offset, variable
put #2, offset2, variable
offset = offset - 1
offset2 = offset2 + 1
loop
Something along those lines should work, I'm a little rusty. I just stared a week or two ago.
If the program has multiple files open at the same time, how do I find the files nFileNum identifier so I can close a particular one?
Is there a piece of code that can return this?
I have the names and addresses of the files stored in an array, might be of a use.
Thanks buddeh, the rest of it really helped
After all the search I think I found something very complete, "friendly" speech, and lots of examples.
This is what I look in a book/tutorial or whatever.
Congrats to the author
LMDG
Hello
I would like to know, how to read a file in binary mode, the other
way around ( from the end to the beginning of the file).
Thanks in advance
Ignacio Cemeli
ignasicemeli@hotmail.com
This tutorial about Files and Directories are Especially useful.But for the sake of Security ,hiding a file or a Folder is a Must.How Could we do it From VB application.Any useful API's dedicated for it....
Anybody help Please..
This thread is for discussions of Files and Folders.