0

My Powershell Script checks if a file exits. When it exist, the existing file is deleted. Then a new file with same name is saved. It works perfect when no dots (".") are in my filename (and path). I thought -LiteralPath would be the solution, but it doesn't work, don't know why. Test-Path seems to work, but Remove-Item doesn't work.

 $xlsFile = "R:\Temp\37 Place i.Bay.\ComeOn 37 Place i.Bay. user_finance.xlsx"

 if (Test-Path -LiteralPath $xlsFile)
        {
            try {
                Write-Host "TestPath positiv, try to delete"
                Remove-Item -LiteralPath $xlsFile -ErrorAction "Stop"
            } catch {
                Write-Warning $_.Exception.Message
            }

            try {
                $xlsObj.ActiveWorkbook.SaveAs($xlsFile);
            } catch {
                Write-Warning $_.Exception.Message
            }

        }
        else
        {
            Write-Host "TestPath negativ"
            $xlsObj.ActiveWorkbook.SaveAs($xlsFile);
        };

Powershell 2.0, R: is a network path.

Thx

dex
  • 3
  • 2
  • I haven't found docs supporting it (and I may not have time to) but I suspect the issue is trying to have a folder name with the final character being a dot. – EBGreen Feb 26 '15 at 16:08
  • Windows GUI will not let you make a folder with a trailing period so you might be onto something – Matt Feb 26 '15 at 16:13
  • found it. "Do not end a file or directory name with a space or a period. Although the underlying file system may support such names, the Windows shell and user interface does not. However, it is acceptable to specify a period as the first character of a name. For example, ".temp"." - https://msdn.microsoft.com/en-us/library/aa365247.aspx – EBGreen Feb 26 '15 at 16:17

1 Answers1

0

Ending a folder name with a dot is not allowed in windows:

Do not end a file or directory name with a space or a period. Although the underlying file system may support such names, the Windows shell and user interface does not. However, it is acceptable to specify a period as the first character of a name. For example, ".temp".

https://msdn.microsoft.com/en-us/library/aa365247.aspx

Matt
  • 45,022
  • 8
  • 78
  • 119
EBGreen
  • 36,735
  • 12
  • 65
  • 85
  • This answer can help delete objects with trailing periods. Also links to a M$ article about this topic. http://stackoverflow.com/a/4123152/3829407 – Matt Feb 26 '15 at 16:31
  • I guess I should have been more clear. I think that the problem is the folder in the path named "37 Place i.Bay.". Not the file name itself. – EBGreen Feb 26 '15 at 16:32
  • Sorry.. that was my fault I was just agreeing with you and offering a solution on how to _potentially_ make it `Remove-Item` work. Cant test since I cant simulate the problem – Matt Feb 26 '15 at 16:33
  • Yeah, I can't recreate the scenario either. I suspect that you could with a linux VM to create the folder in the first place, but I can't devote the time to come up with one right now. – EBGreen Feb 26 '15 at 16:36
  • I am creating folder and filenames from an SQL request. The problem has alway occurred when I updated the data. Now I'm using $myFoldername.TrimEnd("."), to make sure It doesn't happen anymore. – dex Feb 26 '15 at 16:52