I have files with names like this:
414_gtmlk_videos_Mas_147852_hty1147.xls
414_gtmlk_videos_Mas_P147852_hty1147.txt
I want to creat a job to check the filenames and take the part after Mas
in the file name (147852-P147852)
and create a folders with this part name (the folder name should be: 147852-P147852).
And finally move each file to his folder.
I have some C# code below. The first part does the following:
Modify full paths to get "147852" part, between __Mas_ and last _
string pathToGetFile = @"C:\\";
string[] filePaths = System.IO.Directory.GetFiles(pathToGetFile +@"\\", "*_Mas_*");
string[] fullName = new string[filePaths.Length];
for (int i = 0; i < filePaths.Length; i++)
{
fullName[i] = filePaths[i].Substring(filePaths[i].LastIndexOf("\\") + 1);
filePaths[i] = filePaths[i].Substring(filePaths[i].LastIndexOf("_Mas_") + 5);
int l = filePaths[i].IndexOf("_");
filePaths[i] = filePaths[i].Substring(0, l);
Now you can create folders with yours names filePaths is now like that: 147852, P147852
if (!Directory.Exists(@"C:\" + filePaths[i]))
System.IO.Directory.CreateDirectory(@"C:\" + filePaths[i]);
}
Now just move files to new directories
for (int i = 0; i < filePaths.Length; i++)
{
string sourceFile = System.IO.Path.Combine(pathToGetFile, fullName[i]);
string destFile = System.IO.Path.Combine(@"C:\" + filePaths[i], @"C:\" + filePaths[i] + "\\" + fullName[i]);
File.Copy(sourceFile,destFile,true);
}
Now, what happens
Files:
They will be copied according to the: