Searching for all snippets in .xml topic (recursive)

This section is for programmers. Please use it for all discussons on interfacing help with your applications and related subjects.

Moderators: Alexander Halser, Michael Schwarzl

Post Reply
michal_novomesky
Posts: 62
Joined: Thu Jan 12, 2012 2:21 pm
Contact:

Searching for all snippets in .xml topic (recursive)

Unread post by michal_novomesky »

I made a .ps1 script to help identifying all snippets.

Code: Select all

param(
	[Parameter()]
	[string]$topicFile
)

# Check if topicFile exists
if (!(Test-Path $topicFile -PathType Leaf)) {
	Write-Host "Document $topicFile in Topics does not exist" -ForegroundColor Red
	Exit
}

function Snippets {
    param (
		[Parameter()]
		$path,
		[Parameter()]
		$topicFile,
		[Parameter()]
		$depth
	)
	if (!(Test-Path $topicFile -PathType Leaf)) {
		Write-Host "Document $topicFile in Topics does not exist" -ForegroundColor Red
		Exit
	}
	# Load document to XML object
	try {
		[xml] $topic = Get-Content -Path $topicFile
	} catch {
		Write-Host "Document $topicFile parsing failed" -ForegroundColor Red
		Exit
	}
	$path = Split-Path -Path $topicFile
	$depth += 1
	$topic.SelectNodes("//snippet") | ForEach-Object {
		$snippetFile = $path
		$snippetFile += "\"
		$snippetFile += $_.src
		$snippetFile += ".xml"
		for ($i = 1; $i -lt $depth; $i++) {
			Write-Host "  " -NoNewline
		}
		Write-Host $_.src -NoNewline
		Write-Host ".xml"
		Snippets -topicFile $snippetFile -depth $depth
	}
}

$topic = Split-Path -Path $topicFile -Leaf
Echo "Snippets in $topic"
Snippets -topicFile $topicFile -depth 0
Output from Windows CMD:

Code: Select all

C:\scripts\>powershell.exe -File snippets_in_topic.ps1 -topicFile C:\HMproject\Topics\business_unmanaged_windows8_cantupgrade.xml
Snippets in business_unmanaged_windows8_cantupgrade.xml
snippet_ifcantupgrade_w8.xml
snippet_do_order_normal.xml
snippet_upgrade_endpoint_group_olderos.xml
  snippet_upgrade_endpoint_manually.xml
  snippet_dwn_endpoint_olderos.xml
  snippet_upgrade_endpoint_manually_formore.xml
snippet_upgrade_to_w10_normal_w8.xml
  snippet_upgrade_to_w10_why.xml
User avatar
Tim Green
Site Admin
Posts: 23308
Joined: Mon Jun 24, 2002 9:11 am
Location: Bruehl, Germany
Contact:

Re: Searching for all snippets in .xml topic (recursive)

Unread post by Tim Green »

Hi Michal,

Nice, thanks for posting! 8)
Regards,
Tim (EC Software Documentation & User Support)

Private support:
Please do not email or PM me with private support requests -- post to the forum directly.
Post Reply