简体   繁体   English

使用powershell根据文件名移动/创建文件夹/子文件夹

[英]Using powershell to move/make folders/subfolders based on filename

I don't really have much experience in powershell but I have files that I need to organize.我在 powershell 方面并没有太多经验,但我有需要整理的文件。 The files are all pdf and will have a format similar to "Dept123_Name_year.pdf".这些文件都是 pdf,格式类似于“Dept123_Name_year.pdf”。

I want to move the documents into a folder based on "Dept123" and a sub folder "Name".我想将文档移动到基于“Dept123”和子文件夹“Name”的文件夹中。 If the folder is yet to exist I would like it to create the folder/subfolder.如果文件夹尚不存在,我希望它创建文件夹/子文件夹。

To make it easier I was thinking of using creating an "Organize" folder on the desktop and running the program on that.为了方便起见,我想在桌面上创建一个“组织”文件夹并在其上运行程序。 If you think it'd be easier some other way, let me know.如果您认为以其他方式更容易,请告诉我。

Thanks in advance.提前致谢。

You can use a regular expression to match the different components of the filenames, then generate a directory structure based on that.您可以使用正则表达式来匹配文件名的不同组成部分,然后基于此生成目录结构。 The -Force parameter of mkdir lets you ignore whether or not the directory already exists: mkdir-Force参数让你忽略目录是否已经存在:

$list = ls
for ($i=0; $i -le $list.Length; $i++) {
    if ($list[$i].Name -match '([A-Za-z0-9]+)_([A-Za-z]+)_.*\.pdf') {
        $path = Join-Path $matches[1] $matches[2]
        mkdir -Force -Path $path
        cp $list[$i] "$path\."
    }
}

The regular expression part is in the quotes;正则表达式部分在引号中; you might need to modify it to suit your specific needs.您可能需要对其进行修改以满足您的特定需求。 Note that the sections in round brackets correspond to the different parts of the name being extracted;请注意,圆括号中的部分对应于被提取名称的不同部分; these parts are loaded, in order, into the $matches variable, which is generated automatically.这些部分按顺序加载到自动生成的$matches变量中。 Eg '([A-Za-z0-9]+)\\.txt' will match any text file with only letters or numbers in the name, and will stick the actual name - minus the extension - into $matches[1] .例如'([A-Za-z0-9]+)\\.txt'将匹配名称中只有字母或数字的任何文本文件,并将实际名称 - 减去扩展名 - 粘贴到$matches[1]

Using regex and full-form Powershell:使用正则表达式和完整形式的 Powershell:

# using ?<name> within a () block in regex causes powershell to 'name' the property 
# with the given name within the automatic variable, $matches, object.
$Pattern = "(?<Dept>.*)_(?<Name>.*)_(?<Year>.*)\.pdf"

# Get a list of all items in the current location.  The location should be set using
# set-location, or specified to the command by adding -Path $location
$list = Get-ChildItem

# Foreach-Object loop based on the list of files
foreach ($file in $list) {
    # send $true/$false results from -matches operation to $null
    $File.Name -matches $Pattern 2> $Null

    # build destination path from the results of the regex match operation above
    $Destination = Join-Path $matches.Dept $matches.Name

    # Create the destination if it does not exist
    if (!(Test-Path $Destination) ) { 
        New-Item -ItemType Directory -Path $destination 
    }

    # Copy the file, keeping only the year part of the name
    Copy-Item $file "$destination\$($matches.year)"+".pdf"
}

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

相关问题 如何使用 PowerShell 根据文件名移动文件? - How to move files based on filename using PowerShell? PowerShell 将多个文件夹的子项移动到它们自己的子文件夹中 - PowerShell to move subitems of multiple folders into their own subfolders Powershell:将文件夹和子文件夹中的所有文件移动到单个文件夹中 - Powershell: Move all files from folders and subfolders into single folder 在Powershell中使用Robocopy移动文件夹 - Using Robocopy in Powershell to move folders 使用Powershell在共享邮箱中使用&#39;/&#39;批量重命名文件夹和子文件夹 - Bulk rename folders and subfolders with '/' in shared mailbox using powershell PowerShell:查找子文件夹中没有文件的文件夹 - PowerShell: Finding folders with no file in subfolders 使用变量的文件名并使用 Powershell 移动类似的文件 - Using a filename for a variable and move similar files with Powershell 需要根据文件名将一系列数字文件移动到文件夹中 - Need to move a range of numerical files into folders based on filename 创建文件夹并将文件移动到基​​于文件名的文件夹结构 - Create folders and move files to folder structure based on filename PowerShell脚本将文件和文件夹(包括子文件夹)从一个位置移动到x天以外的另一个位置 - PowerShell script to move files and folders including subfolders from one location to another older than x days
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM