473,386 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Getting the Parent firectory of a file


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.

May 13 '06 #1
4 1184
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

May 13 '06 #2
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

May 13 '06 #3
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.

May 13 '06 #4
"Paulers" <Su*******@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'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
May 13 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Krustov | last post by:
<?php $dirname = "."; $dh = opendir($dirname); while ($file = readdir($dh)) { if (is_dir ("$dirname/$file")) { print ""; } print "$file<br>";
9
by: Robert Schneider | last post by:
Hi to all, I don't understand that: I try to delete a record via JDBC. But I always get the error SQL7008 with the error code 3. It seems that this has something to do with journaling, since the...
5
by: Thelma Lubkin | last post by:
I have a form/subform with the common one-to-many relationship. The form allows user to display records and move to other records via the selector, to add,delete, and edit them, with the related...
1
by: Earl Teigrob | last post by:
I did a ton of searching to try and find a simple solution to this issue and finally wrote my own, which I am sharing with everyone. In my searching, I did find a very complete and robust solution at...
10
by: darrel | last post by:
I have this structure: mypage.aspx (class = mypage) myusercontro.ascx On the mypage.aspx I can declare a variable: animal = "monkey" I can read this from the UC by simply doing this:...
4
by: What-a-Tool | last post by:
I am trying to write a program that will take all the members of a data base, add them to a tree, with all child relations as sub-nodes. I am having a problem getting the parent child relations...
5
by: Carlo \(mcp\) | last post by:
Good morning I have this lines: AddHandler MyControl1.Parent.Paint, AddressOf PaintParentHandler AddHandler MyControl2.Parent.Paint, AddressOf PaintParentHandler AddHandler...
8
by: cypher543 | last post by:
This has been driving me insane for the last hour or so. I have search everywhere, and nothing works. I am trying to use the subprocess module to run a program and get its output line by line. But,...
4
by: bushi | last post by:
hi! i'm designing a web application,basically it have two pages,"client_browser.apsx" & "adserver.aspx"."client_browser.aspx" contains an iframe,and in the iframe i have displayed my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.