
Dustin Sultenfuss (Customer) asked a question.
need to rename a file with the date embedded to previous days date
is there a way to download a file for todays date (ToysRUS[MM][DD]) with the date embedded in the file name (no breaks) and rename it to the previous days date, we have about 15 files with different character counts per day and we need to rename all those files to the previous date. example ToysRUS1230 needs to be renamed ToysRUS1229 and ToysRUsToo1230 to ToysRUsToo1229. i cant figure ot how to rename the downloaded file and change the date with different named files with the same dates
Hi Dustin,
Because your file names are variable length, you would need to write a custom script to do this. And, if I am reading your use case correctly, it could be that even if the filenames were more regular in length you still might need a custom script, because of the nature of what you mean by 'yesterday.'
TO use a custom script, the basic idea would be this:
You'd have a traditional task with the source and destination as normal.
You'd add a process to it that ran for every file and add some PowerShell to that process.
The script could be simple or complex, depending on your scheme.
For instance, if all you wanted to do was append the words "blah" to the filename, you'd only need a few lines like the following:
But, if it were that simple, you actually wouldn't even need a script. you can do that with the regular UI in the destination settings by changing the destination filename. I only give that example to show the MISetFileName function as the important item.
BUT, in your case, you need to dynamically parse the name and create a new name based on that parsing on the fly. SO, the pseudo code would be something like the following:
The devil will be in the details of some 2 new functions you need to create to 1) get the current digits, and 2) change them to what you want (decrementing by one day). Of course, you need to consider what happens when you are on the first of the month and decrement. The month needs to move back one as well. So, that might imply that you need to take the digits you are given, and turn them into a proper powershell date, and then use powershell to decrement the date, and then turn them back into simple digits. I realize, this gets thorny pretty quick.
The regular UI can do most of this stuff natively, but, it can't easily deal with the variable nature of your filenames. If all we needed to do was lop off 4 digits at the end of a filename, and those 4 digits were always in the same place, that's easy in the main UI. And, then we can tack on yesterday's date digits. But, you have the variable names, and also, one more detail: I'm guessing you don't always want yesterday's digits. I'm guessing that if a file comes in with 31, standing for December 31, but you happen to process it on the Jan 4, you still want the new name to have a '30' in it (for December 30), and not a '03' (for Jan 03). Our built-in methods can do date math, but they do it based on today's date as the starting point. We'd always want to give you a day back from today, not a day back from whatever that file's digits represented. SO, I really think you've got to drop into some Powershell.
I hope this helps.
Since there is no rhyme or reason to the left side, I cant use [Left([OrigName], 16]
Is there a secret code to keep all the characters to the left of 8 characters?
Dustin Sultenfuss
Server Admin II | HQ
Greater Texas | Aggieland Credit Union
12544 Riata Vista Circle
Austin, TX 78727
Dustin.sultenfuss@gtfcu.org<mailto:Dustin.sultenfuss@gtfcu.org>
I can ponder a little bit more, but let me ask a couple of questions before I make another recommendation:
let me know your thoughts, and I'll try to give some more advice.
jim
1. Yes, it will always be 4 digit, ex Jan= 0104, Feb= 0204 and so on
2. Yes, Extension will always be 4 digits, plus the date equals 8 digits. Ex 0105.txt all though the original file is all caps and they ask for lower case
3. 1. Yes we need Date format which MoveIT handles very well with the [MM][DD] and [MM][DD-], it runs daily so it will always be the date minus 1
2. Yes,. With the new year we need for 1/1 to revert to 12/31 but that is a ways away so not as urgent at this point
Dustin Sultenfuss
Server Admin II | HQ
Greater Texas | Aggieland Credit Union
12544 Riata Vista Circle
Austin, TX 78727
Dustin.sultenfuss@gtfcu.org<mailto:Dustin.sultenfuss@gtfcu.org>
Dustin,
OK, with that, then, essentially the most difficult thing here is that we have to 'trim' the filename, even though it is of variable length. Suprisingly, we can't handle that with Macros currently in the product (it's a known missing feature), so you must, at least for part of this problem, use PowerShell.
I want to be clear, with this proposal, I am not taking the name as given in the original file and subtracting a day based on the digit in the filename. I am removing the date information completely from the original name, and then adding digits back in for yesterday's date. Might seem subtle, but it's an important distinction. If you actually want to use the digits as given in the original file name, and then come up with new digits based on those, then that is a bit harder (but very possible).
What I've tested out is this:
I've added some screenshots to make it clearer.
Also, here's the script text, since you can't cut-and-paste from my screenshot:
This works across months and years. If it's run on Feb 1, for example, the file will be saved with its last 4 digits as 0131.
Screenshots
Dustin Sultenfuss
Server Admin II | HQ
Greater Texas | Aggieland Credit Union
12544 Riata Vista Circle
Austin, TX 78727
Dustin.sultenfuss@gtfcu.org<mailto:Dustin.sultenfuss@gtfcu.org>
OK, let me know. If it doesn't work, it's probably something simple.
One thing that occurred to me: I had created the Task parameter "ShortName" in the task with blank content "" and saved it with the defintion before I even started mucking with a script.. Meaning, the task parameter at least exists before the script runs. I do not believe this is necessary. I believe that when the MISetTaskParam runs, it will gladly create a task param even if it doesn't already exist. BUT, if you have a problem with the param, try creating a dummy parameter in the task first and make sure it is saved with the task definition.