【发布时间】:2019-09-18 13:58:18
【问题描述】:
怎么样了?
我是 Powershell 的新手,我正在尝试简化我的代码,以便我需要在两个文件中执行相同的操作,唯一改变的是文件名和 ReadCount 大小(第一个文件为 15000第二个是 50000)。
当我运行它时,错误显示:
Get-Content : 指定路径的对象 C:\Folder\08_configuration_items 11_CI-Contract-new[0].csv 没有 存在,或已被 -Include 或 -Exclude 参数过滤。在 行:2 字符:7 + $i=0;获取内容 "C:\Folder\$fileArray[$len].csv" -ReadCount $sizeA ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (System.String[]:String[]) [Get-Content], 异常 + FullyQualifiedErrorId : ItemNotFound,Microsoft.PowerShell.Commands.GetContentCommand
Get-Content : 指定路径的对象 C:\Folder\08_configuration_items 11_CI-Contract-new[0]_1.csv 没有 存在,或已被 -Include 或 -Exclude 参数过滤。在 行:3 字符:20 + ... bookContent = Get-Content "C:\Folder\$fileArray[$len]_1.csv" |选择... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (System.String[]:String[]) [Get-Content], 异常 + FullyQualifiedErrorId : ItemNotFound,Microsoft.PowerShell.Commands.GetContentCommand
这是代码,不确定我是否在 Powershell 上使用正确的方式访问数组。
$sizeArray = @(15000,50000)
$fileArray = @("08_configuration_items", "11_CI-Contract-new")
for($len=0; $len -le 1; $len++) {
$i=0; Get-Content "C:\Folder\$fileArray[$len].csv" -ReadCount $sizeArray[$len] | %{$i++; $_ | Out-File "C:\Folder\$fileArray[$len]_$i.csv" -Encoding "UTF8"}
$WorkbookContent = Get-Content "C:\Folder\$fileArray[$len]_1.csv" | Select -Index 0
for($j=2; $j -le $i; $j++) {
$CurrentFileContent = Get-Content "C:\Folder\$fileArray[$len]_$j.csv"
@($WorkbookContent, $CurrentFileContent) | Set-Content "C:\Folder\$fileArray[$len]_$j.csv"
}
}
有什么想法吗?
非常感谢
【问题讨论】:
标签: arrays powershell file