473,396 Members | 1,814 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,396 software developers and data experts.

Renaming a directory

Hello,

Maybe this has been covered in the past in this group, but I wasn't
able to find any references. If it has been covered, please provide me
with a link of some sort.

I'm trying to rename a directory, eg.: C:\TestDir\Dirname1 to
C:\TestDir\dirname1, as you can see, this is only changed to
lowercase.

This is the code::

string dir = "C:\TestDir\Dirname1";
DirectoryInfo di = new DirectoryInfo(dir);
string newDirName = di.Name.ToLower();
string newPath = "C:\\TestDir\\" + newDirName;

//di.MoveTo(newPath);
Directory.Move(di.FullName, newPath);
This throws the exception:

Could not rename dir: C:\TestDir\Dirname1 to: C:\TestDir\dirname1
because of the following reason:
Source and destination path must be different.
I'm also looking at P/Invoke with the functions in shell32 to to it,
but that seems overkill...
Any idea's ?

Thanks in advance,

JB
Nov 22 '06 #1
4 8489

Janiek Buysrogge wrote:
I'm trying to rename a directory, eg.: C:\TestDir\Dirname1 to
C:\TestDir\dirname1, as you can see, this is only changed to
lowercase.
Windows path names are case insensitive, which you can test in Windows
Explorer by trying to create two folders; one with a lower case initial
letter and one upper.

In which case [excuse the pun] you have four options:
1. Use Linux or a Mac
2. Change whatever creates the folder in the first place, to
decapitalise the name
3. Use an intermediate name; i.e. change 'Dirname1' to 'temp' then to
'dirname1'.
4. Another option that somebody else will point out which is much
neater than my option 3.

Nov 22 '06 #2
adi
Try this:

1. Rename your directory to C:\TestDir\Dirname1_temp (make sure you
replace the temp portion with a string that will make your new path
unique
2. Create C:\TestDir\dirname1
3. Copy everything from C:\TestDir\Dirname1_temp
4. Delete C:\TestDir\Dirname1_temp

Pro's: will do the job
Con's:
- extra disk space used (at one moment you have 2 copies of
C:\TestDir\Dirname1)
- extra coding
- operation may take some time if C:\TestDir\Dirname1 contains a lot of
files/data
Janiek Buysrogge a scris:
Hello,

Maybe this has been covered in the past in this group, but I wasn't
able to find any references. If it has been covered, please provide me
with a link of some sort.

I'm trying to rename a directory, eg.: C:\TestDir\Dirname1 to
C:\TestDir\dirname1, as you can see, this is only changed to
lowercase.

This is the code::

string dir = "C:\TestDir\Dirname1";
DirectoryInfo di = new DirectoryInfo(dir);
string newDirName = di.Name.ToLower();
string newPath = "C:\\TestDir\\" + newDirName;

//di.MoveTo(newPath);
Directory.Move(di.FullName, newPath);
This throws the exception:

Could not rename dir: C:\TestDir\Dirname1 to: C:\TestDir\dirname1
because of the following reason:
Source and destination path must be different.
I'm also looking at P/Invoke with the functions in shell32 to to it,
but that seems overkill...
Any idea's ?

Thanks in advance,

JB
Nov 22 '06 #3
Copy the contents of the Directory to another temporary directory. Delete
the first directory, and create the new one with the lower-case name. Copy
the contents of the temp directory back to the new directory and delete the
temp directory.

--
HTH,

Kevin Spencer
Microsoft MVP
Ministry of Software Development
http://unclechutney.blogspot.com

Never trust a dunderhead with a blunderbuss.
"Janiek Buysrogge" <J.*********@Televic.comwrote in message
news:k9********************************@4ax.com...
Hello,

Maybe this has been covered in the past in this group, but I wasn't
able to find any references. If it has been covered, please provide me
with a link of some sort.

I'm trying to rename a directory, eg.: C:\TestDir\Dirname1 to
C:\TestDir\dirname1, as you can see, this is only changed to
lowercase.

This is the code::

string dir = "C:\TestDir\Dirname1";
DirectoryInfo di = new DirectoryInfo(dir);
string newDirName = di.Name.ToLower();
string newPath = "C:\\TestDir\\" + newDirName;

//di.MoveTo(newPath);
Directory.Move(di.FullName, newPath);
This throws the exception:

Could not rename dir: C:\TestDir\Dirname1 to: C:\TestDir\dirname1
because of the following reason:
Source and destination path must be different.
I'm also looking at P/Invoke with the functions in shell32 to to it,
but that seems overkill...
Any idea's ?

Thanks in advance,

JB

Nov 22 '06 #4

To change case use two separate Move operations as follows:

Directory.CreateDirectory(@"C:\Temp\Dir1");
Directory.Move(@"C:\Temp\Dir1", @"C:\Temp\dir1_temp");
Directory.Move(@"C:\Temp\dir1_temp", @"C:\Temp\dir1");
There's no need to copy the contents of the directory to a different
directory, it'd be a waiste fo space. Make sure the temporary
directory doesn't exist (if it does use a different temp name).

HTH,

Sam
------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.


On Wed, 22 Nov 2006 13:30:51 +0100, Janiek Buysrogge
<J.*********@Televic.comwrote:
>Hello,

Maybe this has been covered in the past in this group, but I wasn't
able to find any references. If it has been covered, please provide me
with a link of some sort.

I'm trying to rename a directory, eg.: C:\TestDir\Dirname1 to
C:\TestDir\dirname1, as you can see, this is only changed to
lowercase.

This is the code::

string dir = "C:\TestDir\Dirname1";
DirectoryInfo di = new DirectoryInfo(dir);
string newDirName = di.Name.ToLower();
string newPath = "C:\\TestDir\\" + newDirName;

//di.MoveTo(newPath);
Directory.Move(di.FullName, newPath);
This throws the exception:

Could not rename dir: C:\TestDir\Dirname1 to: C:\TestDir\dirname1
because of the following reason:
Source and destination path must be different.
I'm also looking at P/Invoke with the functions in shell32 to to it,
but that seems overkill...
Any idea's ?

Thanks in advance,

JB
Nov 22 '06 #5

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

Similar topics

0
by: Joe | last post by:
How can I rename a folder that has the same name, but with different character casing? When I do either of the following, I get an error saying "Source and destination path must be different.": ...
1
by: Don Leverton | last post by:
Hi Folks, I have been given a CD with approx 130 .xls files (bean-counters!) that I would like to import and merge to ONE table (tblTradeshow). The XL files are *similarly*, but not...
1
by: Tony | last post by:
I'm porting an old Coldfusion based content and asset management system to ASP.NET (and oh, how good it feels to write REAL code again - was a C++ hack for 10 years, started doing coldfusion about...
1
by: Jason B | last post by:
Hello, I have been trying to figure out how to rename my IIS directories and still have the project be able to run through VS. I have renamed the directories listed in the csproj.webinfo file and...
8
by: Ron Vecchi | last post by:
I know this has been asked before but I out of all the posts I've read regarding using Directory.Move and then the application restarting itself I have found no answer except that Sessions are...
6
by: Martin Bischoff | last post by:
Hi, I'm creating temporary directories in my web app (e.g. ~/data/temp/temp123) to allow users to upload files. When I later delete these directories (from the code behind), the application...
1
by: boops boops | last post by:
Perhaps this is obvious to everyone but me, but how do you rename an existing project when using the .NET IDE? It allows you to save the current form as something else, but not the whole project....
3
by: Daves | last post by:
why the heck can't I use Directory.Move() to rename a directory (containing files) to a entry 2 levels below e.g. uploads/tempfiles -> uploads/tempfiles/48/003files if I do this in one line...
1
by: dkmarni | last post by:
Hi, I am trying to do this perl script, but not able to complete it successfully. Here is the description what the script has to do.. Accept two and only two command line arguments. Again,...
1
by: thom21 | last post by:
hi, I'm having trouble figuring out exactly how to solve the following problem: I have an xml document "doc1.xml" that needs to be renamed and then moved to a new folder directory...
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.