locked
Powershell do no read content on folders with "[ ]" characters RRS feed

  • Question

  • Good morning.

    I´m trying to inspect files on folders to watch if files are older than X days. It works pretty nice with one exception, folders that contains in the name "[ ]" are skipped. I´m using the following:

    Get-ChildItem -Path $Data -Recurse -Force | foreach {
    		If ($_.GetType().Name -eq "FileInfo")
    			{
    			If ($_.LastWriteTime -lt $TestDate)

    $Data, is a variable that keeps the path and is variying in a loop.

    $TestDate is the X days limit date.

    As I told, it works fine wit folders with special characters in the name ($, %, &. _, etc), but if contains "[ ]" ("c:\Jimmy [Neutron] rules\" as example), the folder is skipped.

    Why could be happening this?, and the really important matter, Is there any way to "fix" it?

    Thank you.

    Friday, October 30, 2020 9:53 AM

Answers

  • Hello,

    You could escape this special char or use "LiteralPath" option:

    Get-Item '.\New folder `[test`]\' Directory: D:\ Mode LastWriteTime Length Name ---- ------------- ------ ---- d---- 30/10/2020 11:01 New folder [test]

    Get-Item -LiteralPath 'D:\New folder [test]\' Directory: D:\ Mode LastWriteTime Length Name ---- ------------- ------ ---- d---- 30/10/2020 11:01 New folder [test]


    That should work, or point to the right direction.

    • Proposed as answer by Vector BCO Friday, October 30, 2020 11:23 AM
    • Marked as answer by Paredero Friday, October 30, 2020 1:41 PM
    Friday, October 30, 2020 10:06 AM