gpt4 book ai didi

linux - 使用 powershell 在 linux 发行版中获取文件名和文件扩展名

转载 作者:太空宇宙 更新时间:2023-11-04 11:51:01 28 4
gpt4 key购买 nike

我在 CentOS 上使用以下脚本来获取文件扩展名和不带扩展名的文件名以及完整的文件名,它在 Windows 上运行良好,但是当我在 ubuntu 上使用它时它不起作用并且它向我显示了很多错误:

Method invocation failed because [System.IO.DirectoryInfo] does not contain a method named 'LastIndexOf'.

Method invocation failed because [System.IO.DirectoryInfo] does not contain a method named 'Substring'.

下面是我写的代码

$files = get-childitem /etc/ssl/certs
foreach ($file in $Files)
{
# Nom du fichier avec extension
$pos_last_anti_slash = $file.LastIndexOf("\")
$fullname = $files.Substring($pos_last_anti_slash+1)
# Extension du fichier
$pos_last_point = $file.LastIndexOf(".")
$extension = $file.Substring($pos_last_point+1)
Write-Host $extension

# Nom du fichier sans extension
$filename = $file.Substring($pos_last_anti_slash+1)
$pos_last_points = $filename.LastIndexOf(".")
$filename = $filename.Substring(0, $pos_last_points)
Write-Host $filename
if($filextension == "p7b")
{
openssl x509 -inform p7b -in /etc/ssl/certs/$fullname -out
/etc/ssl/certs/$filename.pem
$var = ((& openssl x509 -in $file -dates -noout) -match 'notAfter')

}
}

最佳答案

使用 Get-Member cmdlet 查看文件对象有哪些字段。我认为可能会对 FullName、BaseName、Extension 和 Name 感兴趣。

Get-ChildItem -File -Path '/etc/ssl/certs' | Get-Member

我还没有测试过这个,但它可能接近你正在寻找的东西。我确实用 -eq 替换了 ==

$certsdir = '/etc/ssl/certs'
$files = Get-ChildItem -File -Path $certsdir
foreach ($file in $files) {
# Nom du fichier avec extension
$fullname = $_.Name

# Extension du fichier
$extension = $_.Extension
Write-Host $extension

# Nom du fichier sans extension
$filename = $_.BaseName
Write-Host $filename

if ($filextension -eq ".p7b") {
openssl x509 -inform p7b -in $_.FullName -out $(Join-Path -Path $certsdir -ChildPath "$filename.pem")
$var = ((& openssl x509 -in $file -dates -noout) -match 'notAfter')
}
}

关于linux - 使用 powershell 在 linux 发行版中获取文件名和文件扩展名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56079099/

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