C# read files and modify data in folders in batches

C# read files and modify the name of the picture in the folder or the content in txt in batches

Modify the names of pictures in a folder in batches

//例:
static void Main(string[] args)
        {
    
    
            ChangeName();
        }

static void ChangeName(){
    
    
    DirectoryInfo dir = new DirectoryInfo(@"路径");//读文件路径
    FileInfo [] files = dir.GetFiles("*.jpg");//查找类型
    for(int i =0;i<files.Length;i++){
    
    
         files[i].MoveTo(dir.FullName+@"\\第"+i.jpg);       
    }
}

Modify the contents of .txt files in a folder in batches

//例:
        static void Main(string[] args)
        {
    
    
            ReadText();
        }
		static void ReadText()
        {
    
    
            DirectoryInfo dir = new DirectoryInfo(@"D:\zzz");
            FileInfo[] files = dir.GetFiles("*.txt");
            for (int i = 0; i < files.Length; i++)
            {
    
    
                Console.WriteLine(files[i].Name);
                ChangeText(files[i]);
            }
        }
        static void ChangeText(FileInfo files )
        {
    
    
            DirectoryInfo dir = new DirectoryInfo(files.ToString());
            string[] fileLines = File.ReadAllLines(files.ToString());
            FileStream write = new FileStream(""+files.ToString,FileMode.OpenOrCreate);
            string str = "";
            char newChar = '1';
            char newChar1 = '0';
             foreach (var item in fileLines)
            {
    
    
                //Console.WriteLine(item.Substring(0,1));
                if (item.Substring(0,1)=="0")
                {
    
    
                    str = newChar + item.Substring(1);
                    Console.WriteLine(str);
                }
                else if (item.Substring(0,1) == "1")
                {
    
    
                    str = newChar1 + item.Substring(1);
                    Console.WriteLine(str);
                }
                else
                {
    
    
                   Console.WriteLine(item);
                }
               

            }
        }

Guess you like

Origin blog.csdn.net/qq_45598937/article/details/130316872
Recommended