none
help need in getting folder path to query file name

    Question

  • Hi

    I am trying to write a powershell script to query folder which has many folders and has xml file in each folder.

    Then from those XML file i am looking for particular word called "ERROR" .

    I am able to get the file name but there its searching the file in base folder and not on the particular sub-folder where the file actually is.

    Any help apprecaited.

    --

    this is the script

    $vfiles = Get-ChildItem -recurse c:\scriptxml | Where-Object {$_.name -like "*madison_*"}
    foreach( $afile in $vfiles)
    {

    [xml]$ufile = Get-Content $afile
    foreach( $user in $ufile.lot.maps | where {$_.level -match "ERROR"})
    {
        write-host $user.param.goal
    }
    }

    Tuesday, March 20, 2012 6:53 AM

Answers

  • $tmp = Get-ChildItem -Recurse -Filter "*.xml" |select versioninfo $len = $tmp.length for ($i=0; $i -lt $len; $i++) { $pth = $tmp[$i].versioninfo|select filename Get-content $pth.filename

    }


    After get-content, you can insert your code.

    Shaba

    • Marked as answer by washta Wednesday, March 21, 2012 3:25 AM
    Tuesday, March 20, 2012 7:25 AM

All replies

  • Try it this way instead.

     

    Get-ChildItem -recurse e:\test2 | Where-Object {$_.name -match "madison"}


    ¯\_(ツ)_/¯

    Tuesday, March 20, 2012 7:12 AM
  • $tmp = Get-ChildItem -Recurse -Filter "*.xml" |select versioninfo $len = $tmp.length for ($i=0; $i -lt $len; $i++) { $pth = $tmp[$i].versioninfo|select filename Get-content $pth.filename

    }


    After get-content, you can insert your code.

    Shaba

    • Marked as answer by washta Wednesday, March 21, 2012 3:25 AM
    Tuesday, March 20, 2012 7:25 AM
  • No luck jrv, still its searching in the c:\scriptxml folder, instead the file is in C:\scriptxml\fold1
    Tuesday, March 20, 2012 7:39 AM
  • The line jrv gave you actually does search recursively and should also find files in deeper folder. If the command is not working can you show what your folder structure looks like or describe it further as the command jrv gave you does exactly what you requested. Just as your own command for that matter. Both -match "madison" and -like "*madison*" find the same data. So if it does not find the files you are looking for something else is wrong.
    Tuesday, March 20, 2012 7:49 AM
    Moderator
  • No luck jrv, still its searching in the c:\scriptxml folder, instead the file is in C:\scriptxml\fold1

    It does exactly what you asked for.

    Are yousaying you don't want to search the root folder but ONLY the subfolders?


    ¯\_(ツ)_/¯

    Tuesday, March 20, 2012 1:34 PM
  • $tmp = Get-ChildItem -Recurse -Filter "*.xml" |select versioninfo $len = $tmp.length for ($i=0; $i -lt $len; $i++) { $pth = $tmp[$i].versioninfo|select filename Get-content $pth.filename

    }


    After get-content, you can insert your code.

    Shaba

    WHat does versioninf hav eto dowith this?


    ¯\_(ツ)_/¯

    Tuesday, March 20, 2012 1:35 PM
  • Thanks Shaba, it did the trick
    Wednesday, March 21, 2012 3:26 AM
  • $tmp = Get-ChildItem -Recurse -Filter "*.xml" |select versioninfo $len = $tmp.length for ($i=0; $i -lt $len; $i++) { $pth = $tmp[$i].versioninfo|select filename Get-content $pth.filename

    }


    After get-content, you can insert your code.

    Shaba

    WHat does versioninf hav eto dowith this?


    ¯\_(ツ)_/¯

    JRV,

    My idea was to make use of versioninfo which will have the full path towards a file.

    Seems that worked !

    Cheers

    Shaba


    Shaba

    Wednesday, March 21, 2012 4:44 AM
  • $tmp = Get-ChildItem -Recurse -Filter "*.xml" |select versioninfo $len = $tmp.length for ($i=0; $i -lt $len; $i++) { $pth = $tmp[$i].versioninfo|select filename Get-content $pth.filename

    }


    After get-content, you can insert your code.

    Shaba

    WHat does versioninf hav eto dowith this?


    ¯\_(ツ)_/¯

    JRV,

    My idea was to make use of versioninfo which will have the full path towards a file.

    Seems that worked !

    Cheers

    Shaba


    Shaba

    I think you need to spen a bit of timelearning PowerSHell.

    Get-ChildItem -recurse -Filter "*.xml" | ForEach-Object{get-Content $_.Fullname}

    'Fullname' is the full pathname of teh file.  VersionInfo may not work for all files and doe snot make a lot of sense in this case.

    You also don't need to count the files.  The enumerator does all of that for you.


    ¯\_(ツ)_/¯

    Wednesday, March 21, 2012 9:33 AM
  • $tmp = Get-ChildItem -Recurse -Filter "*.xml" |select versioninfo $len = $tmp.length for ($i=0; $i -lt $len; $i++) { $pth = $tmp[$i].versioninfo|select filename Get-content $pth.filename

    }


    After get-content, you can insert your code.

    Shaba

    WHat does versioninf hav eto dowith this?


    ¯\_(ツ)_/¯

    JRV,

    My idea was to make use of versioninfo which will have the full path towards a file.

    Seems that worked !

    Cheers

    Shaba


    Shaba

    I think you need to spen a bit of timelearning PowerSHell.

    Get-ChildItem -recurse -Filter "*.xml" | ForEach-Object{get-Content $_.Fullname}

    'Fullname' is the full pathname of teh file.  VersionInfo may not work for all files and doe snot make a lot of sense in this case.

    You also don't need to count the files.  The enumerator does all of that for you.


    ¯\_(ツ)_/¯

    Thanks for clarifying that, I was not aware. And as you mentioned, I am learning powershell and not yet an expert.

    Good luck !


    Shaba

    Wednesday, March 21, 2012 12:15 PM
  • Here is a basic of PowerSHell that might help.

    get-childitem < any single file> |Get-Member

    This will list all of teh members of an object.

    help get-member -full

    Satrt here: http://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx


    ¯\_(ツ)_/¯

    Wednesday, March 21, 2012 12:19 PM
  • Thanks for that tip JRV.

    Shaba

    Wednesday, March 21, 2012 12:24 PM