我做了一些研究,但没有找到任何适合的/我无法更改任何脚本。
我想要一个脚本/链接,将文件/文件夹移动到第一个字符匹配的文件夹中。
就像我想将“apple.txt”移动到文件夹“A”一样,我想将文件夹“John”移动到文件夹“J”
谢谢您的帮助
我在搜索另一个主题时找到了您的帖子,但是以下 Powershell 脚本可以满足您的需求。将它放在包含您的文件的父目录中。它将根据文件名的第一个字母创建文件夹结构。它区分大小写,因此文件名中的“a”与“A”不同。
$OrigFolder = ".\"
$NewFolder = ".\_Sorted to Move"
# Orphans folder, where files that return null in the regex match will be moved
# Example: file "- title.pdf"
# will be moved to ".\_Orphans" folder
$Orphans = '_Orphans' # Use the underscore to sort the folder to the top of the window
# First count the number of files in the $OrigFolder directory
$numFiles = (Get-ChildItem -Path $OrigFolder).Count
$i=0
# Tell the user what will happen
clear-host;
Write-Host 'This script will copy ' $numFiles ' files from ' $OrigFolder ' to _Sorted to Move'
# Ask user to confirm the copy operation
Read-host -prompt 'Press enter to start copying the files'
# Regex to match filenames
$Regex = [regex]"(?:([A-Za-z0-9]?)[A-Za-z0-9]?)"
# Loop through the $OrigFolder directory, skipping folders
Get-ChildItem -LiteralPath $OrigFolder | Where-Object {!$_.PsIsContainer} |
ForEach-Object {
if($_.BaseName -match $Regex){
$ChildPath = $_.BaseName -replace $Regex
#Caluclate copy operation progress as a percentage
[int]$percent = $i / $numFiles * 100
# If first part of the file name is empty, move it to the '_Orphans' folder
if(!$Matches[1]){
$ChildPath = $Orphans}
else {
$ChildPath = $Matches[1]
}
# Generate new folder name
$FolderName = Join-Path -Path $NewFolder -ChildPath ($ChildPath)
# Create folder if it doesn't exist
if(!(Test-Path -LiteralPath $FolderName -PathType Container)){
$null = New-Item -Path $FolderName -ItemType Directory}
# Log progress to the screen
Write-Host "$($_.FullName) -> $FolderName"
# Move the file to the folder
Move-Item -LiteralPath $_.FullName -Destination $FolderName
# Tell the user how much has been moved
Write-Progress -Activity "Copying ... ($percent %)" -status $_ -PercentComplete $percent -verbose
$i++
}
}
Write-Host 'Total number of files in '$OrigFolder ' is ' $numFiles
Write-Host 'Total number of files copied to '$NewFolder ' is ' $i
Read-host -prompt "Press enter to complete..."
clear-host;
更改第 22 行中的正则表达式以更改匹配行为。
希望对你有效。
还可以查看程序 Directory Opus。这是一个非常成熟的产品。它可以轻松处理这些类型的任务。临时使用它有点贵,但是如果您每天管理大量文件,那真是天赐之物。我不知道没有它我该怎么办。在他们的论坛上有很多支持。
——斯科特