file - Powershell:复制子文件夹中的选定文件

标签 file powershell directory subdirectory

我有一个文件夹 c:\graphics (root)和一个创建子文件夹的程序,例如aa , bm , czx , mjpq , zz (我有很多子文件夹,长度是可变的)。

在每个子文件夹中,我有 26 个文件和一个文件夹。例如,aa文件夹有:aa_000.jpg , aa_001.jpg , ..., aa_023.jpg ;其他两个文件 aa_360.jpg , aa.html和一个文件夹。完整路径为C:\graphics\aa\aa_360 .

我需要移动子文件夹 aa_000.jpg 中的 24 个文件( aa_001.jpg , aa_023.jpg , ..., c:\graphics\aa\aa_360 )也等于每个子文件夹。

另一个例子是c:\graphics\mjpq\mjpq_360 , 应该有 mjpq_000.jpg , ..., mjpq_023.jpg .

我开始编写脚本,考虑移动所有 .jpg (25 个文件)在子文件夹中(后来,我不得不考虑提取 xxx_360.jpg )但它不起作用:

Get-ChildItem c:\graphics -rec | where-object {$_.extension -match "jpg"} | ForEach-object {$newPath =(Get-Location).toString()+(Get-Location).ToString().SubString(10)+"_360"); Move-Item $_.FullName $newPath}

但是Get-Location找不到文件路径。


注意:我找到了一个有效的解决方案,但它在控制台中引发了一些错误:

Get-ChildItem c:\graphics -rec | where-object {$_.extension -match "jpg" -and (!($_.name -like "*360*"))} | ForEach-Object {$newPath =($_.DirectoryName).toString()+($_.DirectoryName).ToString().SubString(10)+"_360"; Move-Item $_.FullName $newPath}

最佳答案

我认为你可以将其简化为:

Get-ChildItem c:\graphics -rec *.jpg | Move-Item -Dest {$_.Directory.FullName + "\" +
    $_.Directory.Name + "_360\" } -WhatIf

如果建议的移动操作看起来正确,请删除 -whatif

关于file - Powershell:复制子文件夹中的选定文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15352068/

相关文章:

powershell - 使用 StreamWriter() 写入与 StreamReader() 相同的文件

azure - 向网站授予应用程序注册权限

powershell - 可重用的自定义排序

linux - 在遍历文件夹时更改工作目录

c++ - 当 QFilesystemModel 显示驱动器时,可以在 QTreeView 中更改文件夹颜色吗?

python - python 从不同目录下的多个文件中读取特定数据

c - 从 C 中的路径名中提取基本路径

python - 将列表从 iPython 单元移动到数据文件中

c++ - 在 C++ 中将文件中的间隔整数读入数组

ruby - 检查 Ruby 中的目录是否为空