开发者

Why isn't my File.Move() working?

I'm not sure what exactly i'm doing wrong here...but i noticed that my File.Move() isn't renaming any files.

Also, does anybody know how in my 2nd loop, i'd be able to populate my .txt file with a list of the path AND sanitized file name?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace ConsoleApplication2
{
class Program
{
    static void Main(string[] args)
    {

        //recurse through files.  Let user press 'ok' to move onto next step        
        string[] files = Directory.GetFiles(@"C:\Documents and Settings\jane.doe\Desktop\~Test Folder for [SharePoint] %testing", "*.*", SearchOption.AllDirectories);
        foreach (string file in files)
        {
            Console.Write(file + "\r\n");开发者_运维知识库
        }
        Console.WriteLine("Press any key to continue...");
        Console.ReadKey(true);
        //End section

        //Regex -- find invalid chars
        string pattern = " *[\\~#%&*{}/<>?|\"-]+ *";
        string replacement = " ";
        Regex regEx = new Regex(pattern);

        string[] fileDrive = Directory.GetFiles(@"C:\Documents and Settings\jane.doe\Desktop\~Test Folder for [SharePoint] %testing", "*.*", SearchOption.AllDirectories);
        List<string> filePath = new List<string>();

        //clean out file -- remove the path name so file name only shows
        string result;            
        foreach(string fileNames in fileDrive)
        {
        result = Path.GetFileName(fileNames);
        filePath.Add(result);

        }

        StreamWriter sw = new StreamWriter(@"C:\Documents and Settings\jane.doe\Desktop\~Test Folder for [SharePoint] %testing\File_Renames.txt");

        //Sanitize and remove invalid chars
        foreach(string Files2 in filePath)
        {
            try
            {
                string sanitized = regEx.Replace(Files2, replacement);
                sw.Write(sanitized + "\r\n");
                System.IO.File.Move(Files2, sanitized);
                System.IO.File.Delete(Files2);




            }
            catch (Exception ex)
            { 
            Console.Write(ex);
            }


        }
        sw.Close();

    }

}

}

I'm VERY new to C# and trying to write an app that recurses through a specific drive, finds invalid characters (as specified in the RegEx pattern), removes them from the filename and then write a .txt file that has the path name and the corrected filename.

Any ideas?


Your filepath list contains only the file names. You have removed the directory info from them in the call to Path.GetFileName(), so your File.Move is looking for the target file in the application's default directory, rather than its original location.

I think your code for saving the sanitized file names is correct though. You should use the using() construct around your StreamWriter though, as below, to ensure that the file is closed once you're done with it.

//clean out file -- remove the path name so file name only shows
string result;            
foreach(string fileNames in fileDrive)
{
    // result = Path.GetFileName(fileNames); // don't do this.
    filePath.Add(fileNames);
}

using (StreamWriter sw = new StreamWriter(@"C:\Documents and Settings\jane.doe\Desktop\~Test Folder for [SharePoint] %testing\File_Renames.txt"))
{
        //Sanitize and remove invalid chars  
        foreach(string Files2 in filePath)  
        {  
            try  
            {  
                string filenameOnly = Path.GetFileName(Files2);
                string pathOnly = Path.GetDirectoryName(Files2);
                string sanitizedFilename = regEx.Replace(filenameOnly, replacement);
                string sanitized = Path.Combine(pathOnly, sanitizedFilename);  
                sw.Write(sanitized + "\r\n");  
                System.IO.File.Move(Files2, sanitized);  
            }  
            catch  
            {   
            }  
        }  
}


Are any exceptions being thrown in the call to File.Move()? You have an empty catch block beneath it which will be stopping you from seeing them. Try removing the catch{} or putting some code in there to log any exceptions.


Try using File.AppendAllLines() (with a collection) or File.AppendAllText() (for each individually) instead of a stream. That will make things a little easier.

Also, I understand not wanting your application to bomb, but at the very least, while you're currently writing/debugging comment your try block out so that you can see the exceptions.

Probably not an answer, but perhaps a suggestion to help.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜