Results 1 to 5 of 5
  1. #1
    Lounger
    Join Date
    Jan 2014
    Posts
    30
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Batch file findstr

    Hi,

    Code:
    C:\Allxmls>findstr /s /m "My string" *.xml
    This will query all the xml files and once 'My String' is located it will display the file name on the console, i need one more tag from the xml file if My string is found from that particular file <ClientId>123456</ClientId> on the console. Any ways of doing that at once.

    Thanks in advance.

  2. #2
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,434
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    John,

    You can do it in PowerShell. It would be easier if I had the XML Schema but since I didn't I created a script that didn't require that info.

    Code:
    <#+---------------------------------------------------------------------------+
      | PowerShell Pgm: TestXML.ps1                                          |
      | Programmed By : The Computer Mentor aka Retired Geek aka  |
      | Created       : 24 Jul 2016                                              |
      | Last Updated  :                                                           |
      | Current Ver.  : 1.0                                                       |
      +---------------------------------------------------------------------------+
    #>
    
    param (
     [Parameter(Mandatory=$True)]
       [String]$SrchStr,
     [Parameter(Mandatory=$True)]
       [String] $TagName,
     [Parameter(Mandatory=$False)]
       [String]$DriveDirPath = "G:\BEKDocs\TaskSchedulerExports"
    )
    
    Function Get-TAG {
    
    Param (
         [Parameter(Mandatory=$True)]
           [String] $TAG
    )
          $TAG = $TAG.Trim(" ")
          $TAG = $TAG.Trim("<"+$TagName+">")
          $TAG = $TAG.Trim("</"+$TagName+">")
    
      "$TAG"
    }
    
    $SrchMatched = 0
    $TagRetrieved = 0
    
    $MFiles = Get-ChildItem -path "$($DriveDirPath)\*.xml"
           
    ForEach ($f in $MFiles) {
       $x = get-content -path $f 
       $Found = $x | Select-String -pattern  "$SrchStr"
       If ($Found -ne $null) {
    
         $SrchMatched++
         $RawTag = $x | Select-String -Pattern $("<" + $TagName + ">")
    
         If ($RawTag -ne $null) {
           $TagRetrieved++
           "$($f.fullname) $($TagName): $(Get-TAG -TAG $RawTAG[0]) "
         } #End If ($RawT...
    
       } #End If ($Found...
    }
    
    "$($MFiles.Count)`tFile(s) were processed."
    "$($SrchMatched)`tFile(s) matched Search String."
    "$($TagRetrieved)`tFiles(s) retrieved tags."
    Sample Run:
    Code:
    PS> G:\BEKDocs\Scripts\Test.XML.ps1
    cmdlet Test.XML.ps1 at command pipeline position 1
    Supply values for the following parameters:
    SrchStr: cmd.exe
    TagName: Author
    G:\BEKDocs\TaskSchedulerExports\Administrator CMD Prompt.xml Author: DellXPS8700
    \Bruce 
    G:\BEKDocs\TaskSchedulerExports\Backup Documents to N.xml Author: DellXPS8700\Br
    uce 
    G:\BEKDocs\TaskSchedulerExports\RegEdit - System Level.xml Author: DellXPS8700\B
    ruce 
    G:\BEKDocs\TaskSchedulerExports\System Level CMD Prompt.xml Author: DellXPS8700\
    Bruce 
    44	File(s) were processed.
    4	File(s) matched Search String.
    4	Files(s) retrieved tags.
    
    PS>
    Note: the only .xml files I had were exported Task Scheduler items so that's what I used.

    The code requires the Search string and the XML Tag Name of the value to return w/o the <>.

    You can set the default path to search by changing it in the code. It can also be over ridden by passing it in as a parameter.

    To call the program from the PowerShell Command Window you would type:
    d:\pathtofile\TextXML.ps1 -SrchStr "cmd.exe" -TagName "Author" -Path "d:\path"

    Replacing the blue items above with data consistent with your configuration.

    HTH
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  3. #3
    Lounger
    Join Date
    Jan 2014
    Posts
    30
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Hi,

    Thanks RG for the script, on testing i encountered this error message.
    Unable to index into an object of type Microsoft.PowerShell.Commands.MatchInfo.
    At C:\ALLXML\Test.XML.ps1:48 char:22
    + Get-TAG -TAG $RawTAG[ <<<< 0]
    + CategoryInfo : InvalidOperation: (0:Int32) [], RuntimeException
    + FullyQualifiedErrorId : CannotIndex
    My C:\Allxml folder consists of multiple sub folders the script does not query the sub-folder.

    C:\Allxml--root folder
    folder1--xml files folder2--xml files folder3--xml files
    folder2--xml files folder2--xml files
    folder3--xml files
    xml files
    xml files
    .....

    Thanks in advance.

  4. #4
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,434
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    John,

    Here's a revised script that will take care of the problems.

    Code:
    <#+---------------------------------------------------------------------------+
      | PowerShell Pgm: TestXML.ps1                                          |
      | Programmed By : The Computer Mentor aka Retired Geek aka Bruce E. Kriebel |
      | Created       : 24 Jul 2016                                              |
      | Last Updated  :                                                           |
      | Current Ver.  : 1.0                                                       |
      +---------------------------------------------------------------------------+
    #>
    
    param (
     [Parameter(Mandatory=$True)]
       [String]$SrchStr,
     [Parameter(Mandatory=$True)]
       [String] $TagName,
     [Parameter(Mandatory=$False)]
       [String]$DriveDirPath = "G:\BEKDocs\TaskSchedulerExports"
    )
    
    Function Get-TAG {
    
    Param (
         [Parameter(Mandatory=$True)]
           [String] $TAG
    )
          $TAG = $TAG.Trim(" ")
          $TAG = $TAG.Trim("<"+$TagName+">")
          $TAG = $TAG.Trim("</"+$TagName+">")
    
      "$TAG"
    }
    
    $SrchMatched = 0
    $TagRetrieved = 0
    
    $MFiles = Get-ChildItem -recurse -path "$($DriveDirPath)\*.xml"
           
    ForEach ($f in $MFiles) {
       $x = get-content -path $f 
       $Found = $x | Select-String -pattern  "$SrchStr"
       If ($Found -ne $null) {
    
         $SrchMatched++
         $RawTag = $x | Select-String -Pattern $("<" + $TagName + ">")
    
         If ($RawTag -ne $null) {
           $TagRetrieved++
           "$($f.fullname) $($TagName): $(Get-TAG -TAG $RawTAG) "
         } #End If ($RawT...
    
       } #End If ($Found...
    }
    
    "$($MFiles.Count)`tFile(s) were processed."
    "$($SrchMatched)`tFile(s) matched Search String."
    "$($TagRetrieved)`tFiles(s) retrieved tags."
    FYI: the [0] was in case there were multiple occurrences of the Tag in the XML file. It didn't cause problems for me but then that's code.
    I don't know if your XML files have recurring tags but if so it will be interesting to see what happens.

    If this doesn't work I'll need a zip file with some sample files so I can see what is going on.

    Another thought you might want to try running PowerShell as Admin just in case you don't have access rights to the file as a standard user.

    HTH
    Last edited by RetiredGeek; 2016-07-25 at 09:35.
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  5. #5
    Lounger
    Join Date
    Jan 2014
    Posts
    30
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Hi,

    Thanks RG.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •