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

 

 


  • Jim Cashman (Progress Software)

    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:

    1. $OriginalBaseFileName = $miaclient.MIMacro('[OnlyName]')
    2. $OriginalExtension = $miaclient.MIMacro('[OnlyExt]')
    3. $miaclient.MISetFilename($OriginalBaseFilename + "blah" + "." + $OriginalExtension)

    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:

    1. $OriginalBaseFileName = $miaclient.MIMacro('[OnlyName]')
    2. $OriginalExtension = $miaclient.MIMacro('[OnlyExt]')
    3. # get the date digits in the filename for further processing
    4. $date_digits_from_filename = GetDateDigits($OriginalBaseFileName)
    5. # take those digits and create new digits based on whatever logic makes sense (decrement by one)
    6. $new_date_digits = CreateNewDigits($date_digits_from_filename)
    7. # create a new filename based on the old one,
    8. # where it has stripped the old digits, and added in the new digits.
    9. $NewBaseFilename = $OriginalBaseFileName.substring(0,$OriginalBaseFileName - 4)
    10.  
    11. $miaclient.MISetFilename($NewBaseFilename + $new_date_digits + "." + $OriginalExtension)

    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.

     

     

     

     

    • That does help, but I can not seem to find how to keep the original file name and remove the last 8 digits (0105.txt). I have tried [RIGHT([ORIGNAME],8)] bit it only returns the 0105.txt

      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>
  • Jim Cashman (Progress Software)

    I can ponder a little bit more, but let me ask a couple of questions before I make another recommendation:

    1. is it always 4 digits? So, we can count on there being a '0' when it's a single digit month like January? ('01'). Given your example, I guess the answer is yes.
    2. Do you always expect the extension, so, as you indicate in your most recent post, it's always the last 8 characters we want to eliminate/change? (though, I suppose it doesn't matter to you if we did it the way I showed where i seperate out the extension and then bring it back in in the end.)
    3. Can you give me your thoughts on my comments about the following 2 date related things:
      1. Am I right that you need the new digits to one less than the current digits? It may be that in the normal course of events, that always means yesterday, but, to be totally accurate, it's the day before whatever digit is in the original file name. So, if the filename is has 0104, it should always become 0103, even if we process the file next week, when the day before would be something else.
      2. Am I right that during the turn over to a new year or month, we've got to be careful. If something comes in on Jan 1, for example, you want 0101 -> 1231.

     

    let me know your thoughts, and I'll try to give some more advice.

     

    jim

    • Thank you for your fast response. Please see answers below:


      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>
  • Jim Cashman (Progress Software)

    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:

    1. We create a 'trim from base' script, and include it as a process in your task.
      1. all it does is take the current basefilename (that's the filename without the extension), and lops off the last 4 characters.
      2. It then saves that new name in a task parameter called "Shortname"
    2. Back in your destination definition, we use normal MOVEit macros to create the new filename, using the task parameter that the script generated, plus macros for month and day, plus the extension, and save the file
      1. the macro definition for the new name: [parm:ShortName][MM][DD-] .[OnlyExt]

     

    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:

    1. #Trim From Base PowerShell Script
    2. $miaclient.MILogMsg("******** Creating Trimmed Name Parameter")
    3. $OriginalBaseFileName = $miaclient.MIMacro('[OnlyName]')
    4. $miaclient.MISetTaskParam("ShortName",$OriginalBaseFileName.substring(0,$OriginalBaseFilename.length - 4))

    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.

     

  • Jim Cashman (Progress Software)

    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.