-2

Bit of a speculative question, I have several files that need renaming each day. I'm wondering if it's possible to re-name them to the required spec automatically using a .bat or otherwise simple script.

An example filename would be: OKS-SABB MT940 MT940-OKS2015-11-26-09.38.18.502511.940

What it would need to be changed to is: OKS-SABB [Date -1 day].940

so: OKS-SABB 2015-11-25.940 if today is the 26th.

The file names all conform to that format, except they have different prefixes. e.g

OKA-SABB MT940 MT940-OKS2015-11-26-09.38.18.502511.940

OKB-SABB MT940 MT940-OKS2015-11-26-09.38.18.502511.940

OKS-SABB MT940 MT940-OKS2015-11-26-09.38.18.502511.940

The prefixes need to be maintained in the name change.

Now, is this something possible/ practicable? If yes, how would I go about performing it?

Thanks for the help,

J McNamara
  • 11
  • 2
  • So why don't you use a replace on the space and then wildcard and replace it with a `get-date` function? Do you know how to do that? – Nate Nov 26 '15 at 10:41
  • No, I'm not a coder by trade, just know enough bits and pieces to get by. How would I go about doing the replace -> get-date? As an aside, I haven't ever used PowerShell, but it seemed applicable to finding a solution to my query. – J McNamara Nov 26 '15 at 10:58
  • Okay, is `.940` the file extension? Or is it needed to be there for some reason? – Nate Nov 26 '15 at 11:04
  • Sometimes it takes longer to write the question than to use a search engine: http://blogs.technet.com/b/heyscriptingguy/archive/2015/01/21/adding-and-subtracting-dates-with-powershell.aspx – cfi Nov 26 '15 at 11:42
  • On unix I'd [use bash](http://stackoverflow.com/questions/18180581/subtract-days-from-a-date-in-bash) – cfi Nov 26 '15 at 11:43
  • 1
    @JMcNamara Unless you have some special platform restrictions, this kind of need better finished with language like Python/Perl/Ruby/etc., so that it will works on almost every Operating System platform (GNU/Linux, OS X, Linux, BSD, etc.), you won't have to rewrite it if you move to a different platform. And it will be better if you re-edit your question make it more clear, so others won't close it. – Albert Nov 26 '15 at 12:37

1 Answers1

3

this should do the work. Normally, we don't just write code for people who don't try anything by themselves. But I have a good day, so why not.

$Path = "C:\Install\test"
$Date = ((Get-Date).AddDays("-1").ToString('yyyy-MM-dd'))
$Files = gci $Path -Filter *.940 | ForEach {
    $Prefix = $_.Name.Split('-')[0]
    Rename-Item -Path $_.FullName -NewName "$Prefix-SABB-$Date.940"
}

Just copy this into a text-file and save it as RenameFiles.ps1 and make a scheduled task out of it or whatever you want.

Change the Value of the $Path Variable to your Path.

SimonS
  • 1,891
  • 1
  • 29
  • 53
  • 1
    Thanks Simon. This worked a treat. I've not used powershell before, so it's been quite the learning experience. I appreciate your patience, as this would have been way beyond my knowledge. Thanks again. – J McNamara Nov 26 '15 at 12:02
  • 1
    @JMcNamara you're welcome! just go on and learn powershell. i started about 3 months ago, and didn't have any scripting experience at all. It's easy to learn, and very useful. Another thing: If you want to make a scheduled task out of this script, just refer to my answer in this thread: http://serverfault.com/questions/727590/scheduled-task-for-powershell-script/727860#727860 – SimonS Nov 26 '15 at 12:22