【问题标题】:Powershell to move Excel files based on Initial part of the file namePowershell根据文件名的初始部分移动Excel文件
【发布时间】:2019-10-17 06:28:23
【问题描述】:

我在名为 D:\TestARC_Source 的目录中有数百个类似以下的 excel 文件

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_file 文件夹。

我正在尝试编辑下面的脚本,但它不起作用。我对 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 
} 

【问题讨论】:

  • 您的文字和示例不同,方案是file_50 还是50_file?你的标题也会移动,而你的代码会复制?
  • 嗨..对不起我的错误解释。实际上文件夹名称是 50_file 而不是 file_50。其他 53_file、60_file 和 70_file 也是如此。我们能否再添加一项功能,如果出现任何其他文件(如 14、20 等)并且该文件夹在目标中不可用,它应该将文件保留在源文件夹中。谢谢你,很抱歉造成混乱:)

标签: powershell


【解决方案1】:

以下内容应根据您的条件移动文件。如果您对将要移动的内容感到满意,请移除-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
}

【讨论】:

    【解决方案2】:

    一种有效的方法是按前两位数对文件进行分组,
    检查文件夹是否存在(并在必要时创建)并一次性移动文件。

    ## 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 ';')
          } 
    }
    

    【讨论】:

    • 查看更改后的答案。给新贡献者的提示:如果答案解决了您的问题或者您觉得它有帮助,您应该考虑发送至 accept the answer✔ 和/或 vote up(一旦您获得了 15 个的 reputation,就会看到)跨度>
    猜你喜欢
    • 2017-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-09
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多