Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to edit file extensions across multiple subfolders using powershell or other

got a really daft problem. I have an unzipped file full of subfolders and in each one I have a few text documents with the extension ".log"
I need to search through all these docs for a particular word. When using the explorer search function I get nothing unless I change extension manually to .txt – then it works.
Problem is, there are a lot of sub folders and a lot of these zip files. I used to be able to change extensions in a single folder using cmd prompt but don’t know how to do this over multiple sub folders and in powershell.
Is there an easy way to do this?

Cheers

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Yes you can do this with Powershell, But this is not to rename your files from .log to .txt, Instead this will search for the keyword in all the log files available in the given directory. Also, you haven’t specified what you’re planning to do with the result.

$folder = "D:\Temp\" #Folder path to search files
$keyWord = "Error" #Keyword to search
$FileFilter = "*.log" #File extension for filter and search

#Get the files according to filter. Recurse, but exclude directories
$files = Get-ChildItem -Path $folder -Include $filefilter -recurse | where {$_.PSIsContainer -eq $false}
foreach ($file in $files)
    {
        $result = $file | Select-String $keyWord
       if ($result)  #If Key word is found
            {
                Write-Output "Found match in file $($file.Name) ($($file.Directory))"
                $result
            }
    }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading