Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Visual Basic .NET (http://www.programmingforums.org/forum19.html)
-   -   copying files (http://www.programmingforums.org/showthread.php?t=11319)

NightShade01 Sep 14th, 2006 7:25 PM

copying files
 
Ok so i'm trying to copy a file from one folder to another and i'm running into a problem. currently i know that in order to copy something you use the File.Copy( , ) method (assuming you import system.io.*) problem being the dest file can't be an exsisting file or directory. fine. I used a a loop to pull the file up and then tried the copy method. now my problem being that i'm getting a full path extension and not just a file name. Any one know how to cut the file name path apart without knowing how long the file is or it's name?

ie

:

dim user as string
user = system.environment.username.tostring
directory.createDirectory("C:\Documents and Settings\" & user & "\Desktop\My Backup")

dim backupDest as string = "C:\Documents and Settings\" & user & "\Desktop\My Backup"

dim myFiles as String() = Directory.GetFiles("C:\tmp", "*.doc")

dim i as integer
for i = 0 to myFiles.GetUpperBound(i)
File.Copy(myFiles(i), backupDest & "\" & myFile(i))    //problem occurs here
next


problem i'm having is that myFile(i) where the comment is, i actually the value "C:\tmp\testing.doc" and the two paths are colliding. I need to get the myFile(i) to be just testing.doc

The only thing i can think of is indexOfLast? but doesn't that return an integer?

melbolt Sep 15th, 2006 9:32 AM

there are several ways to do this, here is one.

:

  1.  
  2.         'array to hold substrings
  3.         Dim StringArray() As String
  4.  
  5.         'the path to parse
  6.         Dim RandomPath As String = "C:\tmp\testing.doc"
  7.  
  8.         'breaks the path into substrings "\" is the delimiter and stores it in StringArray
  9.         StringArray = RandomPath.Split("\")
  10.  
  11.         Dim FileName As String
  12.  
  13.         'get the filename which will be in the last index of the array
  14.         FileName = StringArray(StringArray.Length - 1)
  15.  
  16.         MsgBox("The filename is " & FileName)


john Wesley Sep 15th, 2006 11:13 AM

This is pretty much identical to yours with only a slight difference; but try it, it should work. I dont have .net right here so I cant test it for you.


:

    Dim user as String = _
    System.Enviroment.Username.ToString

    Dim backupdest as string = _
    "C:\Documents and Settings\" & user & "\Desktop\My Backup\")

    Directory.CreateDirectory(backupdest)

    Dim myFiles = Directory.GetFiles("C:\tmp", "*.doc")

        For each file as string in myFiles
                File.Copy(file, backupdest & file)
        next


NightShade01 Sep 15th, 2006 12:11 PM

Alright so i tried it and at first i got it to work within the same folder (searching c:\tmp for whatever i needed) then when i knew it work i changed it so that it wouls search all of C:\ as oppsed to just C:\tmp....now it doesn't work here is what i have:

:

dim user as string
user = System.environment.username.tostring
dim mydest as string = "C:\documents and settings\" & user & "\desktop\mybackup\"

try
dim myfiles as string() = Directory.getfiles("C:\", "*.doc",  searchoption.alldirectories)  //comment here
dim i as integer

for i = 0 to myfiles.getupperbound(i)
dim filesbeingcopied as string
dim r as intger = myfiles(i).lastindexof("\")
filesbeingcopied = myfiles(i).substring(r +1)

file.copy(myfiles(i), mydest & filesbeingcopied)
next

catch ex as exception

end try


where i have the comment that's the only line i've changed where the program stops working as intended i went from C:\tmp to C:\ and it doesn't find anyfiles.

john Wesley Sep 29th, 2006 6:55 AM

:

Dim user As String = System.Environment.Username.ToString()
Dim dest As String = "C:\Documents and Settings\" & user & "\Desktop\MyBackup\"

Try

        Dim files As String() = Directory.GetFiles("C:\","*.doc", SearchOption.AllDirectories)

        Dim enum As Collections.IEnumerator = files.GetEnumerator()

        While enum.MoveNext
                Dim fi As New FileInfo(enum.Current)
                File.Copy(fi.Directory & "\" & fi.Name, dest & fi.Name))
        End While

Catch ex As Exception
        MsgBox(ex.Message)
End Try


Try that. I have never used this method before and have just written it, unfortunately I am not at a PC with VS so this is untested.

randum77 Oct 18th, 2006 10:22 AM

Not to bring up an old post, but I was digging around and searching for Copy method and directory searching method. I noticed on the previous codes there is a statement as such:
:

Dim files As String() = Directory.GetFiles("C:\","*.doc", SearchOption.AllDirectories
I'm using VB.net 2003 and I don't have the option to add the SearchOption.AllDirectories. Is this something that isn't available in my version or am I blind? Just curious. :)

john Wesley Oct 18th, 2006 11:46 AM

As long as you can honestly assure me you are not blind then I guess it is safe to say the parameter is not there. But wether this has to do with your version being 2003 im not sure, my presumption is that it is the version of the framework you are using that matters. Although 2003 would come packaged with .NET1.1 an upgrade to .NET2.0 would be recognised by your IDE I beleive, though this is another presumption.

So maybe its just a newer version of the framework that you need...?

randum77 Oct 18th, 2006 12:28 PM

Quote:

Originally Posted by john Wesley (Post 116894)
As long as you can honestly assure me you are not blind then I guess it is safe to say the parameter is not there. But wether this has to do with your version being 2003 im not sure, my presumption is that it is the version of the framework you are using that matters. Although 2003 would come packaged with .NET1.1 an upgrade to .NET2.0 would be recognised by your IDE I beleive, though this is another presumption.

So maybe its just a newer version of the framework that you need...?

I won't vouch that I am not blind. i've been known to have bouts or complete idiocy and blindness at times. Let me dig around a bit and see if I can't validate the version of frame work I have. I just got bombarded w/ more work when typing this. Ugh, the paid life...

randum77 Oct 18th, 2006 1:46 PM

Yep, seems our company is still running 1.0 framework. That stinks. This app need to run on more computers then my own. I guess I have to live w/o that option, damnit.


All times are GMT -5. The time now is 1:14 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC