I have a lot of files and directories named similar to [Origin]Description[Techinfo].

Sometimes, brackets can be round brackets instead of square brackets, or spaces between the parts. How can I rename (ideally recursively) the files that I get something like

script or function [Origin]Description[Techinfo] ---> Description[Origin][Techinfo] ?

I can access the NAS via SSH, it runs FreeBSD (FreeNAS). Or I could do it via a client (Linux or Windows).

  • You have to individuate each single possibility to create a regex. The ones with spaces will be the most difficult because you cannot know in advance if a space belongs to the description or to the Techinfo. But it will be a secondary problem. Is the full list Are a full list of descriptions or of TechInfo available somewhere? – Hastur Feb 7 '15 at 11:42
  • I'm seeing bash in your future, perhaps awk, sed, perl... – Xen2050 Feb 7 '15 at 11:45
  • @Hastur: No, no full lists. We are talking about media files, with arbitrary naming. I am looking for something which identifies the three blocks, based on the brackets, and just swaps from ABC to BAC. – emk2203 Feb 7 '15 at 11:52

Windows, powershell V2, [Origin]Description[Techinfo] -> Description[Origin][Techinfo]:

powershell (ls -Re -Fo -Fi "*[*")^|%{$f=$_.Name -split('[\]\[ \)\(]');$r='\'+$f[2]+'['+$f[1]+']['+$f[3]+']';[IO.File]::Move($_.FullName,$_.DirectoryName+$r)}
  • I am a complete rookie with powershell, so I just copied and pasted it and ran it in a test directory. This is what I got: 'Exception at call of "Move" with 2 argument(s): "An empty filename is not allowed. Parameter name: sourceFileName" In line:1 character:109 + ... ']['+$f[3]+']';[IO.File]::Move($_.FullName,$_.DirectoryName+$r)} + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : ArgumentException' – emk2203 Feb 7 '15 at 20:14
  • @emk2203 run as command or script? – STTR Feb 7 '15 at 20:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.