[英]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.