#powershell #csv
Вопрос:
я читаю в файлах csv с несколькими строками информации, но мой код выполняет только первую строку содержимого csv. если я просто хочу распечатать его с помощью write-host $dataFilesJS
он отлично работает для всего содержимого csv-файла. Но если я хочу выполнить другой код, он просто зацикливается один раз.
мой исходный код:
$CSV= Import-Csv \testtest.csv -Delimiter ';'
$csvFiles = $CSV
$csvFiles.Count
for($i=0;$i -lt $csvFiles.count;$i ){
$filepathpdf = $csvFiles.filePathPdf[$i]
$filepathattachments = $csvFiles.filePathAttachments[$i]
$dataFilesJS= $csvFiles.dataFilesJS[$i]
$jsonFile= $csvFiles.jsonFile[$i]
$destinationFolderSave=$csvFiles.destinationFolderSave[$i]
$kategorieName=$csvFiles.kategorieName[$i]
New-Item -Path $destinationFolderSave -ItemType Directory
$foldersaves="_#_"
$sidepaths = New-Object System.Collections.Generic.List[System.Object]
$sidepathsAttachments= New-Object System.Collections.Generic.List[System.Object]
$children = Get-ChildItem $filepathpdf
$childrenAttachments= Get-ChildItem $filepathattachments
$childrensss = Get-ChildItem $filepathpdf/$children
$childrensssAttachments = Get-ChildItem $filepathattachments/$childrenAttachments
for($i=0;$i -le $childrensss.count;$i ){
$sidepaths.add($childrensss[$i])
}
for($i=0;$i -le $childrensssAttachments.count;$i ){
$sidepathsAttachments.add($childrensssAttachments[$i])
}
write-host $sidepathsAttachments
for($i=0;$i -lt $childrensss.count;$i ){
$childrenpath= $filepathpdf "/" $children "/" $sidepaths[$i]
$pdfchildren = Get-ChildItem $childrenpath
write-host $pdfchildren
Copy-Item $childrenpath/* -Destination $destinationFolderSave -Recurse -force
}
}
Комментарии:
1. [1] НЕ используйте одну и ту же переменную индекса во всех ваших
for
циклах. / / / / / [2] попробуйтеforeach
вместо этого использовать цикл. это позволяет избежать всего «встречного»… [ усмехается ]2. @Lee_Dailey большое спасибо, это сработало
3. мы вам очень рады! рад, что немного помог … [ усмехается ]
Ответ №1:
это сработало для меня:
$CSV= Import-Csv \testtest.csv -Delimiter ';'
$csvFiles = $CSV
$csvFiles.Count
foreach($i in $csvFiles){
$filepathpdf = $i.filePathPdf
$filepathattachments = $i.filePathAttachments
$dataFilesJS= $i.dataFilesJS
$jsonFile= $i.jsonFile
$destinationFolderSave=$i.destinationFolderSave
$kategorieName=$i.kategorieName
New-Item -Path $destinationFolderSave -ItemType Directory
$foldersaves="_#_"
$sidepaths = New-Object System.Collections.Generic.List[System.Object]
$sidepathsAttachments= New-Object System.Collections.Generic.List[System.Object]
$children = Get-ChildItem $filepathpdf
$childrenAttachments= Get-ChildItem $filepathattachments
$childrensss = Get-ChildItem $filepathpdf/$children
$childrensssAttachments = Get-ChildItem $filepathattachments/$childrenAttachments
for($i=0;$i -le $childrensss.count;$i ){
$sidepaths.add($childrensss[$i])
}
for($i=0;$i -le $childrensssAttachments.count;$i ){
$sidepathsAttachments.add($childrensssAttachments[$i])
}
write-host $sidepathsAttachments
for($i=0;$i -lt $childrensss.count;$i ){
$childrenpath= $filepathpdf "/" $children "/" $sidepaths[$i]
$pdfchildren = Get-ChildItem $childrenpath
write-host $pdfchildren
Copy-Item $childrenpath/* -Destination $destinationFolderSave -Recurse -force
}
}