[英]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.