Powershell根据文件名的初始部分移动Excel文件
在名为Powershell根据文件名的初始部分移动Excel文件,powershell,Powershell,在名为D:\TestARC\u Source 5020190429.dat 5120190429.dat 602019111121.dat 702019050926.dat etc. 我需要一个脚本,根据文件名的前两个字符将它们移动到相应的文件夹中。已在目标目录上创建文件夹 D:\TestARC_Destination\file_50 D:\TestARC_Destination\file_51 D:\TestARC_Destination\file_60 D:\TestARC_Destin
D:\TestARC\u Source
5020190429.dat
5120190429.dat
602019111121.dat
702019050926.dat
etc.
我需要一个脚本,根据文件名的前两个字符将它们移动到相应的文件夹中。已在目标目录上创建文件夹
D:\TestARC_Destination\file_50
D:\TestARC_Destination\file_51
D:\TestARC_Destination\file_60
D:\TestARC_Destination\file_70
文件应根据文件名的前两个字符移动到相应的文件夹。例如,5020190429.dat
移动到50_文件
文件夹
我试图编辑下面的脚本,但它不工作。我有一个非常小的PS脚本知识,如果你能帮助我,我将不胜感激
$SourceFolder = "C:\Users\Administrator\Desktop\ARC_Testing\TestARC_Source\"
$targetFolder = "C:\Users\Administrator\Desktop\ARC_Testing\TestARC_Destination\"
# Find all files matching *.sql in the folder specified
Get-ChildItem -Path $SourceFolder -Filter *.dat | ForEach-Object {
# Combine the source filename and target directory
# The source filename has all instances of _ replaced with \
# Cast the resulting string to a FileInfo object to take advantage of extra methods
[System.IO.FileInfo]$destination = (Join-Path -Path $targetFolder -ChildPath $_.Name.replace("_","\"))
# Create the directory if it doesn't already exits
if (!(Test-Path) $destination.Directory.FullName)
{
New-item -Path $destination.Directory.FullName -ItemType Directory
}
# Copy the source to the target directory
copy-item -Path $_.FullName -Destination $Destination.FullName
}
以下内容应根据您的条件移动文件。如果您对将要移动的内容感到满意,请删除
-WhatIf
开关
$Source = "D:\TestARC_Source"
$Dest = "D:\TestARC_Destination"
$FilesToMove = Get-ChildItem -Path $Source -File -Recurse -Filter "*.dat" -Include "[0-9][0-9]*"
Foreach ($file in $FilesToMove) {
Move-Item -Path $file -Destination "$Dest\File_$($file.basename.substring(0,2))” -WhatIf
}
一种有效的方法是按前两位数字对文件进行分组,
检查文件夹是否存在(必要时创建),并一次性移动文件
## Q:\Test\2019\05\31\SO_56397901.ps1
$SourceFolder = "C:\Users\Administrator\Desktop\ARC_Testing\TestARC_Source\"
$targetFolder = "C:\Users\Administrator\Desktop\ARC_Testing\TestARC_Destination\"
Get-ChildItem -Path "$SourceFolder[0-9][0-9]*" -Filter *.dat |
Group-Object {"file_"+$_.BaseName.Substring(0,2)}| ForEach-Object {
$TargetDir = Join-Path $targetFolder $_.Name
if(!(Test-Path $TargetDir)){MD $TargetDir | Out-Null}
$_.Group.FullName | Move-Item -Destination $TargetDir #-WhatIf
}
使用上述测试数据运行后的树:
> TREE /f
├───FILE_50
│ 5020190429.DAT
│
├───FILE_51
│ 5120190429.DAT
│
├───FILE_60
│ 602019111121.DAT
│
└───FILE_70
702019050926.DAT
编辑:不断变化的需求通常会带来新的问题,但变化很小
## Q:\Test\2019\05\31\SO_56397901.ps1
$SourceFolder = "C:\Users\Administrator\Desktop\ARC_Testing\TestARC_Source\"
$targetFolder = "C:\Users\Administrator\Desktop\ARC_Testing\TestARC_Destination\"
Get-ChildItem -Path "$SourceFolder[0-9][0-9]*" -Filter *.dat |
Group-Object {$_.BaseName.Substring(0,2)+"_file"}| ForEach-Object {
$TargetDir = Join-Path $targetFolder $_.Name
if(Test-Path $TargetDir){
$_.Group.FullName | Move-Item -Destination $TargetDir #-WhatIf
} else {
"{0} doesn't exist, files not moved: {1}" -f $TargetDir,($_.Group.FullName -join ';')
}
}
您的文本和示例不同,是方案
文件50
还是50\u文件
?当你的代码复制时,你的标题也会移动?嗨。。对不起,我解释错了。实际上,文件夹名称是50_file而不是file_50。其他53_文件、60_文件和70_文件也是如此。我们是否可以再添加一项功能,即如果出现任何其他文件(如14、20等),而目标文件夹中没有该文件夹,则该文件应保持在源文件夹中的状态。谢谢你,很抱歉弄得一团糟:)请参阅更改后的答案。一个新贡献者的暗示:如果一个答案解决了你的问题,或者你发现它有帮助,你应该考虑✔ 和/或(一旦你得到15分,就会看到)