PowerShell script to create directory with long folder names RRS feed

  • Question

  • Hi All,

    I am trying to create a directory having folders and sub-folders from URL path. I could able to achieve it but there are issues if folder name is long.
    Considering the below path
    ABC/IT/DEF/LOGABLMESTRIC/Global Supply Chain/VOTC Weekly Reports - By Supplier, by region/WEEK24/DINGHUA-MEAD- WEEKLY VENDOR REPORT.XLS 

    By using PowerShell script, it could able to create folders till ' - By Supplier' and ignoring the remaining part till 'WEEK24'. 

    Please check the script below:


    Add-PSSnapin Microsoft.SharePoint.PowerShell

    $Files = Import-Csv "C:\Microsoft PowerShell\GDWFilesTask\GDW Files task\LessFiles.txt"

    $TargetPath = "E:\GDW Files"

    $wc = New-Object System.Net.WebClient
    $wc.UseDefaultCredentials = $true

    ForEach ($File in $Files){ 

      $FPath = $($File.Path)

      $String = $FPath.ToString()

      [System.Collections.ArrayList]$ArrayList = $String.split("/")
      $ArrayList[0] = $TargetPath;
      $ArrayList[2] = "ABC"
      $ArrayList = $ArrayList -notlike "*.*"

      $b = ($ArrayList -join "\")

      New-Item -ItemType directory -Path $b  

      $FileName = Split-Path $FPath -leaf  

      $output = $b +"\"+ $FileName

      $wc.DownloadFile($FPath, $output)

      write-host $FileName " is downloaded." -ForegroundColor Yellow


    Is there any way, we can create complete directory with long folder and sub-folder names?
    Please respond.



    Tuesday, November 20, 2018 12:09 PM

All replies

  • Please shorten the full URL, simplify folder structure or shorten file name.

    Justin Liu Office Servers and Services MVP, MCSE
    Senior Software Engineer
    Please Vote and Mark as Answer if it helps you.

    Wednesday, November 21, 2018 5:24 AM