2017-05-05 63 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 
} 
相关问题