File 基于名称将文件移动到文件夹的Windows脚本

File 基于名称将文件移动到文件夹的Windows脚本,file,sorting,File,Sorting,我做了一些研究,没有发现任何适合我的东西/我不能修改任何脚本 我想要一个脚本/链接,将文件/文件夹移动到第一个字符匹配的文件夹 就像我想把“apple.txt”移到文件夹“A” 与我要将文件夹“John”移动到文件夹“J”相同 感谢您的帮助我在搜索其他主题时找到了您的帖子,但下面的Powershell脚本满足了您的需要。将其放在包含文件的父目录中。它将根据文件名的第一个字母创建文件夹结构。它区分大小写,因此文件名中的“a”与“a”不同 更改第22行中的正则表达式以更改匹配行为 希望对你有用

我做了一些研究,没有发现任何适合我的东西/我不能修改任何脚本

我想要一个脚本/链接,将文件/文件夹移动到第一个字符匹配的文件夹

就像我想把“apple.txt”移到文件夹“A” 与我要将文件夹“John”移动到文件夹“J”相同


感谢您的帮助

我在搜索其他主题时找到了您的帖子,但下面的Powershell脚本满足了您的需要。将其放在包含文件的父目录中。它将根据文件名的第一个字母创建文件夹结构。它区分大小写,因此文件名中的“a”与“a”不同

更改第22行中的正则表达式以更改匹配行为

希望对你有用

还可以查看程序目录Opus。这是一款非常成熟、经过精心加工的产品。它可以轻松地处理这类任务。这对于随意使用来说有点贵,但如果你每天管理大量文件,那就太好了。我不知道没有它我该怎么办。在他们的论坛上有很多支持


-斯科特

你需要脚本吗?在Windows7文件资源管理器中,这样做会更容易。我有大约54个文件夹要做,所以如果我能得到一个,那会更容易。每个文件夹都会分支到子文件夹,因此我认为执行并运行脚本会更快。

$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;