将具有特定扩展名的文件移动到更高层次的文件夹中 [英] move files with specific extension to folder in higher hierarchy

查看:94
本文介绍了将具有特定扩展名的文件移动到更高层次的文件夹中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的所有文件都位于特定的文件夹中:

All my files are in specific folders:

17\1\1\PRO
17\1\2\PRO
17\2\1\PRO
xx\xx\xx\PRO

  • 17是年份(所以下一年是18,依此类推)
  • 第一个1是指定案例编号的文件夹(最多100个).
  • 第二个1是案件编号的子部分.
  • 最后1个文件夹中有一个文件夹PRO,所有数据都驻留在该文件夹中.

    That last 1 has a folder PRO in it where all data resides.

    我们需要移动这些文件,但是文件需要保留在各自的"PRO"文件夹中.

    We need to move these files, but the files need to stay inside their respective "PRO" folders.

    例如:

    • 17\1\1\pro\xxx\www\中的文件需要转到17\1\1\pro\movies
    • 17\2\2\pro\xxdfsdf\eeee\中的文件需要转到17\2\2\pro\movies.
    • a file in 17\1\1\pro\xxx\www\ needs to go to 17\1\1\pro\movies
    • a file in 17\2\2\pro\xxdfsdf\eeee\ needs to go to 17\2\2\pro\movies.

    如果有要移动的文件,则应创建电影文件夹.

    The movies folder should get created if there are files to move.

    我需要获取文件全名的一部分,然后将文件移到电影"文件夹中.问题是我不知道如何分割全名,将\ movies添加到其中并将文件移动到那里.

    I need to get a part of the full name of a file and move the file there to the "movie" folder. The problem is I do not know how to split the full name, add \movies to it and move the files there.

    到目前为止,这是我的代码:

    This is my code so far:

    Get-ChildItem -Path $mypath -Recurse -File -Filter $extension | select $_Fullname |
    Move-Item -Force -Destination ($_Fullname.Split("pro"))
    

    推荐答案

    如果您不知道有多少个目录,我会这样做:

    If you don't know how many directories there are, I would do something like this:

    Get-ChildItem -Path $mypath -Recurse -File -Filter $extension | ForEach-Object {
        if ($_.FullName.IndexOf('\PRO\') -gt 0) {
            $Destination = Join-Path -Path $_.FullName.Substring(0,$_.FullName.IndexOf('\PRO\') + 5) -ChildPath 'movies';
            New-Item $Destination -ItemType Directory -ea Ignore;
            $_ | Move-Item -Destination $Destination;
        } else {
            throw ("\PRO\ path not found in '$($_.FullName)'");
        }
    }
    

    只要您的路径只有一次\pro\,此方法就可以正常工作.如果它们像customer\pro\17\pro\17\1\1\pro\xx\yy\zz\www一样具有多个索引,并且您需要最后一个索引,请使用$_.FullName.LastIndexOf('\pro\').

    This will work fine as long as your paths only have \pro\ once. If they have it more than once like customer\pro\17\pro\17\1\1\pro\xx\yy\zz\www and you need the last index, then use $_.FullName.LastIndexOf('\pro\').

    如果在.\pro\movies\所在的目录之前和之后都有\pro\目录,那么您很麻烦.您可能必须找到其他参考点.

    If you've got \pro\ directories both before and after the directory that .\pro\movies\ is in, well, you're in trouble. You'll probably have to find a different point of reference.

    这篇关于将具有特定扩展名的文件移动到更高层次的文件夹中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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