简体   繁体   English

使用Powershell根据文件名将文档移动到文件夹

[英]Using Powershell to move documents to a folder based on file name

I have searched your database for answers and find things that are similar, but not quite what I am needing. 我已经在您的数据库中搜索了答案,并找到了类似但不是我所需要的东西。 I do not know how to code well enough to take some of the examples and modify them to fit my needs. 我不知道如何编写足够好的代码以采用一些示例并对其进行修改以满足我的需求。

I have documents that I need to move to a folder that has the same name as the beginning of the document name. 我有一些文件需要移到与文件名开头相同名称的文件夹中。 Here are a couple of example of my document names (ParcelNum_CountyNum_Year_DocType.pdf) 这是我的文档名称的几个示例(ParcelNum_CountyNum_Year_DocType.pdf)

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

I currently have the documents residing in a single folder named by the county number (the 3 digit number between the first _ and the 2nd _) 目前,我的文档位于一个以县号(第_和第二个_之间的3位数字)命名的文件夹中

I have folders created that are named by the ParcelNum, which is the first set of characters/numbers at the beginning and before the first _. 我创建了以ParcelNum命名的文件夹,该文件夹是在第一个_开头和之前的第一组字符/数字。 I need Powershell to read the first portion of the document name stopping at the first _ and place it in the folder that matches that numchar. 我需要Powershell来读取文档名称的第一部分,该部分从第一个_停止,并将其放置在与该numchar匹配的文件夹中。 So for example the first document example above is named 188777_014_2013_NOAV.pdf and resides at the root of the CountyNum folder named 014. For that example, I would need it to be moved from the root folder 014, into its subfolder named 188777. 因此,例如,上面的第一个文档示例名为188777_014_2013_NOAV.pdf,并位于CountyMum文件夹名为014的根目录下。例如,我需要将其从根文件夹014移至其名为188777的子文件夹中。

I hope that makes sense. 我希望这是有道理的。 Please feel free to ask questions. 请随时提问。

Thank you, 谢谢,

Here's what I would do. 这就是我要做的。 This is under the assumption that the files you want to move are in a parent folder and the root & sub folders you want to sort them to are also in the same folder, ie if the example PDF's path is C:\\test\\Docs\\188777_014_2013_NOAV.pdf and you want it to end up in C:\\test\\Docs\\014\\188777 . 这是基于以下假设:要移动的文件位于父文件夹中,而要对其进行排序的根目录和子文件夹也位于同一文件夹中,即示例PDF的路径为C:\\test\\Docs\\188777_014_2013_NOAV.pdf ,您希望它以C:\\test\\Docs\\014\\188777 C:\\test\\Docs\\188777_014_2013_NOAV.pdf结尾。

Update the path in the $basePath variable to your liking. 根据您的喜好,将$ basePath变量中的路径更新。 Comments are included above each step with explanations. 注释包含在每个步骤的上方,并带有说明。 Enjoy! 请享用!

# 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
}

Tried to solve your problem: 试图解决您的问题:

 #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 } } 

Store above in a ps1 file, eg moveFile.ps1 . 将其存储在ps1文件中,例如moveFile.ps1 Afterwards open a PowerShell and source the file: 然后打开PowerShell并获取文件:

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

Afterwards you can the function like: 之后,您可以使用以下功能:

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

If you need to debug it open the ps1 in the Powershell ISE, set a breakpoint and start the script. 如果需要调试,请在Powershell ISE中打开ps1,设置一个断点并启动脚本。 For further information see this link . 有关更多信息,请参见此链接

Hope that helps. 希望能有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Powershell根据文件名将文件移动到“文件夹和子文件夹”? - How to move files to Folder and Sub folder based on file name using Powershell? 使用powershell中的regex基于文件名命名文件夹 - Name folder based on file name using regex in powershell 使用 PowerShell 脚本根据 txt 文件中的文件/文件夹结构移动所有位于一个文件夹中的文件 - Move files that are all in one folder based on file/folder structure in txt file using PowerShell script powershell根据文件名的一部分移动文件 - powershell to move files based on part of file name 读取文件,然后根据文件内容名称,使用 Powershell 脚本将文件从一个文件夹移动到另一个文件夹 - Read a file and then based on file content names, move the file from one folder to another using Powershell script 如何使用 PowerShell 搜索特定文件名然后将该文件之后的所有文件移动到另一个文件夹 - How to search for a specific file name then move all files after that file to another folder using PowerShell Powershell:提取文件名的一部分以创建文件夹并将文件移动到文件夹中 - Powershell: Extracting parts of file name to create folders and move files into the folder 如何基于Powershell中的用户输入将文件移动到其他文件夹 - How to move a file to a different folder based on user input in powershell Powershell Script 移动同名不同文件类型到共享文件夹 - Powershell Script to move different file types with the same name to share folder Powershell根据调整后的文件名将文件移动到新目标 - Powershell Move file to new destination based on trimmed file name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM