gpt4 book ai didi

csv - 简单脚本似乎永远无输出运行?

转载 作者:行者123 更新时间:2023-12-03 01:13:54 25 4
gpt4 key购买 nike

我创建了一个简单的PowerShell脚本,以更改一组CSV文件中一列的标题值。当我在包含25个CSV文件的测试文件夹上以.ps1文件形式启动脚本时,该脚本似乎可以运行(出现闪烁的光标),但是它已经运行了一个多小时,并且到目前为止还没有任何输出文件出现。

有人可以指出这里可能出什么问题吗?过去,我已经在这台计算机上成功编写并执行了几个PowerShell脚本,但是从未遇到过此问题,并且搜索没有产生任何结果。

#Rename the first Column header in each output file to 'VZA' so you can work
#with the data

#Iterate code through 211 possible files
$i = 1
While ($i -le 211) {
#Set the variable to the filename with the iteration number
$filename = "c:\zMFM\z550Output\20dSummer\20dSum550Output$i.csv"

#Check to see if that a file with $filename exists. If not, skip to the next
#iteration of $i. If so, run the code change the column header
If (Test-Path $filename) {
#Import the CSV and change the column header to VZA
Import-CSV $filename |
Select-Object @{ expression={_."550 1587600 VZA"}; label='VZA' } |
Export-Csv -NoType "c:\zMFM\z550Output\20dSummer\fixed20dSum550Output$i.csv"
}
}

编辑:
看来我错过了 $i++术语,并且代码现在可以正常运行,但是,输出仅包含 VZA header ,而没有来自导入的CSV文件的数据。我在哪里出错,我假设在 Select-Object代码的某个地方?

最佳答案

在您粘贴的脚本中,变量$i从未在循环内更改。在每次迭代中,它的初始值为1,始终小于211,并且while语句将永远不会退出。

需要明确的是,while语句不会修改循环变量本身。要从1到211进行计数,您需要在循环内递增变量,以使其最终到达末尾。

$i = 1
while($i -le 211) {
write-output "Running iteration $i"
# loop stuff here

# Increment the counter
$i += 1
}

或者,您可以使用 for循环的Powershell版本
1..211 | foreach {
write-output "Running iteration $_"
# loop stuff here
}

关于csv - 简单脚本似乎永远无输出运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34130153/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com