如何将复制文件划分为优先级以复制不同的文件

时间:2019-07-18 15:02:01

标签: arrays powershell directory creation

我正在尝试建立一种在Windows中自动创建作业编号及其所需零件的文件夹结构的方法。我无法使代码发挥作用,使我在其中有一部分的主文件夹,子文件夹和子文件夹中进行制作。我要做的事情是根据优先级将不同的部分转移到每个文件夹中

最多有7个优先级,因此需要除以有多少个零件,因此示例将有4个零件具有4个优先级。.(在此示例代码中,它创建了sales文件夹,然后在其下方放置了一个称为输送机的文件夹然后在编号为的id的输送机文件夹中放入4个文件夹,以根据优先级多少将四个不同的部分放入四个不同的文件夹中

尝试了潜水优先级,但由于我无法理解如何存储数组来执行即时通讯要求而无法使用

 powershell
$salesorder = read-host -prompt 'What is the customer name and salesorder number? (ex.. turnkey - 3335)'
$jobnumber = read-host -prompt 'what is the Plant folder plus job number? (ex... 930-12345)' 
$foldername = $jobnumber + " - " +  $salesorder 
#$conveyornumber = read-host -prompt "what is the number for conveyors? " + " - "
$priority = read-host -prompt "how many priorities are there?"




    ##************************* setting variables for second level basic Folder Structure *********************************

$partpath = '\\wal-file\public\sullivan\sully_temp_part.ipt'
$layout = 'D0100 - Layout'
$footprint = 'D0101 - Footprint'
$equipment = 'D01xx - Equipment'
$Guarding = 'D85xx - Guarding'
$conveyor = 'D60xx - Conveyors'

$platform = 'D90xx - Platforms'

#*************************** creating new variables based on folder name stylings *************************************

$workdir = new-item -path "C:\vault_workspace\Temp_vault\wendt\" -name $foldername -itemtype 'directory' -force
$layoutdir = new-item -path $workdir -name $layout -itemtype 'directory'
$layoutfilename = $layoutdir -join '.ipt'


#*************************** setting work directory input **************************************************************


new-item -path $layoutdir -name "$salesorder.ipt" -itemtype "file"
$footprintdir = new-item -path $workdir -name $footprint -itemtype 'directory'



$conveyordir = new-item -path $workdir -name $conveyor -itemtype 'directory'


#looping statement to create multiple folders 
$conveyorinput = read-host -prompt "how many conveyors need to be made?"

for ($i = 1; $i -le $conveyorinput; $i++){"{0:D2}" -f $number} { [system.io.directory]::CreateDirectory("$conveyordir\D51$I")}
start-sleep -seconds 5 
#creating multiple files 

$conveyorarray = Get-ChildItem $conveyordir | Where-Object {$_.PSChildname} |  Foreach-Object {$_.Name}
foreach ($path in $conveyorarray) {copy-item -path $partpath -destination "$conveyordir\$path\$jobnumber-4-$path-DUmmy-A0.ipt "}

我尝试了几件事,结果却不尽相同,没有达到我期望的目标。我试着处理它,但是我想不出一种方法来实现与即时消息类似的形式,如果我问某种形式的If语句可能会做到这一点,但是我将它放在哪里以及它将如何工作?

感谢您的帮助

2 个答案:

答案 0 :(得分:0)

这只是一个建议,您可以考虑添加功能以使代码易于阅读。我看不到你想要的。如果需要查看Write-Host发生了什么,还可以在输出中添加它吗?我不知道什么是卑鄙的样本,或与此相关的样本,所以我猜它是文件夹还是IPT文件?我无法阅读代码code-readability,因为就创建目录而言,它无法传达您的意图。如果您可以省略“样本示例”语言并使用小部件,我想我们会更好地理解它。比如我需要4个Widget文件供4个人查看?

    # powershell
$salesorder = read-host -prompt 'What is the customer name and salesorder number? (ex.. turnkey - 3335)'
$jobnumber = read-host -prompt 'what is the Plant folder plus job number? (ex... 930-12345)' 
$foldername = $jobnumber + " - " +  $salesorder 
#$conveyornumber = read-host -prompt "what is the number for conveyors? " + " - "
$priority = read-host -prompt "how many priorities are there?"




##************************* setting variables for second level basic Folder Structure *********************************

# Dunno what this file is or how it got here?
$partpath = '\\wal-file\public\sullivan\sully_temp_part.ipt'
$layout = 'D0100 - Layout'
$footprint = 'D0101 - Footprint'
$equipment = 'D01xx - Equipment' #Are these meant to be numbers or x character?
$Guarding = 'D85xx - Guarding'
$conveyor = 'D60xx - Conveyors'

$platform = 'D90xx - Platforms'

#*************************** creating new variables based on folder name stylings *************************************
$folderArray = @() # of things you want to keep track of
function CreateNewFolder() { 
    param( 
        $path = "C:\vault_workspace\Temp_vault\wendt\",
        $name = $foldername,
        [switch] $force
    )

    $newDirectory = new-item -path $path -name $name -itemtype 'directory' -force:$force
    Write-Host "Adding $name to FolderArray"
    $folderArray.Add($newDirectory)

    return $newDirectory 

}
$workdir = CreateNewFolder -path "C:\vault_workspace\Temp_vault\wendt\" -name $foldername -force
#$workdir = new-item -path "C:\vault_workspace\Temp_vault\wendt\" -name $foldername -itemtype 'directory' -force
$layoutdir = CreateNewFolder -path $workdir -name $layout 
#$layoutdir = new-item -path $workdir -name $layout -itemtype 'directory'
$layoutfilename = $layoutdir -join '.ipt' # Why are we joining the directory to ipt?


#*************************** setting work directory input **************************************************************


new-item -path $layoutdir -name "$salesorder.ipt" -itemtype "file"
#$footprintdir = new-item -path $workdir -name $footprint -itemtype 'directory'
$footprintdir = CreateNewFolder -path $workdir -name $footprint 


$conveyordir = new-item -path $workdir -name $conveyor -itemtype 'directory'


#looping statement to create multiple folders 
$conveyorinput = read-host -prompt "how many conveyors need to be made?"

for ($i = 1; $i -le $conveyorinput; $i++){
    # This just gets printed out?
    "{0:D2}" -f $number
} 

{ 
    [system.io.directory]::CreateDirectory("$conveyordir\D51$I")
}
start-sleep -seconds 5 
#creating multiple files 

$conveyorarray = Get-ChildItem $conveyordir | Where-Object {
    $_.PSChildname
} |  Foreach-Object { 
    @{ 
        Name = $_.Name
        FullName = $_.FullName
    }
}
foreach ($path in $conveyorarray) {
    $name = $_.Name
    $fullName = $_.Name
    $destination = '{0}\{1}\{2}-4-{3}-Dummy-A0.ipt' -f $conveyordir, $name, $jobnumber, $path
    copy-item -path $fullName -destination $destination # "$conveyordir\$path\$jobnumber-4-$path-DUmmy-A0.ipt "
}

答案 1 :(得分:0)

所以我实际上完全改变了语法,但是我能够做到以下

$salesorder = read-host -prompt 'What is the customer name and salesorder number? (ex.. turnkey - 3335)'
$jobnumber = read-host -prompt 'what is the Plant folder plus job number? (ex... 930-12345)'
$topleveldir = new-item -path "C:\vault_workspace\Temp_vault\wendt\" -name "M$jobnumber - $salesorder" -ItemType 'directory'

$subfolders = "D0100 - Layout", "D0101 - Footprint", "D01xx - Equipment","D85xx - Guarding", "D60xx - Conveyors", "D90xx - Platforms", "D09XX - Chutes"

foreach($subfolder in $subfolders){new-item -path "$topleveldir" -name $subfolder -type directory }
#$partpath = '\\wal-file\public\sullivan\sully_temp_part.ipt'    

#p1
$p1conveyorinput = read-host -prompt "how many p1 conveyors need to be made?"

for ($i = 1; $i -le $p1conveyorinput; $i++) { 
$p1c = "{0:D2}" -f $i
[system.io.directory]::CreateDirectory("$topleveldir\D60xx - Conveyors\D61$p1c-p1")
copy-item -path "\\wal-file\public\sullivan\sully_temp_part.ipt" -destination "$topleveldir\D60xx - Conveyors\D61$p1c-p1\D61$p1c-p1-dummy.ipt" -force
}
start-sleep -seconds 1

我遇到的唯一问题是如何循环回到最后一条语句

#p1
$p1conveyorinput = read-host -prompt "how many p1 conveyors need to be made?"

for ($i = 1; $i -le $p1conveyorinput; $i++) { 
$p1c = "{0:D2}" -f $i
[system.io.directory]::CreateDirectory("$topleveldir\D60xx - Conveyors\D61$p1c-p1")
copy-item -path "\\wal-file\public\sullivan\sully_temp_part.ipt" -destination "$topleveldir\D60xx - Conveyors\D61$p1c-p1\D61$p1c-p1-dummy.ipt" -force
}
start-sleep -seconds 1

在没有复制和更改变量的情况下,存在尽可能多的优先级(由于功能正常,我现在要这样做)