简体   繁体   English

Powershell根据文件名的初始部分移动Excel文件

[英]Powershell to move Excel files based on Initial part of the file name

I have hundreds of excel files like the following in directory called D:\\TestARC_Source 我在名为D:\\TestARC_Source目录中有数百个类似以下的excel文件

5020190429.dat
5120190429.dat
602019111121.dat
702019050926.dat 
etc.

I need a script to move them to respective folders based on first two characters of file name. 我需要一个脚本,根据文件名的前两个字符将它们移动到相应的文件夹中。 Folders are already created on target directory. 文件夹已在目标目录上创建。

D:\TestARC_Destination\file_50
D:\TestARC_Destination\file_51
D:\TestARC_Destination\file_60
D:\TestARC_Destination\file_70

Files should move to respective folders based on first two characters of file name. 文件应基于文件名的前两个字符移动到相应的文件夹。 For example 5020190429.dat moves to 50_file folder. 例如5020190429.dat移至50_file文件夹。

I am trying to edit below script but it's not working. 我正在尝试编辑以下脚本,但无法正常工作。 I have a very little knowledge in PS script and I'll appreciate if you can help me out. 我对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 
} 

The following should move the files based on your criteria. 以下内容应根据您的条件移动文件。 Please remove the -WhatIf switch if you are satisfied with what will be moved. 如果对要移动的内容感到满意,请删除-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
}

An efficient approach would be to group the files by the 1st two digits, 一种有效的方法是将文件按前两位数字分组,
check if the folder exists (and create if necessary) and move the files in one go. 检查文件夹是否存在(并在必要时创建)并一次性移动文件。

## 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 after running with your above testdata: 使用上面的测试数据运行后的树:

> TREE /f
├───FILE_50
│       5020190429.DAT
│
├───FILE_51
│       5120190429.DAT
│
├───FILE_60
│       602019111121.DAT
│
└───FILE_70
        702019050926.DAT

EDIT: Changing Requirements usually afford a new question, nevertheless the changes are minimal 编辑:更改需求通常会提出一个新问题,尽管更改很小

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 powershell根据文件名的一部分移动文件 - powershell to move files based on part of file name 根据文件名中的奇数移动文件 - powershell - Move files based on odd number in file name - powershell 根据文件名时间戳Powershell将文件移动到年/月文件夹 - Move files into year/month folders based on file name timestamp powershell 如何使用Powershell根据文件名将文件移动到“文件夹和子文件夹”? - How to move files to Folder and Sub folder based on file name using Powershell? Powershell 命令/脚本根据日期将文件移动到子目录(在文件名的前 8 个字符中) - Powershell command / script to move files to subdirectories based on date (in the first 8 characters of the file name) 根据文件名中的日期戳移动文件 - Move Files Based on Date Stamp in File Name Powershell - 根据文件名将文件分类到目录中 - Powershell - Sort files into directory based on file name Powershell根据调整后的文件名将文件移动到新目标 - Powershell Move file to new destination based on trimmed file name 使用Powershell根据文件名将文档移动到文件夹 - Using Powershell to move documents to a folder based on file name Powershell:提取文件名的一部分以创建文件夹并将文件移动到文件夹中 - Powershell: Extracting parts of file name to create folders and move files into the folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM