Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 跨目录移动和操作文件-Powershell_Windows_Powershell_Batch File - Fatal编程技术网

Windows 跨目录移动和操作文件-Powershell

Windows 跨目录移动和操作文件-Powershell,windows,powershell,batch-file,Windows,Powershell,Batch File,我正在尝试将文件列表从一个目录移动到另一个目录。问题是,当项目移动到新目录时,我想自动组织它们 例。。 我有一个包含数千个文件名的文件夹 所有文件名都是相对于用户的userID的。某些用户在此文件夹中有多个文件,因此它会在名称的末尾附加一个数字。即susy.txt,susy1.txt,susy2.txt,carl.txt,carl1.txt,等等 我试图做的是为每个有多个文件的用户创建一个特定的文件夹(在新目录中),并将所有相关文件移动到该文件夹中。所以我注意到有多个susy文档。所以我想创建一

我正在尝试将文件列表从一个目录移动到另一个目录。问题是,当项目移动到新目录时,我想自动组织它们

例。。 我有一个包含数千个文件名的文件夹

所有文件名都是相对于用户的userID的。某些用户在此文件夹中有多个文件,因此它会在名称的末尾附加一个数字。即
susy.txt
susy1.txt
susy2.txt
carl.txt
carl1.txt
,等等

我试图做的是为每个有多个文件的用户创建一个特定的文件夹(在新目录中),并将所有相关文件移动到该文件夹中。所以我注意到有多个susy文档。所以我想创建一个名为Susy的文件夹,并将
Susy.txt
susy1.txt
,和
susy2.txt
放入其中。。。对于所有文件,依此类推

甚至可以作为一个批处理文件来做这件事吗?如果可以的话,有人能告诉我做这件事的正确方向吗?我在编写批处理脚本方面有少量的知识,我想借此机会了解更多


这与我先前提出的一个问题非常相似。我非常感谢收到的回复,他们给了我很大的帮助。当时,Adi Inbar的回答正是我所需要的。然而,我被迫做了一个修改,我自己也试过了

Adi Inbar的回答

Get-ChildItem | ?{$_.Name -match '(\D+)\d*\.txt'} | %{
  md $matches[1] -ea SilentlyContinue 
  Move-Item $_ $matches[1]
}
简短的甜言蜜语也很重要,这正是我所需要的。但是,它仅适用于将要组织但保留在同一父文件夹中的文件

这就是我所尝试的:

Get-ChildItem –path "P:\My Documents\Org Test\Test1" | Where{$_.Name -match '(\D+)\d*\.txt'} | Foreach{
     md P:\'My Documents'\'Org Test'\Test2\$matches[1] -ea SilentlyContinue
     Move-Item $_ P:\'My Documents'\'Org Test'\Test2\$matches[1]
}
据我所知和基本理解,这应该是可行的。。。但我得到一个错误,说移动项目:无法创建文件时,该文件已经存在

At P:\My Documents\Org Test\Test.ps1:3 char:3
    +      Move-Item -Path P:\'My Documents'\'Org Test'\Test1\$_ -destination P:\'M ...
    +    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : WriteError: (P:\My Documents...t1\Johnny123.txt:FileInfo) [Move-Item], I
        + FullyQualifiedErrorId : MoveFileInfoItemIOError,Microsoft.PowerShell.Commands.MoveItemCommand
我确信它就在我的嘴边,但我无法得到它。我有非常基本的powershell脚本编写经验,只需要快速修复


编辑:

我已经能够通过使用以下脚本“解决”我的问题:

Get-ChildItem –path P:\'My Documents'\'PST Org Script'\Test1 | Foreach-Object{ 
    move-item -Path $_.fullname -Destination "P:\My Documents\PST Org Script\Test2" -ea SilentlyContinue }

cd P:\'My Documents'\'PST Org Script'\Test2

Get-ChildItem | ?{$_.Name -match '(\D+)\d*\.txt'} | %{
  md $matches[1] -ea SilentlyContinue 
  Move-Item $_ $matches[1]
} 
我很好奇。我觉得这可以在上面的3行代码中完成。这似乎有点多余。但我知道什么


谢谢

我已经能够通过以下方式解决我的问题:

Get-ChildItem –path P:\'My Documents'\'PST Org Script'\Test1 | Foreach-Object{ 
    move-item -Path $_.fullname -Destination "P:\My Documents\PST Org Script\Test2" -ea SilentlyContinue }

cd P:\'My Documents'\'PST Org Script'\Test2

Get-ChildItem | ?{$_.Name -match '(\D+)\d*\.txt'} | %{
  md $matches[1] -ea SilentlyContinue 
  Move-Item $_ $matches[1]
} 
然而,我觉得,有一种更短、更简单的方法,可以通过我先前在原始问题中输入的方法的一些变体来实现这一点。

试试这个:

$srcPath = 'P:\My Documents\PST Org Script\Test1'
$dstPath = 'P:\My Documents\PST Org Script\Test2'
Get-ChildItem $srcPath | Where {$_.Name -match '(\D+)\d*\.txt'} | 
    Foreach {$targetDir = Join-Path $dstPath $matches[1]
             md $targetDir -ea 0
             Move-Item $_ $targetDir -WhatIf}