Community forum

Please note that VisualCron support is not actively monitoring this community forum. Please use our contact page for contacting the VisualCron support directly.


ThomasDoss
2012-01-05T18:52:05Z
Hello,

I've been butting my head against this for awhile, and have finally caved in and am asking for assistance. I need to rename a file in a rather specific way. I need to take a file named

'DATE.COMPANY.SERVER.FILECONTENTS.EXT'

and make it

COMPANY.SERVER.FILECONTENTS.EXT

Please note the single quotes that need to be removed from both ends. I've come at this a dozen different ways and have yet to get it to work without making one rename command per file and specifying the output file's exact name. Any help would be appreciated.
Sponsor
Forum information
Support
2012-01-05T19:13:04Z
Hi Thomas,

while I am no Regular expressions expert this seems like something you should use regex for. There are a lot of forums for regex where you can post this. Then you can use the Regex function in the string category.
Henrik
Support
http://www.visualcron.com 
Please like  VisualCron on facebook!
ErikC
2012-01-06T09:20:06Z
You can try the .NET code execute task for this.

Provide the full path to the method of the old file and it will rename is according to your needs.

Regards,
Erik

using System;
using System.Text.RegularExpressions;
using System.IO;

public class Test
{
	public static void RenameFile(string oldFilePath)
	{
		if(File.Exists(oldFilePath))
		{
			//split the oldFilePath in path and filename
			string Directory = Path.GetDirectoryName(oldFilePath);
			string oldFileName = Path.GetFileName(oldFilePath);

			//The two patterns using groups
			//The new pattern dismisses the 1st part and the quotes
			string regOldFileName = (@"^'(.+)\.(.+)\.(.+)\.(.+)\.(.+)'$");
			string regNewFileName = @"$2.$3.$4.$5";

			//Make a regular expression object
			Regex newNameRegex = new Regex(regOldFileName);

			//Create the name for the new File
			//If there is no match with the regOldFileName, the newFileName = olfFileName
			string newFileName = newNameRegex.Replace(oldFileName, regNewFileName);			

			//The rename of the actual file using full the path
			string newFilePath = Path.Combine(Directory, newFileName);
			File.Move(oldFilePath, newFilePath);
		}
 	}
}

Uses Visualcron since 2006.
ThomasDoss
2012-01-06T15:03:35Z
I got it working. I should have mentioned that I need to only grab one days data out of the folder at a time and then later need to put the same date (which is often a past date but not in a specific pattern) back on the file. What I did was this:

1. List contents of Upload directory
2. save a var as the first date stamp
3. save a var as the character position of the first appearance of the single quote (ie 29)
4. save a var as the character position of the first carriage return (ie 68)
5. Copy (ie move) files to Processing directory with the following mask

['A-Z 0-9]*.{STRING(Substring|{TASK(67b46d6c-4aac-4eac-9a04-27b912c4e572,StdOut)}|{USERVAR(Var1)}|{MATH(Subtract|Integer|{USERVAR(Var2)}|{USERVAR(Var1)}|#0)})}[']?

Using the list task output as the input for the substring call. This grabs the first file name in the folder (without its date stamp). The file is then saved with the name 123 and post processed with this mask

{STRING(Replace|{NEWNAME()}|123|{STRING(Substring|{TASK(67b46d6c-4aac-4eac-9a04-27b912c4e572,StdOut)}|{USERVAR(Var1)}|{MATH(Subtract|Integer|{USERVAR(Var2)}|{USERVAR(Var1)}|#0)})})}

Which places its name without a date stamp back on it. There is a condition in place on the next task that check to see if all files with the date from step 2 have been processed out yet.

6. this step runs the processing on the files
7. Rename to add the date stamp back from the var
8. move the files to an archive directory

Steps 3 and 4 were problematic as the {STRING(IndexOf||)} command outputs a string even though it appears to be an integer and I needed to do math on it. I had to create two variables for each. the first actually checks the index and gets the number (in string format) the second is a dummy placeholder. The task then sets the dummy (with type int32) to the value of the fist (the string). If the constant box is checked, then this works, if not it sets it to the text from the first variable command itself.

There is probably more for me to do to get this perfect, and there were probably a dozen ways to do it easier, but this does work. Thank you both for your effort.
Scroll to Top