我做了一些研究,但没有找到适合的东西/我不能改变任何剧本。
我想要一个脚本/链接,将文件/文件夹移动到第一个字符匹配的文件夹中。
就像我想将'apple.txt'移到文件夹'A' 我希望将文件夹“John”移动到文件夹“J”
感谢您的帮助
答案 0 :(得分:0)
我在搜索其他主题时找到了您的帖子,但以下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行中的正则表达式以更改匹配行为。
希望它适合你。
另请查看程序目录Opus。这是一款非常成熟,精心打造的产品。它可以轻松处理这些任务。这对于偶然使用来说有点贵,但是如果你每天都管理很多文件,这是天赐之物。我不知道没有它我会怎么做。很多人都支持他们的论坛。
- 斯科特