Windows Server TechCenter > Windows Server Forums > Windows PowerShell > script can't process directories starting with a space...
Ask a questionAsk a question
 

Answerscript can't process directories starting with a space...

  • Thursday, November 05, 2009 9:26 PMLLPAETEC Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code

    Hello all,
    I have a script which purges all files and then purges all empty folders from a location.  My problem is that a lot of the files and folders are auto generated from other systems, and sometimes it just so happens that a folder or file is created with a space at the begining of its name.

    The exact error is:

    Get-ChildItem : Could not find a part of the path

    Here is my code, would anyone be able to assist a work around for this error?

    $a = get-childitem "\\myfileshare\share1" -recurse -force
    $b = 450
    start-transcript share1Folder_Cleanup.log
    #delete files older than $a
    $a | `
        ? {$_.GetType() -match "FileInfo"} | `
        ?{ $_.LastWriteTime -lt [datetime]::now.adddays(-$b) }  | `
        rm -recurse -force -whatif
    #delete empty folders
    $a | `
         ? {$_.GetType() -match "DirectoryInfo"} | `
         ?{ $_.GetFiles().Count -eq 0 -and $_.GetDirectories().Count -eq 0 } | `
         rm -recurse -force -whatif
    stop-transcript
    Also, I do run into issues where a file path is simply too long to process.  Over 254 characters I believe.  Is there any way to intelligently design the script to account for this?  The script will still run regardless of all errors it encounters, i'm just hoping someone has some ideas on how to get around these two issues.

    Any help would be greatly appreciated!
    nothing

Answers

  • Monday, November 09, 2009 4:08 AMMervyn ZhangMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi,

    Since the server is not a Windows system, I’m afraid PowerShell cannot handle this situation. To workaround this problem, I suggest you create a Scheduled Task on Unix server to delete these folders.

    Or use the following tool to delete these folders.

    PuTTY
    http://www.chiark.greenend.org.uk/~sgtatham/putty/faq.html

    For your reference:
    How to execute a command on remote machine?
    http://www.linuxforums.org/forum/linux-networking/39218-how-execute-command-remote-machine.html

    If you have more questions, please also involve Unix support to check if they have any workaround.

    Thanks.


    This posting is provided "AS IS" with no warranties, and confers no rights.
  • Wednesday, November 18, 2009 2:10 PMLLPAETEC Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    I am all set.  I've given both our Unix and apps teams a link to the following document to make sure the files they create on the Windows system falls within these constraints.

    http://msdn.microsoft.com/en-us/library/aa365247(VS.85).aspx

    Thanks again!
    nothing

All Replies

  • Friday, November 06, 2009 1:10 AMMarco ShawMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I don't know if the space issue is related to using a network share or not.  I tried some local tests with PowerShell v2, and it seemed to work fine.

    As for deleting long file names, check this:
    http://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/f86be966-0019-4a01-834c-f47440f07f18
  • Friday, November 06, 2009 2:56 AMMervyn ZhangMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi,

    As far as I know, a file or folder cannot have a name which begins with a space.

    Please paste the result of the command below for research:

    get-childitem "\\myfileshare\share1" -recurse –force

    For Windows naming conversion, please refer to the article below:
    Naming Files, Paths, and Namespaces
    http://msdn.microsoft.com/en-us/library/aa365247(VS.85).aspx

    Thanks.

    This posting is provided "AS IS" with no warranties, and confers no rights.
  • Friday, November 06, 2009 2:04 PMLLPAETEC Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code

    Hi Mervyn, here is the output

    PS C:\Scripts> get-childitem "\\fileserver1\share1\Preview\C\cath" -recurse -force
    Get-ChildItem : Cannot find path '\\fileserver1\share1\Preview\C\cath' because it does not exist.
    At line:1 char:14
    + get-childitem  <<<< \\fileserver1\share1\Preview\C\cath' -recurse -force


    The subfolder in this share is called " St. Michael-111"

    Now what I should mention is that these files are NOT created/generated by a Windows user or system.  It's actually a Unix system via SFU & NFS depositing files on a Windows storage server.

    Are you aware of any way to get around this?  I actually don't know powershell very well, but is there a way to check for folders that are causing the above issue?


    - Marco,
    I see that I can use "$fso.deletefile" instead of "rm".  How can I alter this script to take advantage of that?


    nothing
  • Monday, November 09, 2009 4:08 AMMervyn ZhangMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi,

    Since the server is not a Windows system, I’m afraid PowerShell cannot handle this situation. To workaround this problem, I suggest you create a Scheduled Task on Unix server to delete these folders.

    Or use the following tool to delete these folders.

    PuTTY
    http://www.chiark.greenend.org.uk/~sgtatham/putty/faq.html

    For your reference:
    How to execute a command on remote machine?
    http://www.linuxforums.org/forum/linux-networking/39218-how-execute-command-remote-machine.html

    If you have more questions, please also involve Unix support to check if they have any workaround.

    Thanks.


    This posting is provided "AS IS" with no warranties, and confers no rights.
  • Monday, November 16, 2009 2:44 AMMervyn ZhangMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi,

    Do you need any other assistance? If there is anything we can do for you, please let us know.

    Thanks.

    This posting is provided "AS IS" with no warranties, and confers no rights.
  • Wednesday, November 18, 2009 2:10 PMLLPAETEC Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    I am all set.  I've given both our Unix and apps teams a link to the following document to make sure the files they create on the Windows system falls within these constraints.

    http://msdn.microsoft.com/en-us/library/aa365247(VS.85).aspx

    Thanks again!
    nothing
  • Thursday, November 19, 2009 1:28 AMMervyn ZhangMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi,

    Glad you have resolved the problem. If you have more questions in the future, you’re welcomed to this forum.

    Thanks.


    This posting is provided "AS IS" with no warranties, and confers no rights.