gpt4 book ai didi

powershell - 从 for 内的字符串中删除字符

转载 作者:行者123 更新时间:2023-12-04 00:36:12 25 4
gpt4 key购买 nike

我试图通过删除最后 25 个字符来获取病毒的文件夹位置,我发现可以使用我已经测试过并且工作正常的这段代码来实现这一点

$string = "the scripts"
$string = $string.Substring(0,$string.Length-1)
$string

示例字符串

C:\Users\jose\Desktop\MUSICA\011_Leyendas\nlwdvakybapcybqkvcfwr\nlwdvakybapcybqkvcfwr.exeC:\Users\jose\Desktop\MUSICA\70'S, 80'S & 90'S.english\nlwdvakybapcybqkvcfwr\nlwdvakybapcybqkvcfwr.exeC:\Users\jose\Desktop\MUSICA\90\nlwdvakybapcybqkvcfwr\nlwdvakybapcybqkvcfwr.exeC:\Users\jose\Desktop\MUSICA\AlbertoBarro\nlwdvakybapcybqkvcfwr\nlwdvakybapcybqkvcfwr.exeC:\Users\jose\Desktop\MUSICA\BACHATA\nlwdvakybapcybqkvcfwr\nlwdvakybapcybqkvcfwr.exeC:\Users\jose\Desktop\MUSICA\CHICO CHE MEZCLADO\nlwdvakybapcybqkvcfwr\nlwdvakybapcybqkvcfwr.exe         C:\Users\jose\Desktop\MUSICA\joan sebastian - M. A. Solís\nlwdvakybapcybqkvcfwr\nlwdvakybapcybqkvcfwr.exeC:\Users\jose\Desktop\MUSICA\JUAN GABRIEL 1 DUOS\nlwdvakybapcybqkvcfwr\nlwdvakybapcybqkvcfwr.exe

While trying to do it within a FOR the substring method doesn't cut the string correctly, some idea of why is not working any these sample code

$viruslocation = Get-Content -Path .\Desktop\virus.txt

for ($i = 1; $i -lt $viruslocation.Count; $i++) {
$Length =$viruslocation[$i]
$tem= $Length.Substring(0,$Length.Length-22)
write $tem
}

for ($i = 1; $i -lt $viruslocation.Count; $i++) {
$tem= $viruslocation[$i].Substring(0,$viruslocation[$i].Length-22)
write $tem
}

预期结果

C:\Users\jose\Desktop\MUSICA\011_Leyendas\nlwdvakybapcybqkvcfwr

最佳答案

GetDirectoryName 是您要查找的内容:

$virusLocations = 'C:\Users\jose\Desktop\MUSICA\011_Leyendas\nlwdvakybapcybqkvcfwr\nlwdvakybapcybqkvcfwr.exe',
'C:\Users\jose\Desktop\MUSICA\70''S, 80''S & 90''S.english\nlwdvakybapcybqkvcfwr\nlwdvakybapcybqkvcfwr.exe',
'C:\Users\jose\Desktop\MUSICA\90\nlwdvakybapcybqkvcfwr\nlwdvakybapcybqkvcfwr.exe',
'C:\Users\jose\Desktop\MUSICA\AlbertoBarro\nlwdvakybapcybqkvcfwr\nlwdvakybapcybqkvcfwr.exe',
'C:\Users\jose\Desktop\MUSICA\BACHATA\nlwdvakybapcybqkvcfwr\nlwdvakybapcybqkvcfwr.exe',
'C:\Users\jose\Desktop\MUSICA\CHICO CHE MEZCLADO\nlwdvakybapcybqkvcfwr\nlwdvakybapcybqkvcfwr.exe',
'C:\Users\jose\Desktop\MUSICA\joan sebastian - M. A. Solís\nlwdvakybapcybqkvcfwr\nlwdvakybapcybqkvcfwr.exe',
'C:\Users\jose\Desktop\MUSICA\JUAN GABRIEL 1 DUOS\nlwdvakybapcybqkvcfwr\nlwdvakybapcybqkvcfwr.exe'

$virusLocations | % {
[System.IO.Path]::GetDirectoryName($_)
}

关于powershell - 从 for 内的字符串中删除字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52753195/

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