C# 重命名图像文件
我想重命名一个目录(C# 重命名图像文件,c#,image,rename,C#,Image,Rename,我想重命名一个目录(C:\Users\userPC\Desktop\MATT\PROVA\IMG\AW12),其中包含带有C.的3000个图像文件。这些图像实际上具有以下类型的名称: area1_area2_area3_area4.jpg 我想将区域2和区域4组成一个新文件,重命名为area2\u area4.jpg。这些区域没有固定数量的字符。我该怎么办?我发现了这个讨论 但是我是一个编程新手,我无法研究如何解决我的问题。这里有一个解决方案。请注意,在制造任何混乱之前,它不会检查:) 必须使
C:\Users\userPC\Desktop\MATT\PROVA\IMG\AW12
),其中包含带有C.
的3000个图像文件。这些图像实际上具有以下类型的名称:
area1_area2_area3_area4.jpg
我想将区域2和区域4组成一个新文件,重命名为area2\u area4.jpg
。这些区域没有固定数量的字符。我该怎么办?我发现了这个讨论
但是我是一个编程新手,我无法研究如何解决我的问题。这里有一个解决方案。请注意,在制造任何混乱之前,它不会检查:) 必须使用此类型的参数调用该方法
rename("C://Users//userPC//Desktop//MATT//PROVA//IMG//AW12//")
可以在此处更改分隔符->
s.split(“')
这里有一个解决方案。请注意,在弄乱之前它不会检查:)
必须使用此类型的参数调用该方法
rename("C://Users//userPC//Desktop//MATT//PROVA//IMG//AW12//")
可以在此处更改分隔符->
s.split(“'')
首先使用
var listOfFileNames = Directory.GetFiles(directory);
您提到这些区域没有固定数量的字符(我假设这些区域由下划线字符分隔)。因此,使用下划线字符作为分隔符,将每个文件名分成四个区域
然后构建新的文件名,例如
foreach(var fileName in listOfFileNames)
{
var areas = fileName.Split('_');
var newFileName = string.Format({0}{1}{2}, areas[0], areas[1],".jpg");
}
希望这有助于首先使用
var listOfFileNames = Directory.GetFiles(directory);
using System.IO;
namespace ConsoleApplication
{
public class Program
{
public static void Main(string[] args)
{
// Rename all files in the C:\Temp\ directory.
Program.RenameFiles(new DirectoryInfo(@"C:\Temp\"));
}
public static void RenameFiles(DirectoryInfo path)
{
// Does the path exist?
if (path.Exists)
{
// Get all files in the directory.
FileInfo[] files = path.GetFiles("*.jpg");
foreach (FileInfo file in files)
{
// Split the filename
string[] parts = file.Name.Split('_');
// Concatinate the second and fourth part.
string newFilename = string.Concat(parts[1], "_", parts[3]);
// Combine the original path with the new filename.
string newPath = Path.Combine(path.FullName, newFilename);
// Move the file.
File.Move(file.FullName, newPath);
}
}
}
}
}
您提到这些区域没有固定数量的字符(我假设这些区域由下划线字符分隔)。因此,使用下划线字符作为分隔符,将每个文件名分成四个区域
然后构建新的文件名,例如
foreach(var fileName in listOfFileNames)
{
var areas = fileName.Split('_');
var newFileName = string.Format({0}{1}{2}, areas[0], areas[1],".jpg");
}
希望这对使用
System.IO
和System.Text.RegularExpressions
中的类非常简单的操作有所帮助。向谷歌寻求关于这些名称空间的帮助,这不会花费你太长时间!区域之间有区域分隔符吗?我的意思是,例如''@looper',我尝试了一些从网络上获取的代码,但没有成功。我的想法是使用FileStream,但不幸的是,我还是一个自己编写完整脚本的新手。@trippino是的,下划线将四个区域分开。使用System.IO
和System.Text.RegularExpressions
中的类将非常简单。向谷歌寻求关于这些名称空间的帮助,这不会花费你太长时间!区域之间有区域分隔符吗?我的意思是,例如''@looper',我尝试了一些从网络上获取的代码,但没有成功。我的想法是使用FileStream,但不幸的是,我还是一个自己编写完整脚本的新手。@trippino是的,下划线将这四个区域分开。感谢用户65439抽出时间。我尝试过这样做:void按钮1click(objectsender,EventArgs e){var-listOfFileNames=Directory.GetFiles(“C://Users//userPC//Desktop//MATT-SUPERDRY//PROVA//IMG//AW12//”);foreach(listOfFileNames中的var-fileName){var-arears=fileName.Split(“);var-newFileName=string.Format({0}{1}{2},areats[1],areats[3],.jpg”);File.Move(fileName,listOfFileNames+newFileName);}
但它在“var newFileName”行中生成了一些错误。(感谢用户65439占用您的时间。我尝试了以下方法:void按钮1click(objectsender,EventArgs e){var listOfFileNames=Directory.GetFiles(“C://Users//userPC//Desktop//MATT SUPERDRY//PROVA//IMG//AW12//”);foreach(listOfFileNames中的var fileName){var areas=fileName.Split('''u');var newFileName=string.Format({0}{1}{2},areas[1],areas[3],“.jpg”);File.Move(fileName,listOfFileNames+newFileName)}
但它在“var newFileName”行中生成了一些错误:(感谢nucandrei抽出时间。我尝试了您的代码,但调试器JIt收到了一条错误消息,上面写着“索引超出数组边界。继续。退出”。您知道为什么吗?文件夹中可能有一些文件没有3个分隔符。您应该在设置新名称之前检查它。尝试添加“if(ab.Lenght>3)”{string newName=..”trippino可能有一个很好的提示。此错误通常发生在访问数组不支持的索引时。因此,在本例中,您有一些文件不符合area1_area2_area3_area4.jpg格式。感谢您的提示。事实上,我看到一些文章只有3个区域。我已删除了它们,但已添加了您建议的控件但不幸的是,仍然生成了一个异常:“如果已经存在,则无法创建文件”?!?!关于最后一行中的参数:写入“System.IO.file.Move(newName,path);”不正确?此消息现在出现,因为重命名操作在多个文件上成功(在出现错误之前)感谢nucandrei抽出时间。我尝试了您的代码,但调试器JIt收到一条错误消息,上面写着“索引超出数组边界。继续。退出”。您知道为什么吗?文件夹中可能有一些文件没有3个分隔符。您应该在设置新名称之前检查它。尝试添加“if(ab.Lenght>3){string newName=..”trippino可能有一个很好的提示。此错误通常发生在访问数组不支持的索引时。因此,在本例中,您有一些文件不符合area1_area2_area3_area4.jpg格式。感谢您的提示。事实上,我看到一些文章只有3个区域。我已删除了它们,但已添加了您建议的控件但不幸的是,仍然生成了一个异常:“如果已经存在,则无法创建文件”?!?!关于最后一行中的参数:写入“System.IO.file.Move(newName,path);”不正确?此消息现在出现,因为重命名操作在多个文件上成功(在出现错误之前)感谢Patrik抽出时间。我尝试了你的代码,但得到了以下错误:“未处理的异常:无法创建文件”()感谢Patrik抽出时间。我尝试了你的代码,但得到了以下错误:“未处理的异常:无法创建文件”
using System.IO;
namespace ConsoleApplication
{
public class Program
{
public static void Main(string[] args)
{
// Rename all files in the C:\Temp\ directory.
Program.RenameFiles(new DirectoryInfo(@"C:\Temp\"));
}
public static void RenameFiles(DirectoryInfo path)
{
// Does the path exist?
if (path.Exists)
{
// Get all files in the directory.
FileInfo[] files = path.GetFiles("*.jpg");
foreach (FileInfo file in files)
{
// Split the filename
string[] parts = file.Name.Split('_');
// Concatinate the second and fourth part.
string newFilename = string.Concat(parts[1], "_", parts[3]);
// Combine the original path with the new filename.
string newPath = Path.Combine(path.FullName, newFilename);
// Move the file.
File.Move(file.FullName, newPath);
}
}
}
}
}