首页 > 系统相关 >powershell遍历文件夹压缩以及编写生成k3配置

powershell遍历文件夹压缩以及编写生成k3配置

时间:2024-07-05 09:01:07浏览次数:19  
标签:AppendChild 遍历 kddItem xmlDoc k3 InnerText file CreateElement powershell

Add-Type -AssemblyName System.IO.Compression  
Add-Type -AssemblyName System.IO.Compression.FileSystem 
# 设置源文件夹和目标日志文件的路径  
$sourceFolder = "C:\myapp\bin"  
$destFolder = "C:\myapp\OutPut\"  
$logFilePath = "C:\myapp\log.xml"  

$configPath = "\ClientBin\CustomControl\WPF\" 

# 创建一个新的XML文档  
$xmlDoc = New-Object System.Xml.XmlDocument  
$xmlDeclaration = $xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", $null)  
$xmlRootElement = $xmlDoc.CreateElement("KDDownloadItems")  
$xmlDoc.AppendChild($xmlDeclaration)  
$xmlDoc.AppendChild($xmlRootElement)  
  
# 获取源文件夹中所有文件的路径  
$files = Get-ChildItem -Path $sourceFolder -File  
  
foreach ($file in $files) {  
    # 构造ZIP文件的路径(与源文件同目录,但扩展名为.kdz)  
    $zipFilePath = [System.IO.Path]::Combine($destFolder, $file.Name + ".kdz")  
	$fullName = $file.FullName
  
  
    & "C:\Program Files\7-Zip\7z.exe" a -tzip "$zipFilePath" "$fullName"


    # 创建KDDownloadItem节点  
    $kddItem = $xmlDoc.CreateElement("KDDownloadItem")  
    $xmlRootElement.AppendChild($kddItem)  
  
    # 添加子节点  
    $deployPath = $xmlDoc.CreateElement("DeployPath")  
    $kddItem.AppendChild($deployPath)  
  
    $versionInfo = $xmlDoc.CreateElement("VersionInfo")  
    $versionInfo.InnerText = (Get-Date).ToString("yyyy-MM-dd#HH:mm:ss") # 示例时间戳,您可以根据需要修改  
    $kddItem.AppendChild($versionInfo)  
  
    $currentVersionInfo = $xmlDoc.CreateElement("CurrentVersionInfo")  
    $kddItem.AppendChild($currentVersionInfo)  
  
    $sourcePath = $xmlDoc.CreateElement("SourcePath")  
    $sourcePath.InnerText = $configPath.Replace('\', '/') +  $file.Name + ".kdz" # XML通常使用正斜杠  
    $kddItem.AppendChild($sourcePath)  
  
    $itemName = $xmlDoc.CreateElement("ItemName")  
    $itemName.InnerText = $file.Name  
    $kddItem.AppendChild($itemName)  
  
    $extName = $xmlDoc.CreateElement("ExtName")  
    $extName.InnerText = ".kdz"  
    $kddItem.AppendChild($extName)  
  
    $svrDeployPath = $xmlDoc.CreateElement("SvrDeployPath")  
    $svrDeployPath.InnerText = $configPath # 示例路径,您可能需要根据实际情况调整  
    $kddItem.AppendChild($svrDeployPath)  
}  
  
# 保存XML文档  
$xmlDoc.Save($logFilePath)  
  
Write-Host "日志文件已生成:" $logFilePath

  

标签:AppendChild,遍历,kddItem,xmlDoc,k3,InnerText,file,CreateElement,powershell
From: https://www.cnblogs.com/coolyylu/p/18285025

相关文章