用于根据源列表 (.txt) 移动文件的 Powershell 脚本 [英] Powershell script to move files based on a source list (.txt

查看:36
本文介绍了用于根据源列表 (.txt) 移动文件的 Powershell 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个目录中有数千个文件(.pdf、.xls、.doc),它们都有相似的命名约定(类型"始终是一个常量字符串,即:账单或发票);

I have thousands of files in a directory (.pdf, .xls, .doc) and they all have a similar naming convention (the "type" is always a constant string, ie: billing or invoice);

帐号_帐号_type.pdf

帐号_ 帐号_type.doc

accountname_ accountnumber_type.doc

账户名_账户号码_type.xls

手头的任务是接收一个随机的帐号和帐号列表(类型"始终是一个常数,即:账单、发票、运输或订单,它们的格式各不相同)并将它们从目录 A 移到目录 B.我可以将列表放入 .csv 文件中以匹配 accountname_accountnumber_type.

The task at hand is to receive a random list of accountnames and account numbers (the "type" is always a constant, ie: billing, invoice, shipping or order and they vary in format) and move them from Directory A into Directory B. I can get the list into a .csv file to match the accountname_accountnumber_type.

我一直在尝试创建一个 powershell 脚本来引用 accountname_accountnumber 并将这些项目从一个目录 A 移动到目录 B,但没有成功.

I have been trying to create a powershell script to reference the accountname_accountnumber and move those items from one directory A to directory B with no luck.

SAMPLE 我发现了一些更简单的东西,但我希望能够对其进行编辑以创建一个新的目的地,并且如果未从该列表中找到该文件,则不会停止.另外,如果我可以从 .txt 列表中选择这个,我认为这比粘贴所有内容更容易.

SAMPLE I found something a bit simpler, but I wanted to be able to edit this to create a new destination and not halt if the file is not found from this list. Also, if I could have this pick from a .txt list I think that would be easier than pasting everything.

$src_dir = "C:\DirA\"
$dst_dir = "D:\DirB-mm-dd-yyyy\" #This code requires the destination dir to already be there and I need to have the code output a new Directory, it could output based on date script run that would be amazing

$file_list = "accountname1_accountnumber001_type", #If I can select to import csv here
"accountname2_accountnumber002_type",
"accountname3_accountnumber003_type",
"accountname4_accountnumber004_type",
"accountname5_accountnumber005_type",
"accountname6_accountnumber006_type"

foreach ($file in $file_list) #This errors out and stops the script if the file is not located in source and I need to have the script continue and move on, with hopefully an error output
{
  move-Item $src_dir$file $dst_dir
}

它们可以是任何文件格式,我试图让代码只匹配帐户名和帐号,因为这两个将定义确切的客户.无论是发票、账单还是运输都无关紧要,因为他们希望移动与该客户相关的所有文件.

They can be any file format, I am trying to get the code to match ONLY the accountname and accountnumber since those two will define the exact customer. Whether it is invoice, billing or shipping doesn't matter since they want all files associated with that customer moved.

例如,每个帐户可能有 4 个,并且类型格式可能与 pdf、doc 和 xls 不同,我需要根据前两个指标(帐户名、帐户号)移动所有文件.

For Example there could be 4 of each for every account and the type format may vary from pdf, doc and xls, I need to move all files based on their first two indicators (accountname,accountnumber).

alice_001_invoice.pdf

alice_001_invoice.pdf

alice_001_billing.doc

alice_001_billing.doc

alice_001_shipping.pdf

alice_001_shipping.pdf

alice_001_order.xls

alice_001_order.xls

George_245_invoice.pdf

George_245_invoice.pdf

George_245_billing.doc

George_245_billing.doc

George_245_shipping.pdf

George_245_shipping.pdf

George_245_order.xls

George_245_order.xls

Bob_876_invoice.pdf

Bob_876_invoice.pdf

Bob_876_billing.doc

Bob_876_billing.doc

Bob_876_shipping.pdf

Bob_876_shipping.pdf

Bob_876_order.xls

Bob_876_order.xls

Horman_482_invoice.pdf

Horman_482_invoice.pdf

Horman_482_billing.doc

Horman_482_billing.doc

Horman_482_shipping.pdf

Horman_482_shipping.pdf

Horman_482_order.xls

Horman_482_order.xls

CSV:

账号、账号

爱丽丝,001

乔治,245

鲍勃,876

霍曼,482

推荐答案

这个怎么样:

$CurrentDate = [DateTime]::Now.ToString("MM-dd-yyyy")
$DestinationDir = "D:\DirB-$CurrentDate"
New-Item $DestinationDir -ItemType Directory -ErrorAction SilentlyContinue

$AccountToMove = Import-CSV $CSVPath
Foreach ( $Account In $AccountToMove ){
    $FilePattern = "*$($Account.AccountName)*$($Account.AccountNumber)*"
    ls $SourceDir | Where Name -like $FilePattern | Move-Item -Destination $DestinationDir
}

这篇关于用于根据源列表 (.txt) 移动文件的 Powershell 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆