我有一个文件组织者powershell脚本运行没有任何错误,但它不执行移动操作



这是我为自己写的一个文件整理脚本。为了我的一个特殊目的。每当我试图运行它,它运行和关闭。但是移动操作没有发生。下面的注释可以帮助您理解代码的作用。请告诉我我在这里做错了什么。我对Powershell脚本非常陌生。

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
# Global variable declarations
$global:pathsFromConfig = Get-Content -Path $PSScriptRoot"MoverPaths.txt"
$global:categoriesFromConfig = Get-Content -Path $PSScriptRoot"MoverCategories.txt"
$global:categryHash = @{}
# Method call to read configs, create dirs, & move files
readCreateAndMoveFiles
# Method definition
function readCreateAndMoveFiles{
# Reads categories config.txt and splits them line by line
# Adds each line as a key value pair to a hashtable
foreach($category in $categoriesFromConfig)
{
$temp = $category -split ":"  
$categryHash.add($temp[0].trim().toString(),($temp[1]).trim().toString())
}
# For each category in the hash table, calls create directory method, and then moves the files based on current category
foreach($hashItem in $categryHash.GetEnumerator()){
# Creates a directory with the Hash Key
Foreach($pathToMonitor in $pathsFromConfig){
$categoryFullPath = $pathToMonitor+$hashItem.Name
createDirectory($categoryFullPath)
# Moves files into that directory 
Set-Location -Path $pathToMonitor
$extentions = $hashItem.Value
Get-Item $extentions | Move-Item -Destination $categoryFullPath
$categoryFullPath = ""
}
}
}

# Method Definition
function createDirectory ($categoryName)
{
if(Test-Path -Path $categoryName)
{
# Directory already Exists!
}
else
{
# Creates Directory
md $categoryName
}
}

配置文件如下:

MoverCategories.txtImages:*.jpg,*.jpeg,*.png,*.tiff,*.raw,*.heic,*.gif,*.svg,*.eps,*.icoDocuments:*.txt,*.pdf,*.doc,*.docx,*.xls,*.xlsx,*.ppt,*.pptx,*.html,*.xls,*.csv,*.rtx

MoverPaths.txt

D:Downloads

我找到了一个方法。谢谢你的建议。现在脚本移动文件。而不是发送所有的扩展在一个单一的镜头,我把它变成一个数组,并发送它一个接一个。现在它运行良好。如果你们能帮我缩短执行时间就太好了。但是代码现在可以工作了,我很高兴。

foreach($hashItem in $categryHash.GetEnumerator()){
# Creates a directory with the Hash Key
Foreach($pathToMonitor in $pathsFromConfig){
$categoryFullPath = $pathToMonitor+$hashItem.Name
createDirectory($categoryFullPath)
# Moves files into that directory 
[String[]]$extentions = @()
$extentions = $hashItem.Value -split ','
foreach($string in $extentions)
{
Get-Item $pathToMonitor* -Include $string | Move-Item -Destination $categoryFullPath
}
}
}

试试这个

#specify path(s)
$path = "$env:USERPROFILEDownloads"
## this is make an array of the extensions in the foloder
$extensions = Get-ChildItem -Path $path  | Select-Object -Unique  -Property @{label = 'ext'
expression                                                                        = { $_.Extension.substring(1) }
}
## this function will 
function New-FoldersByName {
param
(
[Parameter(Mandatory = $true, ValueFromPipeline = $true, HelpMessage = 'Data to process')]
$InputObject
)
process { 
Set-Location $path
if (!(Test-Path -PathType Container $InputObject )) {
New-Item -ItemType directory -Name $InputObject.ext -WhatIf
Write-Host -Message "A folder named $($InputObject.ext) does not exist. Creating..."
}
else {
Write-Host -Message "A folder named $($InputObject.ext) already exists. Skipping..."
}
}
}

##this is a reuseable function to moves items in a folder into a subfolder named after the files extension
## if extension is .exe the file with be moved to ./EXE/filename.exe
function Move-ItemsByName {
param
(
[Parameter(Mandatory = $true, ValueFromPipeline = $true, HelpMessage = 'Data to process')]
$InputObject
)
process {
Set-Location -Path $path
Move-Item -Path ('*.{0}' -f $InputObject.ext) -Destination ('{0}' -f $InputObject.ext) -WhatIf
}
}
$extensions | New-FoldersByName
$extensions | Move-ItemsByName

最新更新