폴더의 일부 파일 이름 바꾸기
C #을 사용하여 폴더에서 일부 파일의 이름을 변경 (즉, 각 이름에 동적으로 ID 추가)하는 작업이 있습니다.
예 : help.txt에서 1help.txt로
어떻게 할 수 있습니까?
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));
}
}
내 목적을 위해이 코드를 작성해야했기 때문에 여기에 그냥 덤프하겠습니다.
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);
}
}
찾고있는 기능 File.Move(source, destination)
은 System.IO
네임 스페이스입니다. 또한 DirectoryInfo
폴더의 내용에 액세스하려면 같은 네임 스페이스 의 클래스를 살펴보십시오 .
C #에서 파일 이름을 어떻게 바꿀 수 있습니까?를 확인하십시오 . . C # 에 이름이 변경되지 않았는지 몰랐습니다 ...System.IO.File.Move(oldFileName, newFileName)
다음 과 같이 File.Move 를 사용할 수 있습니다 .
string oldFilePath = Path.Combine( Server.MapPath("~/uploads"), "oldFileName");
string newFilePath = Path.Combine( Server.MapPath("~/uploads"), "newFileName");
File.Move(oldFilePath, newFilePath);
On .NET Framework 4.0 I use FileInfo.MoveTo()
method that only takes 1 argument
Just to move files my method looks like this
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);
}
}
to rename files my method looks like this
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);
}
}
AS you can see to Rename file it syntax is almost the same as to Move it, just need to modify the filename
before using MoveTo()
method.
ReferenceURL : https://stackoverflow.com/questions/680786/rename-some-files-in-a-folder
'programing' 카테고리의 다른 글
ggplot2에서 글꼴 변경 (0) | 2021.01.14 |
---|---|
GWT : GET 요청에서 URL 매개 변수 캡처 (0) | 2021.01.14 |
단위 테스트를위한 테스트 데이터 파일 경로 (0) | 2021.01.14 |
비디오에서 물체와의 거리를 어떻게 확인할 수 있습니까? (0) | 2021.01.14 |
C에서 16 진수 문자열을 바이트 배열로 (0) | 2021.01.14 |