script to rename files in ASP.NET C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • calibunny
    New Member
    • Apr 2010
    • 1

    #1

    script to rename files in ASP.NET C#

    We are trying to move files to SharePoint but need to rename them first since many have invalid characters. A previous developer started the script below. Any suggestions on how to adept it to check for valid file names and then rename the invalid files?

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using Microsoft.SharePoint;
    using System.Data.SqlClient;
    using System.Threading;
    
    namespace SPMoveFiles
    {    class Program
        {
            private static string site, web, list, problem;
            private static SPSite mySite;
            private static SPWeb myWeb;
            static void Main(string[] args)
            {          
                Console.WriteLine("Enter the directory of the files you would like to migrate.");
                string filePath = Console.ReadLine();
                
            RenameFiles(filePath);          
            }
    
            //This method needs to be changed.
            private static void RenameFiles(string filePath){
                string[] files = Directory.GetFiles(filePath);
                foreach (string fileName in files)
                {
                      //Concatenate the file to 20 characters.
                    //Replace invalid characters with an underscore.               
                }
                string[] dirs = Directory.GetDirectories(filePath);
                foreach (string dirName in dirs)
                {
                    
                    //Here, if the directory is greater than 20 characters, we must concatenate it to 20 characters.
    }
    }
    return result;}
    }
    }
    Thanks for any assistance,
    Cali
    Last edited by tlhintoq; Apr 17 '10, 12:41 AM. Reason: [CODE] ...Your code goes between code tags [/CODE]
  • yarbrough40
    Contributor
    • Jun 2009
    • 320

    #2
    try looking into the FileInfo class. It's in the System.IO Namespace

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      TIP: When you first created your question you were asked to wrap your code with [code] tags.
      [imgnothumb]http://files.me.com/tlhintoq/10jihf[/imgnothumb]
      It really does help a bunch. Look how much easier it is to read now that someone has done it for you. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        So your questions is "How do I rename a file?"
        System.Files.Mo ve(oldname, newname)
        It's weird, but it is Windows. If you move it from a folder, into the same folder with a new name, that is a re-rename.

        Comment

        • Monomachus
          Recognized Expert New Member
          • Apr 2008
          • 127

          #5
          Originally posted by tlhintoq
          So your questions is "How do I rename a file?"
          System.Files.Mo ve(oldname, newname)
          It's weird, but it is Windows. If you move it from a folder, into the same folder with a new name, that is a re-rename.
          Not only windows AFAIK in Linux too, and Unix

          Comment

          Working...