Powershell-根据文件名将文件复制到不同的目标文件夹 [英] Powershell - copy files to different destination folders based on file names

查看:495
本文介绍了Powershell-根据文件名将文件复制到不同的目标文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的Powershell Gurus,

Dear Powershell Gurus,

我在一个名为C:\Downloads\Signs的文件夹中有数千个文件。
文件以其尺寸命名,例如13X20 abcdjd.psf,8X20 jdscnjfc.psf,14X24 dje.psf等。
我要做的是将这些文件移动到C中创建的目标文件夹中。 :\Downloads\Signs和文件夹名是文件名的大小。例如,文件夹名称将为13X20、8X20、14X24等,并且取决于其尺寸的唯一文件名。
因此,与其手动查看C:\Downloads\Signs文件夹中有多少文件然后分别移动它们,不如手动移动它们,我们如何在Powershell中做到这一点?

I have a few thousands of files in a folder called C:\Downloads\Signs. The files are named with their dimensions such as 13X20 abcdjd.psf, 8X20 jdscnjfc.psf, 14X24 dje.psf etc. What I want to do is to move these files to destination folders created within the C:\Downloads\Signs and the folder names are the dimensions of the file names. Example the folder names will be 13X20, 8X20, 14X24 etc and it depends upon as many unique file names with their dimensions. So, instead of moving them manually looking at how many files are there in the C:\Downloads\Signs folder and then moving them individually, how can we do it in Powershell?

谢谢,
Sanders。

Thanks, Sanders.

推荐答案

此脚本将从C:\Downloads\Signs文件夹的根目录,并将文件移至目标文件夹(如果文件夹不存在,则创建文件夹):

This script will pick up all psf from the root of the C:\Downloads\Signs folder and will move the files to the destination folders (folders will create if they do not exist):

Get-ChildItem C:\Downloads\Signs -Filter *.psf | Where-Object {!$_.PSIsContainer} | Foreach-Object{

    $dest = Join-Path $_.DirectoryName $_.BaseName.Split()[0]

    if(!(Test-Path -Path $dest -PathType Container))
    {
        $null = md $dest
    }

    $_ | Move-Item -Destination $dest -Force
}

这篇关于Powershell-根据文件名将文件复制到不同的目标文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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