Home > Blockchain >  Find path after matching string archive found path to zip
Find path after matching string archive found path to zip

Time:10-03

I would like to build the powershell script to find a string in a configuration file, and use the path found after the match to copy this particular folder to a specified location. After all the folders I need are copied, I want to zip the folder.

The config file is an xml and the specific lines where I want to find the path are like these: <add key="TlgxDir" value="C:\myapplication\Tlgx" /> example of the config file: https://mega.nz/file/wfRQBLzD#S6DeYTSvDLeilG0Hl0fLwlO4rREhGQaj6G05dhbNchI

So for example my search value in the file "config.xml" is "TlgxDir". I want to copy the folder in the specified path behind it "c:\myapplication\tlgx" to a specified folder (e.g.C:\temp\backup). After I did this procedure for multiple folders I want to zip the destination folder (backup.zip)

I already tried some things but I'm not very familiar with PowerShell...

Thanks in advance to anyone who can help me with this question :)

CodePudding user response:

Just use the PowerShell xml parser:

$xml = [xml](Get-Content .\ui00.exe.config)
$xml.configuration.appSettings.add.where{ $_.key -eq 'TlgxDir' }.Value
C:\myapplication\Tlgx

CodePudding user response:

Ok you can do something like this:

#set target location
$targetPath = 'C:\temp\backup'
#Load xml
$xml = New-Object -TypeName xml
$xml.load([path])
#Find node by using xpath and store path 
$node = $xml.SelectSingleNode("//add[@key='TlgxDir']")
$sourceDirectory = $node.value
#Copy data
copy-item -Path $sourceDirectory -Destination $targetPath -Recurse -Force -Confirm:$false
#Zip Target
Compress-Archive -Path $targetPath -DestinationPath C:\temp\backup.Zip -Force -Confirm:$false
  • Related