简体   繁体   English

Powershell:使用数组和gci

[英]Powershell: Using array and gci

thanks for help in advance. 谢谢你的帮助。 I am new to powershell, so sorry for noobish questions. 我是Powershell的新手,很抱歉提出这个笨拙的问题。

Problem: I import a csv with filenames to an array. 问题:我将带有文件名的csv导入到数组。 The array consists out of numbers (ie 95325866256) what are filenames. 该数组由数字(即95325866256)组成,它们是文件名。 My aim is to copy all files from a remote system with the IDs from the array. 我的目标是使用阵列中的ID从远程系统复制所有文件。 Here is what I tried: 这是我尝试过的:

$array_chipIDs = Import-csv "C:\Data\chipID.csv" -UseCulture
$Export = "C:\Data\Export"
Write-Host -ForegroundColor Cyan "Anzahl der IDs: "$array_chipIDs.count

foreach ($i in $array_chipIDs){
Write-Host -ForegroundColor Yellow $i 

 foreach ($chipID in $array_chipIDs) {  
       Get-ChildItem -Path $pfad1 -Recurse -Include *.sdf, *.idat | select FullName | Copy-Item -Path {$_.Fullname} $export  
    if ($chipID -eq ''){
   break
   }
       }

But I miss the Point to give the gci the actual array value (the filename). 但我想念Point给gci实际的数组值(文件名)。

Thanks for help! 感谢帮助!

You are doing it wrong. 你做错了。 You get the IDs from file and you loop through them but the files you are copying are from $pfad1 variable which is not defined anywhere by the way. 您可以从文件中获取ID,并循环浏览它们,但是要复制的文件来自$ pfad1变量,该变量在任何地方都没有定义。 You are also looping twice without a reason. 您也无缘无故地循环两次。

If you just want to copy files from one folder to another folder then use this example. 如果仅要将文件从一个文件夹复制到另一文件夹,请使用此示例。

 $source = "c:\sourcefolder"
 $destination = "c:\destinationfolder\"
 get-childitem -Recurse $source | Where {$_.PSIsContainer -eq $False} | 
     copy-item -Destination $destination

You can define the source and destination folders to be network paths as long as you have permissions on the folders. 您可以将源文件夹和目标文件夹定义为网络路径,只要您对该文件夹具有权限即可。

Now this will copy any files from source to destination. 现在,这会将所有文件从源复制到目标。 If you want to restrict particular type or extension of files then we can extend the above. 如果您想限制文件的特定类型或扩展名,那么我们可以扩展以上内容。

 $source = "c:\sourcefolder"
 $destination = "c:\destinationfolder\"
 $extensions = @("*.sdf","*.idat")
 foreach($e in $extensions){
     get-childitem -Recurse $source -Filter $e | Where {$_.PSIsContainer -eq $False} | 
         copy-item -Destination $destination

}

This code will then try and get only the extensions added to the array $extensions. 然后,此代码将尝试仅将扩展名添加到数组$ extensions中。 You can add more extensions. 您可以添加更多扩展名。

If you want to do it from the file like the excel then may be share a sample. 如果您想从像excel这样的文件中进行操作,则可以共享一个示例。 The file must have the correct file name. 该文件必须具有正确的文件名。 If it does not have the full path then you need to define the actual path in a variable like source. 如果没有完整路径,则需要在变量(例如source)中定义实际路径。 But can't say much unless you give us an example. 但是,除非您给我们提供示例,否则不能说太多。 Hope this helps. 希望这可以帮助。

The first loop was just to check the Import of my csv. 第一个循环只是检查我的csv的导入。 But here is now a working foreach-loop. 但是现在这是一个工作的foreach循环。

foreach($chipID in $array_chipIDs) {
   Get-ChildItem -Path $s_path -Recurse -Include "filetype" | Where-Object { $_.FullName -match $chipID.ChipID } | Copy-Item -Destination $export
    }

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

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