繁体   English   中英

Powershell脚本基于源列表(.txt

[英]Powershell script to move files based on a source list (.txt

我在目录中有成千上万个文件(.pdf,.xls,.doc),并且它们都有类似的命名约定(“类型”始终是常量字符串,即:开票或发票);

ACCOUNTNAME _ ACCOUNTNUMBER _type.pdf

ACCOUNTNAME _ ACCOUNTNUMBER _type.doc

ACCOUNTNAME _ ACCOUNTNUMBER _type.xls

当前的任务是接收帐户名和帐号的随机列表(“类型”始终是常数,即:开票,发票,运输或订单,并且它们的格式有所不同)并将它们从目录A移到目录B。我可以将列表放入.csv文件中,以匹配accountname_accountnumber _type。

我一直在尝试创建一个Powershell脚本来引用accountname_accountnumber并将这些项目从一个目录A移到目录B,但没有运气。

样本我发现有点简单,但是我希望能够对其进行编辑以创建新的目的地,并且如果从该列表中找不到该文件,则不希望暂停。 另外,如果我可以从.txt列表中进行选择,我认为这比粘贴所有内容容易。

$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
}

它们可以是任何文件格式,我正在尝试获取仅匹配帐户名和帐户号的代码,因为这两个帐户将定义确切的客户。 无论是发票,开票还是运输都没有关系,因为他们希望移动与该客户关联的所有文件。

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

alice_001_invoice.pdf

alice_001_billing.doc

alice_001_shipping.pdf

alice_001_order.xls

George_245_invoice.pdf

George_245_billing.doc

George_245_shipping.pdf

George_245_order.xls

Bob_876_invoice.pdf

Bob_876_billing.doc

Bob_876_shipping.pdf

Bob_876_order.xls

Horman_482_invoice.pdf

Horman_482_billing.doc

Horman_482_shipping.pdf

Horman_482_order.xls

CSV:

帐号,帐号

爱丽丝001

乔治245

鲍勃876

霍曼482

关于将文件移动到子目录的代码部分(您已经从帖子中删除了)对您的业务规则没有多大意义。 由于您从不显示示例CSV文件内容,因此完全是在猜测。

为了简化处理,假设您获得了以下源文件。 编辑您的帖子以显示CSV文件的内容以及要将文件移动到的位置。

C:\some\path\A\Alice_001_bill.doc
C:\some\path\A\Alice_001_invoice.xls
C:\some\path\A\Bob_002_invoice.pdf
C:\some\path\A\Bob_002_invoice.doc
C:\some\path\A\Eve_003_bill.xls
C:\some\path\A\Eve_003_invoice.doc

这个怎么样 :

$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
}

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM