help need in getting folder path to query file name
-
Tuesday, March 20, 2012 6:53 AM
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
}
}
All Replies
-
Tuesday, March 20, 2012 7:12 AM
Try it this way instead.
Get-ChildItem -recurse e:\test2 | Where-Object {$_.name -match "madison"}
¯\_(ツ)_/¯
-
Tuesday, March 20, 2012 7:25 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:39 AMNo luck jrv, still its searching in the c:\scriptxml folder, instead the file is in C:\scriptxml\fold1
-
Tuesday, March 20, 2012 7:49 AMThe 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 1:34 PM
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:35 PM
WHat does versioninf hav eto dowith this?$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
¯\_(ツ)_/¯
-
Wednesday, March 21, 2012 3:26 AMThanks Shaba, it did the trick
-
Wednesday, March 21, 2012 4:44 AM
WHat does versioninf hav eto dowith this?$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
¯\_(ツ)_/¯
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 9:33 AM
WHat does versioninf hav eto dowith this?$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
¯\_(ツ)_/¯
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 12:15 PM
WHat does versioninf hav eto dowith this?$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
¯\_(ツ)_/¯
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:19 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:24 PMThanks for that tip JRV.
Shaba

