2009-03-25 23 views
51

मेरे पास C# का उपयोग कर फ़ोल्डर में कुछ फ़ाइलों के नाम बदलने (यानी प्रत्येक नाम को आईडी जोड़ना) का नाम बदलने का कार्य है।फ़ोल्डर में कुछ फ़ाइलों का नाम बदलें

उदाहरण: help.txt 1help.txt को

मैं यह कैसे कर सकते हैं?

उत्तर

82

FileInfo पर एक नज़र डालें।

क्या कुछ इस तरह:

void RenameThem() 
{ 
    DirectoryInfo d = new DirectoryInfo("c:/dir/"); 
    FileInfo[] infos = d.GetFiles("*.myfiles"); 
    foreach(FileInfo f in infos) 
    { 
     // Do the renaming here 
     File.Move(f.FullName, Path.Combine(f.DirectoryName, "1" + f.Name)); 
    } 
} 
+15

"1" + f.FullName –

8

समारोह है कि आप देख रहे हैं System.IO नाम स्थान का File.Move(source, destination) है। फ़ोल्डर की सामग्री तक पहुंचने के लिए DirectoryInfo वर्ग (उसी नामस्थान का) भी देखें।

1

How can I rename a file in C#? देखें। मुझे नहीं पता था कि C# का नाम नहीं है ... ऐसा लगता है कि आपको System.IO.File.Move(oldFileName, newFileName)

8

मैं इसे यहां डंप कर दूंगा क्योंकि मुझे अपने कोड के लिए यह कोड लिखना होगा।

using System; 
using System.IO; 

public static class FileSystemInfoExtensions 
{ 
    public static void Rename(this FileSystemInfo item, string newName) 
    { 
     if (item == null) 
     { 
      throw new ArgumentNullException("item"); 
     } 

     FileInfo fileInfo = item as FileInfo; 
     if (fileInfo != null) 
     { 
      fileInfo.Rename(newName); 
      return; 
     } 

     DirectoryInfo directoryInfo = item as DirectoryInfo; 
     if (directoryInfo != null) 
     { 
      directoryInfo.Rename(newName); 
      return; 
     } 

     throw new ArgumentException("Item", "Unexpected subclass of FileSystemInfo " + item.GetType()); 
    } 

    public static void Rename(this FileInfo file, string newName) 
    { 
     // Validate arguments. 
     if (file == null) 
     { 
      throw new ArgumentNullException("file"); 
     } 
     else if (newName == null) 
     { 
      throw new ArgumentNullException("newName"); 
     } 
     else if (newName.Length == 0) 
     { 
      throw new ArgumentException("The name is empty.", "newName"); 
     } 
     else if (newName.IndexOf(Path.DirectorySeparatorChar) >= 0 
      || newName.IndexOf(Path.AltDirectorySeparatorChar) >= 0) 
     { 
      throw new ArgumentException("The name contains path separators. The file would be moved.", "newName"); 
     } 

     // Rename file. 
     string newPath = Path.Combine(file.DirectoryName, newName); 
     file.MoveTo(newPath); 
    } 

    public static void Rename(this DirectoryInfo directory, string newName) 
    { 
     // Validate arguments. 
     if (directory == null) 
     { 
      throw new ArgumentNullException("directory"); 
     } 
     else if (newName == null) 
     { 
      throw new ArgumentNullException("newName"); 
     } 
     else if (newName.Length == 0) 
     { 
      throw new ArgumentException("The name is empty.", "newName"); 
     } 
     else if (newName.IndexOf(Path.DirectorySeparatorChar) >= 0 
      || newName.IndexOf(Path.AltDirectorySeparatorChar) >= 0) 
     { 
      throw new ArgumentException("The name contains path separators. The directory would be moved.", "newName"); 
     } 

     // Rename directory. 
     string newPath = Path.Combine(directory.Parent.FullName, newName); 
     directory.MoveTo(newPath); 
    } 
} 
+2

के बजाय पथ.कॉम्बिन (f.Directory.ToString(), "1" + f.Name) का उपयोग करें System.IO.FileInfo पर कोई नामकरण विधि नहीं है! – Chris

+3

@ क्रिसिस यही कारण है कि यात्रा ने 'FileInfo' के लिए 'नाम बदलें') 'विधि' लिखा था। – svick

+0

@ एसविक डह! जो मुझे स्क्रॉल करने और सभी कोड पढ़ने के लिए सिखाएगा। मेरी गलती। – Chris

0

आप File.Move इस तरह उपयोग कर सकते हैं,:

string oldFilePath = Path.Combine(Server.MapPath("~/uploads"), "oldFileName"); 
string newFilePath = Path.Combine(Server.MapPath("~/uploads"), "newFileName"); 

File.Move(oldFilePath, newFilePath); 
0

.NET फ्रेमवर्क 4.0 पर मैं FileInfo.MoveTo() विधि है कि केवल 1 तर्क

लेता है बस फ़ाइलों को स्थानांतरित करने के लिए अपने विधि इस

तरह लग रहा है का उपयोग
private void Move(string sourceDirName, string destDirName) 
{ 
    DirectoryInfo dir = new DirectoryInfo(sourceDirName); 
    FileInfo[] files = null; 

    files = dir.GetFiles(); 

    foreach (FileInfo file in files) 
    { 
     string temppath = Path.Combine(destDirName, file.Name); 
     file.MoveTo(temppath); 
    } 
} 

फ़ाइलों का नाम बदलने के लिए मेरी विधि इस तरह दिखती है

private void Rename(string folderPath) 
{ 
    int fileCount = 0; 

    DirectoryInfo dir = new DirectoryInfo(folderPath); 

    files = dir.GetFiles(); 

    foreach (FileInfo file in files) 
    { 
     fileCount += 1; 
     string newFileName = fileCount.ToString() + file.Name; 
     string temppath = Path.Combine(folderPath, newFileName); 

     file.MoveTo(temppath); 
    } 
} 

के रूप में आप फ़ाइल यह वाक्य रचना का नाम बदलें करने के लिए देख सकते हैं लगभग एक ही इसे स्थानांतरित करने के रूप में है, बस MoveTo() विधि का उपयोग करने से पहले filename को संशोधित करने की जरूरत है।

संबंधित मुद्दे