1

We currently have a CIFS share hosted from a SAN device and are planning to move toward a case management system. The issue I'm having is a lot of the various files in our file share end with a trailing dot (.) and fail to upload.

I've been trying to get a script working that would keep file names the same but batch remove any trailing dots. I've been able to do this with variouse special characters using powershell.

We also have many sub folders that have a dot (.) in the middle of the folder name and would need to leave these alone.

Example

Original File name: 20151101 Luis K. Abrishamian M.D..pdf

New file name: 20151101 Luis K. Abrishamian M.D.pdf

I used this one to remove all & from file names

Get-ChildItem -recurse -name | ForEach-Object { Move-Item $_ $_.replace("&", "and") }

I tried this one but it failed to do what i needed

Get-ChildItem -recurse -name | ForEach-Object { Move-Item $_ $_.replace("*.", "") }
  • Try escaping. e.g., if every character was renamed to a couple of hex digits, that could work, if filename lengths aren't exceeded. Although, there may be more efficient ways that involve only escaping what you need to. Make sure any escape code is suitable unique from any valid text (or that the escape code gets properly escaped). Not sure offhand about PowerShell (and low on time), so I'll just mention this to help point you (if needed)... : Escaping is often used in other contexts, like in URLs (maybe programming, e.g. JavaScript's escape() function.) – TOOGAM Mar 11 '16 at 18:05
0

Try the following:

Get-ChildItem -recurse -name |  ForEach-Object { If($_.Substring($_.Length-1) -eq ".") { Move-Item $_ $_.Substring(0,$_.Length-1) } }

This will get each item, check if the last character is a ., and if it is it'll move it to the same name without the last character.

  • No dice. File names remained the same – AimdSoldier Mar 11 '16 at 17:14
  • @AimdSoldier Any error message? – Jonno Mar 11 '16 at 17:27
  • No It didn't give me any errors. Used out-file at the end and there was nothing in file – AimdSoldier Mar 11 '16 at 17:35
0

You could try

Get-ChildItem -recurse -name | ForEach-Object { Move-Item $_  ([regex]::Replace($_, "\.$" , "")) }
  • No luck. Trailing dots still there on my test files – AimdSoldier Mar 11 '16 at 18:00
0

Came from an anonymous person at powershell.org

# Replace '..' from names (remove one dot)
Get-ChildItem -Recurse -Path .\my\path | Rename-Item -NewName {$_.name -replace '\.\.','.'} -WhatIf
0

No one-liners for me.

foreach ($file in (Get-ChildItem 'C:\test' -Recurse) ){
  $newName = $file.DirectoryName + '\' + ( $file.Basename.replace('.', '').replace('&', 'and') ) + $file.Extension
  move-item $file.FullName $newName 
}

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.