C# - Rename if length over X

Status
Not open for further replies.

EvilAlchemist

2[H]4U
Joined
Jan 11, 2008
Messages
2,730
Been trying to research this but not coming up with many answers on my own:

Have a small application that pulls the pdf files from a directory and shows them in a listbox.
All of the file names "should" be 8 Characters long + file extension.
Lately, the personal saving the pdf files are adding extra things into the name.

Correct Name: 15001123.pdf
What is Happening: 15001231 10/2 Johnny Report.pdf

It makes the listbox look tacky and just kinda irks me i have to keep asking ppl to stop.

So, I want to make a string to loop threw the directory and rename the files basically after 8 characters to "chop off" all the extra they add on ..

Any suggestions ?

Here is my code that pulls the files for the listbox and deletes older files.
(Just so you can see i do have a working program and not a random question)
Code:
            InitializeComponent();
            DirectoryInfo dinfo = new DirectoryInfo("\\\\SERVER\\Forms\\CAD Reports");
            FileInfo[] Files = dinfo.GetFiles("*.pdf");
            var orderedFiles = Files
            .OrderByDescending(d => d.CreationTime)
            .Take(20);


            foreach (FileInfo file in orderedFiles)
            {
                CADList.Items.Add(Path.GetFileNameWithoutExtension(file.Name));
            }
            string[] oldfiles = Directory.GetFiles("\\\\SERVER\\Forms\\CAD Reports");

            foreach (string file in oldfiles)
            {
                FileInfo fi = new FileInfo(file);
                if (fi.CreationTime < DateTime.Now.AddDays(-14))
                    fi.Delete();

            }
 
Path.GetfileNameWithoutExtension(file.Name) is returning a string (or should be), so i would just test with .Length and, if longer than 8 chars, rename as desired.

You'd then probably want to call your CADList.Items.Add() method on the new renamed filename.
 
Last edited:
You'd then probably want to call your CADList.Items.Add() method on the new renamed filename.

I just noticed I have had that in backward order for 2 months ...
I should be deleting then calling the files for the list.
Guess no one ever ran into the issue with older files.

Trying your suggestions now .. going to see if i can figure this out. ty
 
okay, i gave my self a headache trying to get this working with no success..
 
Last edited:
Can you post your latest implementation / attempt?

One thing i forgot to mention is that once you've detected that the length of your current CAD file name is too long, you'll need to actually rename the file on disk too. My suggestion above didn't do that, it simply made the list box pretty.

What you could try now is:
1) Read in each CAD File
2) Test the length of the filename
3) If longer than 8 chars, get the new name string
4) Rename the File on disk with System.IO.File.Move() //This is File.Move, not FileInfo.Move
5) Now add the new file to your CAD list


Note: If System.IO.File.Move() doesn't work, you could try the VB assembly Microsoft.VisualBasic.FileIO.FileSystem.RenameFile(a, b);. You'd just need to add a ref to it: Microsoft.VisualBasic.
 
Last edited:
Just threw this together. Seems to work fine.

Code:
static void Renamer()
        {
            List<string> filePaths = Directory.GetFiles(@"c:\filetest\","*.pdf").ToList();
            foreach (string origPath in filePaths)
            {
                string nameOnly = Path.GetFileNameWithoutExtension(origPath);
                if (nameOnly.Length > 8)
                {
                    string newName = nameOnly.Substring(0, 8);
                    string newPath = Path.Combine(Path.GetDirectoryName(origPath), newName + ".pdf");
                    File.Move(origPath, newPath);
                }
                
            }
        }
 
Also take note- in what I posted, it would fail if two or more files share the first 8 letters of a file name - say she uploaded "stupidlyNamedFile.pdf" and "stupidlyNamedFile-FINAL.pdf". You'll probably want to put some sort of check in for situations like that,and rename accordingly- I leave the implementation to you, it's like 4 lines of code.
 
Better question: why the stupid character limit? Because it makes something look "tacky"?

Any suggestions ?

Drop the character limit requirement, make the listbox wider, and avoid this whole mess.
 
Better question: why the stupid character limit? Because it makes something look "tacky"?



Drop the character limit requirement, make the listbox wider, and avoid this whole mess.

I would say this is probably the better approach.
 
Status
Not open for further replies.
Back
Top