Click here to Skip to main content
15,891,702 members
Please Sign up or sign in to vote.
1.00/5 (6 votes)
See more:
How would I rename a File with pure C# .NET (no API!) using wildcards and using actually a real "rename" or "name" command, not a move or copy or something like that.

1) I wish not to move the files, don't suggest to "move" - they are large large files!
2) I don't wish to use the Visual Basic namespace for it - I would like to do it with C#.
3) I need to use Wildcards on this!

the dos equivalent would be: ren C:\ServerLogs\etc\*.log *.oldlogs

What I have tried:

While VB6 and even VB .NET has an inbuild command to rename files I cannot find a native C# command. Only including the VB namespace and this is ruled out, because the tool shall stay small and run only with pure C#.
I was okay to use the dos command, but there is a timing issue and a path issue with it. The path issue is, that with the dos command the tool only works if I have actually an explorer window open in the logfile directory. if I change my path the tool cannot work longer. so I tried to use DOS but it is to problematic with the currentdir variable. I found nothing on the whole .NET about a valid rename with C#, only to MOVE the whole file. I will not move a file to rename it!
Has someone a better idea, if unavoidable even as an API?

Attention: Please no loops there, need only wildcards without slow loops.

I agree on a DOS Command too. But I need the right syntax for the DOS Command that I have not to rely on curdir and my application would work in any directory.
If this is possible, please tell me how I have to call the DOS Command the right way with full paths in both strings and how I can wait until the DOS Command is really finished. And then I need to know if it was really successfully finished. That's the problem with DOS, else I would use only DOS for C#, since C# has no powerful commands to deal with files!
Posted
Updated 12-Apr-16 0:19am
v3
Comments
Sinisa Hajnal 11-Apr-16 9:54am    
VB.NET has the method, as you noticed, but internally it uses File.Move just with some extra checks (existence of the files in question).

Also from MSDN: When using Rename to copy a file from an unprotected location to a protected location, the file retains the less restricted rights. Check to make sure that you are not introducing a possible security risk.
Sergey Alexandrovich Kryukov 11-Apr-16 10:28am    
In any of the OS C# can work in, there is no such thing as "DOS command".
In C#, there is no such thing as "command".

You whole question is the reflection of a user's, non-engineering views. You need to start seeing the real operations behind user-level metaphors. And yes, you need Move. You idea of "whole file" is totally imaginary. The operation depends on Move is equivalent to copy and delete only between different volumes; in other cases, it is not.

—SA
Philippe Mori 12-Apr-16 12:49pm    
What you want is not directly available and you don't want to learn/use what would help you fixes your problems. I could do many suggestions but obviously, they are all above your current skills and you don't see to want to invest time in doing things correctly.

In C#, you rename a file by using the System.IO.File.Move method.
If the containing folder for the source and destination files is the same, the file will not be moved, but renamed instead (i.e., there will not be any copy of the file, only the filename will be changed).

SO: How to rename a file in .NET?[^]
MSDN: File.Move Method (String, String)[^]

[Edit] Added example code
There is also a Path.ChangeExtension method that could suit your need.
Path.ChangeExtension Method (String, String)[^]
C#
foreach (string filename in Directory.GetFiles(@"C:\ServerLogs\etc", "*.log", SearchOption.TopDirectoryOnly)
{
   Path.ChangeExtension(filename, ".oldlogs");
}
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 11-Apr-16 10:29am    
Sure, a 5.
—SA
phil.o 11-Apr-16 10:33am    
Thanks Mr Kryukov :)
Sergey Alexandrovich Kryukov 11-Apr-16 10:40am    
Ah, we forgot the trickier part of the question, about wild cards (and Solution 1 is just wrong). Please see Solution 4.
—SA
Sergey Alexandrovich Kryukov 11-Apr-16 17:36pm    
Your whole application should be: take set of files in a directory (recursive or not) and move it. And this application (only one file) can be put down as a whole, and only if you don't understand what is the comprehensive content sufficient for communication on the problem. Not the application where you want to use what you are asking about. Isn't it obvious?
—SA
phil.o 12-Apr-16 3:40am    
"move" was anyway outruled in beforehand
You have to explain this then. Because from what I understand you do not want the file to be copied then deleted, you want it to be renamed directly. Which is exactly what the Move method is doing when the source and destination folders are the same.
If you do not want to use the Move method just because its name is Move, then it would be like asking "Ok I want to drive but I don't want to step down on the gas pedal".
And for the wildcard thing, you do realize what these wildcars are for, don't you? It's just a way to say "Take all the .log files in a place and change their extension to .oldlogs". There is no existing method in C# that will analyze the wildcards the way a DOS program does; instead, you have to enumerate in a loop the files you are interested in, and act on them iteratively. Solution 1 has given the way.
Please define exactly what you mean when you say "no move".
Not as easy as DOS unfortunately - I would start with something like

C#
DirectoryInfo d = new DirectoryInfo(@"C:\ServerLogs\etc\");
FileInfo[] infos = d.GetFiles();
foreach(FileInfo f in infos)
{
    String newFileName = // Use A Regex Here To Translate f.FullName -> newFileName 
                         // Or Maybe newFileName = f.FullName.Replace(".log", ".oldlogs");
    File.Move(f.FullName, newFileName);
}
 
Share this answer
 
v6
Comments
Garth J Lancaster 12-Apr-16 7:51am    
the c# solution needs sfa to make it work - iirc you can pass a wildcard to d.GetFiles(); as in d.GetFiles("*.oldlogs"); to get only those files

if you want a DOS solution you could do

string renCmdText = "/C ren C:\ServerLogs\etc\*.log *.oldlogs";
System.Diagnostics.Process.Start("cmd.exe", renCmdText);
From solution 2 and 3, you already know that you should really need File.Move. I only have to add that even of the source and destination files are in different directories but in the same disk volume, it's not physically moving of the whole file — the file system is not that stupid. It's still closer to renaming, by functionality.

Now, remaining thing is using the wildcard. No one explained it; and the idea of using Regex (Solution 1) is irrelevant.

Really, the wildcards can come with System.IO.Directory.GetFiles(string, string, SearchOption), in a second parameter, searchPattern:
Directory.GetFiles Method (String, String, SearchOption) (System.IO)[^].

However, it works not exactly as most people would reasonably expect. I would even call it a flaw or a bug in the system. Please see my past answer explaining that: Directory.Get.Files search pattern problem[^].

—SA
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 11-Apr-16 13:43pm    
Read my answer carefully. It's second parameter. It can have wild cards.
There is no such thing as "extension".
I have no idea why you have the problem you described; you did not demonstrate it. Such problem doesn't really exist. Maybe you screwed up something, but who knows how? The problem is really simple.
—SA
Sergey Alexandrovich Kryukov 11-Apr-16 17:30pm    
No. There is no "commercial version" of .NET. All versions are freely re-distributable. Read this:
https://msdn.microsoft.com/en-us/library/system.io.directory%28v=vs.110%29.aspx.

You will see three (3) GetFiles functions with one, two or three parameters.

Now, there is no such thing as "rename drive", and never way.
What function "doesn't handle files only the same directories"? What can it even mean?

Look, to do development, you have to learn to pay at least minimal attention.

—SA
Richard MacCutchan 12-Apr-16 6:38am    
That is because you misunderstand what you are supposed to do. You cannot use wildcards inside a program like that, as they have no meaning. You can use them to get a list of all matching files, but then you will still need to rename (aka move) each one individually. And that is all that the command line rename command does under the covers. So, bottom line, why do you want to write a program that does exactly what rename does already?

Oh, and by the way, renaming from drive C: to drive E: means that the files will have to be physically moved.
Sergey Alexandrovich Kryukov 12-Apr-16 8:57am    
Listen again. I cannot help a person who cannot pay at least some minimal attention.
A wild card comes in the second parameter of Directory.GetFiles. NOT File.Move.
—SA
Sergey Alexandrovich Kryukov 12-Apr-16 9:00am    
From the standpoint of any API, there are volumes. It does not matter if they are partitions on the same disk.
After all, do you understand the purpose of partitions?

And there is no such thing as "DOS command" on your system.

—SA
Hi,

for all intrinsic purposes the File.Move() method is a file rename method if the path is the same for the file being "moved"(absolute)it will only rename the file,
else it will move the file if it is a different path(Relative). See the following MSDN link for further information File.Move Method (String, String) (System.IO)[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 11-Apr-16 13:55pm    
You failed to understand what we explained to you. What Move actually does depends on actual parameters. It's not what you assume it does.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900