我的所有代码:
#read list filenames + filter#
$listname = Get-ChildItem 'Y:\Users\H\Documents\D\Game Of Throne' -Filter *.bmp
#Read list file txt#
$line = Get-Content 'Y:\Users\H\Documents\D\Game Of Throne\prod\recup.txt'
#account name#
$count_listname= $listname.count
#account line txt#
$count_line = $line.count
#number of max action (inf_list)#
$count= $count_listname, $count_line
$inf_list= ($count | measure -Min).Minimum
#Var file by file of folder#
$list_split= $listname
$list_split | foreach {
$list_split = $_ -split '*'
Write-Host $list_split []
}
#Var line by line of the textfile#
$line_split = $line
$line_split | foreach {
$line_split = $_ -split '*'
Write-Host $line_split []
}
#Select type to delete#
$erase=Read-Host "Enter the type to delete"
#Select number of line#
$nb = Read-Host "Enter the number line to add."
#Line of replace#
$list_input[$nb] = Read-Host "Line(s):"
#Boucle#
$i= 0
while ($i -le $nb-1) {
$list_input[$i]| rename-item -newname { $_.name -replace ( $erase , $list_input[$nb] )}
$i++
}
#output#
echo "File folder" $listname ""
echo "Fichier texte " $line ""
echo "Number of file in folder" $count_listname ""
echo "Number of line in txt" $count_line ""
echo "Number max of actions" $inf_list ""
echo "Line by line" $line_split ""
echo "List one by one" $list_split ""
echo "Type to delete" $erase ""
#echo table($line_split[n] or $line_split[n]#
我在自动化脚本(Read-Host)方面遇到了一些问题,这可以使用 $erase 来处理,但我也需要它来获取表格行数以及行数(没有文本文件)
哈姆敦
答案1
由于不清楚您想要实现什么,所以我只能向您展示如何编写易于理解的代码。
您的变量名使得脚本难以理解。此外,其中似乎还存在逻辑缺陷。
你要这个做什么?
$inf_list= ($count | measure -Min).最小值
和这个:
$list_split | foreach { $list_split = $_ -split '*' Write-Host $list_split []
}
这突然从何而来?list_input 变量到那时还没有被使用过。
$list_input[$nb]
这是一个函数,如果像这样调用,Do-Whatever -Verbose
将会输出你的图像和文件的行(我猜这就是你的拆分逻辑的用途):
Function Do-Whatever
{
[CmdletBinding()]
param(
$ImagePath = 'Y:\Users\H\Documents\D\Game Of Throne',
$ImageFilter = '*.bmp',
$SomeList = 'Y:\Users\H\Documents\D\Game Of Throne\prod\recup.txt'
)
$Images = Get-ChildItem $ImagePath -Filter $ImageFilter
$ListContents = Get-Content $SomeList
if ($PSBoundParameters['Verbose'])
{
Write-Verbose "Found Images: $($Images.count)"
foreach($Image in $Images) {Write-Verbose $Image.Fullname)
Write-Verbose "List entries count: $($ListContents.count)"
foreach($Line in $ListContents) { Write-Verbose $Line}
}
#[logic goes here]
}
答案2
$inf_列表:
我计算文件夹中的文件数量和文本文件中的行数,然后取循环中最小的值。
$list_split:我用它逐一制作文件夹中所有文件的表格。
$list_input:只是一个错误,不用担心
是的,这是正确的逻辑
实际上我的代码是这样工作的:
#read list filenames + filter#
$listname = Get-ChildItem 'Y:\Users\H\Documents\D\Game Of Throne' -Filter *.bmp
#Read list file txt#
$line = Get-Content 'Y:\Users\H\Documents\D\Game Of Throne\prod\recup.txt'
#count name#
$count_listname= $listname.count
#count line txt#
$count_line = $line.count
#number of max action (inf_list)#
$count= $count_listname, $count_line
$inf_list= ($count | measure -Min).Minimum #count min
#Var file by file of folder#
$list_split= $listname
$list_split | foreach {
$list_split = $_ -split '*'
}
#Var line by line of the textfile#
$line_split = $line
$line_split | foreach {
$line_split = $_ -split '*'
Write-Host $line_split []
}
#box delete#
$erase=Read-Host "Put the caracter to delete"
#loop#
$i= 0
while ($i -le $inf_list-1) {
$list_split[$i]| rename-item -newname { $_.name -replace ( $erase , $line_split[$i] )}
$i++
}
#output#
echo "File folder" $listname ""
echo "Fichier texte " $line ""
echo "Number of file in folder" $count_listname ""
echo "Number of line in txt" $count_line ""
echo "Number max of actions" $inf_list ""
echo "Line by line" $line_split ""
echo "List one by one" $list_split ""
echo "Type to delete" $erase "
然后我会尝试你的代码