-1

I am pretty new to scripting and I have been stuck on this problem.

I want to extract a zip file and in that zip file there is a xml file called file.xml which I need to extract information from.

The file is huge and I only need to extract info between two tags.

<Report name="result\_many fail" summary="20" yes=19 no="1" finished=20> 

I need to extract information between the tags name and finished and save it to a txt file. The txt file should look like this:

name result_many fail, summary 20, yes 19 , no  1, finished 20

The problem is that it unzips to the right destination folder but it doesn't save anything into the result.txt file. My txt file is always empty.

This is my code:

echo "unzipping file..."
powershell "Expand-Archive C:\path_to_zip_file -Destinationpath C:\path_to_to_destination_folder;
$Path = “C:\path_to_to_destination_folder\file.xml;”
Select-Xml -XPath '//Report[@finished]').Node.InnerText;
Set-Content -Path 'C:\path_to_to_destination_folder\result.txt'

@echo "done"

Could someone help me out?

konoha
  • 57
  • 1
  • 8
  • Is this the code you are actually using? It doesn't even run – SE1986 Oct 19 '21 at 21:51
  • You are missing to set the `-Path` to your source xml document. See the [Select-Xml](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-xml?view=powershell-7.1) docs. – Filburt Oct 19 '21 at 21:52
  • @SEarle1986 yeah it runs , it just extracts correctly and then stops – konoha Oct 20 '21 at 07:28
  • it failed when I tried. There is a syntax error on this line as you have a `)` `Select-Xml -XPath '//Report[@finished]').Node.InnerText;` – SE1986 Oct 20 '21 at 09:23

1 Answers1

0

Leaving aside incidental aspects of your question (seemingly calling from cmd.exe with a broken attempt to pass a multi-line command string, invalid sample XML, lack of a file argument to Select-Xml, lack of input to the Set-Content cmdlet):

$path = 'C:\path_to_to_destination_folder\file.xml'
(Select-Xml '//Report[@finished]' $path).
  Node.Attributes.ForEach({ $_.Name + ' ' + $_.Value }) -join ', ' |
    Set-Content C:\path_to_to_destination_folder\result.txt

To call your PowerShell code from cmd.exe (a batch file) requires you to either pass it on a single line or to carefully spread it across multiple lines with line-continuations, using ^, based on the techniques described in this answer:

@echo off

echo unzipping file...

powershell -c Expand-Archive C:\path_to_zip_file -DestinationPath C:\path_to_to_destination_folder; ^
  $path = 'C:\path_to_to_destination_folder\file.xml'; ^
  (Select-Xml '//Report[@finished]' $path). ^
    Node.Attributes.ForEach({ $_.Name + ' ' + $_.Value }) -join ', ' ^| ^
      Set-Content C:\path_to_to_destination_folder\result.txt

echo done

If you need help with the fundamentals, here are some PowerShell learning resources:

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • i forgot to add `"Expand-Archive"`after the powershell keyword but it unzips, the result.txt file is still empty even with your code – konoha Oct 20 '21 at 07:39
  • @konoha, that applies that your XPath query doesn't find anything. Note that your XML snippet is invalid, because some attributes lack quoting. Here's an example of what would work: `Select-Xml '//Report[@finished]' -Content ''` – mklement0 Oct 20 '21 at 14:07
  • Also note that if you're really calling from `cmd.exe`, even your corrected command won't work, because you cannot pass multi-line commands without using line-continuation with `^`. And the lack-of-input problems hinted at in this answer persist. – mklement0 Oct 20 '21 at 14:08
  • are there any resources I could look up to do this correctly? is there really a lot missing ? – konoha Oct 20 '21 at 15:49
  • 1
    thanks a lot ! I will read trough the articles and try to understand them – konoha Oct 20 '21 at 19:49