2017-05-05 62 views
0

我已經搜索了數據庫中的答案並找到類似的東西,但不是我所需要的東西。我不知道如何編寫足夠好的代碼來修改它們以滿足我的需求。使用Powershell將文檔移動到基於文件名的文件夾

我有文檔需要移動到與文檔名稱的開頭名稱相同的文件夾。這裏有幾個例子我的文檔名(ParcelNum_CountyNum_Year_DocType.pdf)的

  • 188777_014_2013_NOAV.pdf
  • 242353_227_2014_HRGNOTICE.pdf
  • R506275_246_2013_NOAV.pdf

我現在有居住在一個文件由縣編號命名的單個文件夾(第一個_和第二個_之間的3位數字)

我有創建的文件夾已命名通過ParcelNum,這是第一組字符/數字在開頭和第一個_之前。我需要Powershell讀取停止在第一個_處的文檔名稱的第一部分,並將其放在與numchar匹配的文件夾中。 因此,例如上面的第一個文檔示例被命名爲188777_014_2013_NOAV.pdf,並位於名爲014的CountyNum文件夾的根目錄。 對於該示例,我需要將它從根文件夾014移到其名爲188777的子文件夾中。

我希望這是有道理的。請隨時提問。

謝謝

+1

我們希望看到您已編碼到目前爲止。 –

+0

我不知道如何編寫足夠的代碼來做到這一點,而我現在只是想學習Powershell。我甚至不知道從哪裏開始。 – Byegone

回答

1

試圖解決您的問題:在PS1文件之上

#Requires -Version 4.0 
 

 
function Move-FileToDirectoryWithSamePrefix 
 
{ 
 
    [CmdletBinding()] 
 
    Param(
 
     [Parameter(Mandatory = $true, Position = 0)] 
 
     [ValidateScript({Test-Path $_ -PathType Container})] 
 
     [string] 
 
     $DirectoryToMoveFileTo, 
 

 
     
 
     [Parameter(Mandatory = $true, Position = 1)] 
 
     [ValidateScript({Test-Path $_ -PathType Container})] 
 
     [string] 
 
     $DirectoryToSearchFiles 
 
     ) 
 

 
     Begin { 
 
      # Needed to find all files for movement 
 
      $fileRegex = ".*_[0-9]{3}_[0-9]{4}_.*\.pdf" 
 
      $currentLocation = Get-Location 
 
     } 
 
     Process{ 
 

 
      # find files to move 
 
      $matchingFile = Get-ChildItem -Path $DirectoryToSearchFiles -Recurse | Where-Object { $_.Name -match $fileRegex } 
 

 
      # Change to destination directory 
 
      Set-Location $DirectoryToMoveFileTo 
 

 
      foreach ($file in $matchingFile) { 
 
       $directoryName = ($file -split "_")[0] 
 

 
       # Find the director to move to 
 
       $dirToMoveFile = Get-ChildItem -Recurse -Include $directoryName 
 

 
       if ($dirToMoveFile -eq $null) { 
 
        # Create directory if not existing 
 
        $dirToMoveFile = New-Item -Name $directoryName -Path $DirectoryToMoveFileTo -ItemType Directory 
 
       } 
 

 
       Move-Item -Path $file.FullName -Destination $dirToMoveFile.fullName 
 
      } 
 
     } 
 
     End{ 
 
      Set-Location $currentLocation 
 
     } 
 
}

商店,例如moveFile.ps1。然後打開PowerShell和源文件:

PS C:\> . C:\PathToYourPs1File\moveFile.ps1 

之後,您可以像功能:

PS C:\temp> Move-FileToDirectoryWithSamePrefix -DirectoryToMoveFileTo .\filesDestination -DirectoryToSearchFiles .\files 

如果您需要調試它打開PowerShell的ISE的PS1,設置一個斷點,然後啓動腳本。欲瞭解更多信息,請參閱link

希望有所幫助。

2

這是我會做的。這是假設您要移動的文件位於父文件夾中,並且您要對其進行排序的子文件夾也位於同一文件夾中,即如果示例PDF的路徑爲C:\test\Docs\188777_014_2013_NOAV.pdf且您希望它結束在C:\test\Docs\014\188777

根據自己的喜好更新$ basePath變量中的路徑。每個步驟的註釋都包含解釋。請享用!

# This is the parent directory for the files and sorted folders 
$basePath = "C:\test\Docs" 

# This sets that folder as your CWD (Current working directory) 
Set-Location $basePath 

# This grabs the *files* underneath the parent directory, ignoring sub directories 
$files = Get-ChildItem $basePath | Where-Object {$_.PSIsContainer -eq $false} 

# Starts iterating through each file 
foreach ($file in $files) { 
    # Split the base file name by underscore (creates an array with each part of the name seperated) 
    $split = $file.BaseName -split "_" 
    # Store the second item [1] in the array $split as the root folder name 
    $root = "$basePath\$($split[1])" 
    # Store the first item [0] in the array $split as the sub folder name 
    $sub = "$root\$($split[0])" 

    # Check if the root folder exists, create it if not 
    if (!(Test-Path $root -ErrorAction SilentlyContinue)) { 
     New-Item $root -ItemType Directory | Out-Null 
    } 

    # Check if the sub folder exists, create it if not 
    if (!(Test-Path $sub -ErrorAction SilentlyContinue)) { 
     New-Item $sub -ItemType Directory | Out-Null 
    } 

    # Move the file to the sub folder 
    Move-Item $file.FullName -Destination $sub -Verbose 
} 
相關問題