|
-
Feb 17th, 2010, 07:33 PM
#1
Thread Starter
New Member
How to copy multiple text files but keeping origional name
i made an application that searches for an text file and copy it to
c:\test\. But when is find multiple files i cant copy it because it will overwrite the file in c:\test\ *.txt.
Is there a way i can copy a file and Keep the origional file name?
i have been looking hours for this...
Code:
Public Class Form1
Dim naar As String = "c:\test\"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For Each foundFile As String In My.Computer.FileSystem.GetFiles( _
My.Computer.FileSystem.SpecialDirectories.Desktop, _
FileIO.SearchOption.SearchAllSubDirectories, "*test*.txt", "*test2*.txt")
System.IO.File.Move(foundFile, naar)
ListBox1.Items.Add(foundFile)
Next
End Sub
End Class
-
Feb 17th, 2010, 07:35 PM
#2
Re: How to copy multiple text files but keeping origional name
You obviously can't keep the original file name if there are multiple files with the same name unless you copy them to different folders.
-
Feb 17th, 2010, 08:07 PM
#3
Thread Starter
New Member
Re: How to copy multiple text files but keeping origional name
mmm Then how do i save the first file as 1.txt, 2 as 2.txt ,.. ?
i tryd different things but i dident succeed.
-
Feb 17th, 2010, 08:12 PM
#4
Re: How to copy multiple text files but keeping origional name
The first thing to do is to think about the steps you would need to perform. Forget code for the moment. Think about what you physically need to do, e.g. check whether the file exists, etc. Once you have a list of steps that you know will work, only THEN should you consider writing any code. The code you write should implement your steps. If you don't know what the steps are then how can you possibly write code to perform them?
-
Feb 17th, 2010, 08:22 PM
#5
Thread Starter
New Member
Re: How to copy multiple text files but keeping origional name
i first need the base concept working than i will expand to checks etc.
-
Feb 17th, 2010, 08:29 PM
#6
Re: How to copy multiple text files but keeping origional name
 Originally Posted by spikee
i first need the base concept working than i will expand to checks etc.
Again, what are the steps you want to perform? You don't need to code to work out what to do. You need to work out what to do in order to write code. You don't have to have any programming experience to be able to come up with the steps. Have a go. We can help you fix it if it's not right but have a go.
-
Feb 17th, 2010, 08:51 PM
#7
Thread Starter
New Member
Re: How to copy multiple text files but keeping origional name
1. search text files = done
2. puts ther locations in a listbox = done
3.copy the text files (file name: 1.txt ,2.txt etc...)
the 3 part of naming it 1,2 ... is the hard part. i cant find a way to do it.
Thanks for the help!
-
Feb 17th, 2010, 09:08 PM
#8
Re: How to copy multiple text files but keeping origional name
 Originally Posted by spikee
1. search text files = done
2. puts ther locations in a listbox = done
3.copy the text files (file name: 1.txt ,2.txt etc...)
the 3 part of naming it 1,2 ... is the hard part. i cant find a way to do it.
Thanks for the help!
And that is the exact part I'm talking about. What are the steps you need to perform to get that part done? Like I said, you don't need any programming experience to come up with the steps. Imagine that someone asked you to do the same thing with a pen and paper. How would you do it? Are you saying that you would just sit there and say that you couldn't do it? Of course not. You could do it fairly easily I reckon. So, what are the steps you use? Those are the EXACT same stpes that your program must use. Once you have worked out what those steps are, writing the code to implement them is quite easy.
-
Feb 18th, 2010, 04:36 AM
#9
Thread Starter
New Member
Re: How to copy multiple text files but keeping origional name
Dim keer as integer = 0
1.search the file
2. get save path from listbox
3. save the file (source, "C:/test/" + keer + ".txt")
keer(+1)
next
is this the steps you mean?
-
Feb 18th, 2010, 04:42 AM
#10
Re: How to copy multiple text files but keeping origional name
Those are the sort of steps that I'm talking about but those steps aren't going to cut it as is. Think about what it is that you are trying to do.
Let's say that someone pointed you to several book shelves, each with several books, and they told you that they wanted you to move all those books to another shelf on the other side of the room. Now let's say that they told you that some of the books had the same name so, when you put them on the other shelf, you had to add a label to any duplicates so that they could be distinguished from the others. Could you do that? Of course you could. Anyone could. You don't have to be a programmer to do something like that. This is EXACTLY the same thing as you're trying to do with these files. So, if you would be able to successfully move and label the books, you MUST know what steps you would take to do so. What are they?
-
Feb 18th, 2010, 04:47 AM
#11
Fanatic Member
Re: How to copy multiple text files but keeping origional name
 Originally Posted by spikee
Dim keer as integer = 0
1.search the file
2. get save path from listbox
3. save the file (source, "C:/test/" + keer + ".txt")
keer(+1)
next
is this the steps you mean?
If you were doing this manually, you'd look in the folder and establish which is the current highest file name e.g. test4.txt
So, you need to stick all the file names into an array, loop through the array removing the left 4 characters (t e s t) which will leave you with an array that just contains [1], [2], [3] etc. up to the highest file name. You then need to sort the array to get the highest value at the top and that is your current highest file value. Add one to it and create your new file.
-
Feb 18th, 2010, 04:54 AM
#12
Thread Starter
New Member
Re: How to copy multiple text files but keeping origional name
there is no need for looking for the higherst value already in c:\test\ .
this code will only search one time.
-
Feb 18th, 2010, 05:25 AM
#13
Fanatic Member
Re: How to copy multiple text files but keeping origional name
 Originally Posted by spikee
there is no need for looking for the higherst value already in c:\test\ .
this code will only search one time.
I must have misunderstood you.
I thought you had a folder with various files in with incremental file names and that you wanted to write another file to the foldery by finding the current highest file name and incrementing the number within the file name by 1.
-
Feb 18th, 2010, 05:48 AM
#14
Thread Starter
New Member
Re: How to copy multiple text files but keeping origional name
ncrementing the number within the file name by 1.
is my problem i need to fix but i cant find a slolution.
-
Feb 18th, 2010, 05:55 AM
#15
Fanatic Member
Re: How to copy multiple text files but keeping origional name
I'm still not quite sure what you are trying to do.
One way you could do it - if you are struggling accessing the file names from the folder - is to stick the filenumber into a database and retrieve it each time you are writing a new file and increment the number by one, use it for the new file name and then update the database.
-
Feb 18th, 2010, 06:02 AM
#16
Thread Starter
New Member
Re: How to copy multiple text files but keeping origional name
vb Code:
Dim naar As String = "c:\test\" Dim keer As Integer = 1
naar = location the text file goes to ( normaly it is: c:\test\blabla.txt)
keer will say wich number the file wil be named at.
vb Code:
System.IO.File.copy(foundFile, naar)
this copy's (source,location+file name)
this is what i want to achive:
vb Code:
System.IO.File.copy(foundFile, naar+keer)
so the second file will be: (source,c:/test/2.txt etc... )
i hope you guys understand what i want to achive
the next file will be named 1 more than the one before.
ex: file 6: c:\test\6.txt
-
Feb 18th, 2010, 06:17 AM
#17
Fanatic Member
Re: How to copy multiple text files but keeping origional name
Dim strFileName As String = ""
Dim dir As New IO.DirectoryInfo("C:\test")
Dim aryFile As IO.FileInfo() = di.GetFiles("*.txt")
Dim myFile As IO.FileInfo
For Each myFile In aryFile
'myFile.Name will give you the name of the file
Next
I guess if your files are called just 1.txt, 2.txt etc - you could sort the array to extract the highest number in the file names, add one to it, and create your new file.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|