【发布时间】:2014-09-26 10:18:01
【问题描述】:
有没有办法我们可以使用 powershell 基于文件扩展名创建文件夹,然后将这些文件移动到这些文件夹中。例如,我有 .jpg 文件和 .txt 文件。我希望 powershell 查看哪些文件是 .txt ,然后创建一个名为 textfiles 的文档并将所有 .txt 文件移动到该文件夹中。 我所有的文件都位于 C:\testfiles
$files = 'C:\testfiles\*.txt'
$foundfiles = Get-ChildItem $files -Filter *.txt -Force -Recurse
new-item $foundfiles -type directory
我知道这没有意义。真的需要帮助
我的脚本
Get-ChildItem 'C:\testfiles' -Filter *.txt | 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
}
这很完美,但问题是我在 10 个不同的位置有文件。但在我的脚本中,我只给出了 1 条路径。如何指定多个位置
【问题讨论】:
-
是的,有办法。到目前为止,您尝试过什么?
-
$files = 'C:\testfiles*.txt' $foundfiles = Get-ChildItem $files -Filter *.txt -Force -Recurse new-item $foundfiles
-
嗨 raf 我刚刚修改了问题
标签: powershell