March 7, 2012 at 1:20 am
Hi All
I know its possible but my C#.Net or VB.Net skills are none.
What I want to do is rename all files in a directory removing the timestamp which is put on the end of the file.
The purpose is to automate a copy of our backups from the 3rd party hosting environments to our ftp site, then either download and rename or rename then download, then restore to our support environment.
The copy and the download I have got working, its just renaming the files removing for the time stamp.
So for example we have a DB called aspnet_gb which generates a backup file called aspnet_gb_FULL_201203070816.bak, all I want to do is remove the _201203070816 and leave the file as aspnet_gb_FULL.bak or even aspnet_gb_FULL_.bak so that I can build a constant filename into an automated restore script for ease of use.
Everything I've looked at so far is either renaming the whole file something else, or adding a string to the end of the file name, but not removing a string from the file.
I've got the below code to get the list of files in a directory
public void Main()
{
DirectoryInfo directoryInfo = new DirectoryInfo(@"D:\DestinationFolder");
//if the director exists then proceed
if (directoryInfo.Exists)
{
var fileList = directoryInfo.GetFiles();
foreach (FileInfo fleInfo in fileList)
{
var newFileName = GetNewFileName(fleInfo);
//copies the new file names
fleInfo.CopyTo(newFileName, true);
//delete the old/original files
fleInfo.Delete();
}
Dts.TaskResult = (int)ScriptResults.Success;
}
else
{
MessageBox.Show("Directory not found", "Invalid directory",
MessageBoxButtons.OK, MessageBoxIcon.Information);
Dts.TaskResult = (int)ScriptResults.Failure;
}
}
And this code to add a string to the end
private static string GetNewFileName(FileInfo fleInfo)
{
var shortDate = DateTime.Now.ToShortDateString().Replace("/", string.Empty);
var timeInMilliSec = DateTime.Now.Millisecond.ToString();
var format = string.Format("{0}_{1}", shortDate, timeInMilliSec);
var extension = ".txt";
return Path.Combine(fleInfo.DirectoryName,
string.Concat(fleInfo.Name.Split('.')[0], "_", format, extension));
}
Can anyone help modify the above or point me in the right direction?
Thanks in advance
March 7, 2012 at 2:40 am
This was removed by the editor as SPAM
March 7, 2012 at 2:43 am
current flow is
file system task to delete all files on the FTP server
for each loop with a file system task to copy all BAK files from the instance's backup drive to the FTP folder
so I have the filename in a variable in the copy task, so this could be passed in as the source for the rename, just unsure on how I would set the new filename variable as its going to be a variable length string and the timestamp is not the same for each file.
i've modified the C# script to what I think will work and tested with a couple of the small backups, but its doing a copy of the file and renaming so taking time on that, changed it to a move instead but just copying the files back to the ftp folder at the moment to see if its any speedier.
if you could help with the variable aspect as well, then that might be another option I could investigate
March 7, 2012 at 3:07 am
this is working as I need it, will also invesitgate the variable route as well
public void Main()
{
DirectoryInfo directoryInfo = new DirectoryInfo(@"\\server\share");
//if the director exists then proceed
if (directoryInfo.Exists)
{
var fileList = directoryInfo.GetFiles();
foreach (FileInfo fleInfo in fileList)
{
var newFileName = GetNewFileName(fleInfo);
//copies the new file names
fleInfo.MoveTo(newFileName);
//delete the old/original files
//fleInfo.Delete();
}
Dts.TaskResult = (int)ScriptResults.Success;
}
else
{
MessageBox.Show("Directory not found", "Invalid directory",
MessageBoxButtons.OK, MessageBoxIcon.Information);
Dts.TaskResult = (int)ScriptResults.Failure;
}
}
private static string GetNewFileName(FileInfo fleInfo)
{
var length1 = fleInfo.Name.Length;
var length2 = length1 - 19;
var filename = fleInfo.Name.Substring(0, length2);
var extension = ".bak";
return Path.Combine(fleInfo.DirectoryName, string.Concat(filename, extension));
}
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply