Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / May 2006

Tip: Looking for answers? Try searching our database.

Getting the Parent firectory of a file

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Paulers - 13 May 2006 01:05 GMT
hello.

how do I get the file's parent directory name out of a path like this?

c:\test\myDirectory\file.txt

Im looking to extract the "Directory" from the path so I can create
myDirectory in another directory and copy the files.

what I am really trying to accomplish is to move the parent directory
and all it's files to another directory but I can not get a
Directory.Move to work so I am attempting to do it manually.

any help is greatly appreciated.
jayeldee - 13 May 2006 02:10 GMT
Here's a couple of functions you can use.

I'm curious - why couldn't you get the Directory.Move to work?  Did it
give you an error?  Docs say 'The destination cannot be another disk
volume or a directory with the identical name. It can be an existing
directory to which you want to add this directory as a subdirectory.'

Private Function GetDirectory(ByVal FileName As String) As String

       Dim fi As IO.FileInfo

       fi = New IO.FileInfo(FileName)

       Return fi.Directory.FullName

   End Function

   Private Sub CopyDirs(ByVal sourceDir As String, ByVal targetDir As
String, ByVal OverwriteFiles As Boolean)

       Dim sourceDirInfo As IO.DirectoryInfo
       Dim filesToCopy() As IO.FileInfo

       ' check that the source dir exists
       If Not IO.Directory.Exists(sourceDir) Then
           Throw New IO.DirectoryNotFoundException("Invalid sourceDir:
" & sourceDir)
       Else
           sourceDirInfo = New IO.DirectoryInfo(sourceDir)
           filesToCopy = sourceDirInfo.GetFiles
       End If

       ' Create the target directory if it doesn't exist
       If Not IO.Directory.Exists(targetDir) Then
           IO.Directory.CreateDirectory(targetDir)
       End If

       ' loop through the files and copy them
       For Each f As IO.FileInfo In filesToCopy
           f.CopyTo(targetDir & "\" & f.Name, OverwriteFiles)
       Next

       Return

   End Sub
Paulers - 13 May 2006 03:22 GMT
Wow thanks a bunch for all your help!

how do I pull the 2222 out of c:\1111\2222 ?

Im trying to construct the destination variable
jayeldee - 13 May 2006 04:32 GMT
If you create a new IO.FileInfo and pass it "c:\1111\2222" in the
constructor, you can get the name of the file and the name of its
directory with the the .Name and .Directory.FullName properties.
Herfried K. Wagner [MVP] - 13 May 2006 20:34 GMT
"Paulers" <SuperGh0d@gmail.com> schrieb:
> how do I get the file's parent directory name out of a path like this?

Check out the shared methods of 'System.IO.Path'.

Signature

M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.