Home > Back-end >  How to edit file extensions across multiple subfolders using powershell or other
How to edit file extensions across multiple subfolders using powershell or other

Time:10-14

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

CodePudding user response:

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
            }
    }
  • Related