文件组织PowerShell脚本 [英] File organization powershell script

查看:199
本文介绍了文件组织PowerShell脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图监视一个目录及其新文件的子目录。我想看看文件扩展名,并根据文件扩展名将其移动到合适的文件夹。例如,如果我有一个文件夹C:\\\\\\\\\\\\\\\\\\\\并且我下载了一个AVI文件到该文件夹​​,像脚本一样的id来看它,并将文件移动到C:\\ \\自动运行。或者,如果我下载的MP3文件的整个文件夹,我想它将自动将整个文件夹移动到C:\音乐。现在它只是移动MP3文件到C:\音乐,但我不知道如何移动父文件夹。



以下是我迄今为止所写的内容:请记住,我是PowerShell新手!



<$ p如果(!(Test-Path -path C:\music))
{
New-Item C:\music\ -type directory

if(!(Test-Path -path C:\movies))

{
New-Item C:\movies\ -type directory


$ b $ PickupDirectory = Get-ChildItem -recurse -PathC:\ users \Dave\desktop\test
$ folder ='C :\ users \Dave\desktop\test'
$ watcher = New-Object System.IO.FileSystemWatcher $ folder
$ watcher.IncludeSubdirectories = $ true
$ watcher.EnableRaisingEvents = $ true
$ watcher



Register-ObjectEvent $ watcher -EventName Changed -SourceIdentifier'Watcher'-Action {




foreach($ PickupDirectory中的$ file)
{
if($ file.Extension.Equals('。avi'))
{

写主机$ file.FullName#输出文件已满名称到屏幕
写主机$目的地#输出完整目的地路径到屏幕

move-Item $ file.FullName -destination c:\电子
}

if($ file.Extension.Equals('。mp3'))
{

写主机$ file.FullName#输出文件全名到屏幕
写-Host $ Destination #Output完整的目的地路径屏幕

move-Item $ file.FullName -destination c:\music
}


if($ file.Extension.Equals(.mp4))
{

写主机$ file.FullName#输出文件全名到屏幕
写主机$目的地#输出完整目标路径到屏幕

move-Item $ file.FullName -destination c:\movies
}
}
}
}

由于某些原因,它不会移动文件。我得到它看到的变化,但不移动文件。



请帮助!

解决方案

事件的Action脚本块运行

尝试移动这一行:

  $ PickupDirectory = Get-ChildItem -recurse -PathC:\ users \Dave\desktop\test

放入您的动作脚本块,看看是否有帮助。



如果一个Switch的语句,但这只是个人喜好。

添加该父路径的一种方法



<$ p $ code $ $($ file.fullname).split('\')[ - 2]
[void] [IO.Directory] ​​:: CreateDirectory(c:\音乐\\ $ $父母)
move-Item $ file.FullName -destination c:\music\ $ parent


Im trying to monitor a directory and its subdirectory for new files. I want to look at the file extension and move it to its appropriate folder based on the file extension. For example, If i have a folder "C:\users\dave\desktop\test" and i download an avi file to that folder, id like the script to see it and move the file to a C:\movies automatically. Or if i download an entire folder of mp3 files, i'd like it to move that entire folder to C:\music automatically. Right now it moves just the mp3 files into C:\music, but I cant figure out how to move the parent folder too.

Here is what i have written so far: Keep in mind, I'm new to powershell!

if (!(Test-Path -path C:\music))
{
   New-Item C:\music\ -type directory
}
if (!(Test-Path -path C:\movies))

{
   New-Item C:\movies\ -type directory
}


$PickupDirectory = Get-ChildItem -recurse -Path  "C:\users\Dave\desktop\test"
$folder = 'C:\users\Dave\desktop\test'
$watcher = New-Object System.IO.FileSystemWatcher $folder
$watcher.IncludeSubdirectories = $true
$watcher.EnableRaisingEvents = $true
$watcher



Register-ObjectEvent $watcher -EventName Changed -SourceIdentifier 'Watcher' -Action { 




foreach ($file in $PickupDirectory)
{
    if ($file.Extension.Equals('.avi'))
    {

        Write-Host $file.FullName #Output file fullname to screen
        Write-Host $Destination   #Output Full Destination path to screen

    move-Item $file.FullName -destination c:\movies
}

 if ($file.Extension.Equals('.mp3'))
   {

    Write-Host $file.FullName #Output file fullname to screen
    Write-Host $Destination   #Output Full Destination path to screen

    move-Item $file.FullName -destination c:\music
}


if ($file.Extension.Equals(".mp4"))
    {

    Write-Host $file.FullName #Output file fullname to screen
    Write-Host $Destination   #Output Full Destination path to screen

    move-Item $file.FullName -destination c:\movies
}
}
}
}

For some reason, it does not move the files. Ive gotten it to see changes, but not move files.

Please Help!

解决方案

The Action scripblock of events runs in a different runspace, and the variables in your main script aren't visible there.

Try moving this line:

$PickupDirectory = Get-ChildItem -recurse -Path  "C:\users\Dave\desktop\test"

into your action script block and see if that helps.

I'd also trade that stack of If statements for a Switch, but that's just personal preference.

One way to add that parent path

 $parent = ($file.fullname).split('\')[-2]
 [void][IO.Directory]::CreateDirectory("c:\music\$parent")
 move-Item $file.FullName -destination c:\music\$parent

这篇关于文件组织PowerShell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆