Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 14th, 2006, 6:25 PM   #1
NightShade01
Programmer
 
Join Date: Oct 2005
Posts: 52
Rep Power: 3 NightShade01 is on a distinguished road
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?
NightShade01 is offline   Reply With Quote
Old Sep 15th, 2006, 8:32 AM   #2
melbolt
Hobbyist Programmer
 
melbolt's Avatar
 
Join Date: Feb 2005
Location: PA, USA
Posts: 244
Rep Power: 4 melbolt is on a distinguished road
Send a message via AIM to melbolt Send a message via Yahoo to melbolt
there are several ways to do this, here is one.

vbnet Syntax (Toggle Plain Text)
  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)
__________________
I have never let my schooling interfere with my education. -Mark Twain-

Xbox live gamertag: melbolt
melbolt is offline   Reply With Quote
Old Sep 15th, 2006, 10:13 AM   #3
john Wesley
Hobbyist Programmer
 
john Wesley's Avatar
 
Join Date: May 2006
Location: United Kingdom
Posts: 119
Rep Power: 3 john Wesley is on a distinguished road
Send a message via MSN to john Wesley Send a message via Yahoo to john Wesley
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
__________________
Mona Lisa must of had the highway blues you can tell by the way she smiles..
john Wesley is offline   Reply With Quote
Old Sep 15th, 2006, 11:11 AM   #4
NightShade01
Programmer
 
Join Date: Oct 2005
Posts: 52
Rep Power: 3 NightShade01 is on a distinguished road
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.
NightShade01 is offline   Reply With Quote
Old Sep 29th, 2006, 5:55 AM   #5
john Wesley
Hobbyist Programmer
 
john Wesley's Avatar
 
Join Date: May 2006
Location: United Kingdom
Posts: 119
Rep Power: 3 john Wesley is on a distinguished road
Send a message via MSN to john Wesley Send a message via Yahoo to john Wesley
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.
__________________
Mona Lisa must of had the highway blues you can tell by the way she smiles..
john Wesley is offline   Reply With Quote
Old Oct 18th, 2006, 9:22 AM   #6
randum77
Programmer
 
randum77's Avatar
 
Join Date: Jun 2006
Location: Fayettehell, NC
Posts: 56
Rep Power: 3 randum77 is on a distinguished road
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.
__________________
_Marshall_

"America has bred a society that is innocent and incapable of accepting responsibility, but yet, is able to place blame on others without guilt."
randum77 is offline   Reply With Quote
Old Oct 18th, 2006, 10:46 AM   #7
john Wesley
Hobbyist Programmer
 
john Wesley's Avatar
 
Join Date: May 2006
Location: United Kingdom
Posts: 119
Rep Power: 3 john Wesley is on a distinguished road
Send a message via MSN to john Wesley Send a message via Yahoo to john Wesley
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...?
__________________
Mona Lisa must of had the highway blues you can tell by the way she smiles..
john Wesley is offline   Reply With Quote
Old Oct 18th, 2006, 11:28 AM   #8
randum77
Programmer
 
randum77's Avatar
 
Join Date: Jun 2006
Location: Fayettehell, NC
Posts: 56
Rep Power: 3 randum77 is on a distinguished road
Quote:
Originally Posted by john Wesley View Post
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...
__________________
_Marshall_

"America has bred a society that is innocent and incapable of accepting responsibility, but yet, is able to place blame on others without guilt."
randum77 is offline   Reply With Quote
Old Oct 18th, 2006, 12:46 PM   #9
randum77
Programmer
 
randum77's Avatar
 
Join Date: Jun 2006
Location: Fayettehell, NC
Posts: 56
Rep Power: 3 randum77 is on a distinguished road
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.
__________________
_Marshall_

"America has bred a society that is innocent and incapable of accepting responsibility, but yet, is able to place blame on others without guilt."
randum77 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Unix commands compatible with Windows? titaniumdecoy Bash / Shell Scripting 7 Oct 5th, 2006 7:25 AM
copying files first before deleting them harjit Visual Basic 1 May 15th, 2005 7:05 AM
Checking source codes of image, audio and video files on_auc C++ 3 Feb 21st, 2005 8:36 PM
shutil.copy corrupting files? TimL Python 0 Feb 20th, 2005 3:32 PM
Bash script to access all files in a directory shinni Bash / Shell Scripting 4 Feb 18th, 2005 3:26 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:43 AM.

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