Home > Mobile >  Powershell command to identify all the Windows type and UNIX type text file
Powershell command to identify all the Windows type and UNIX type text file

Time:09-17

The GIT is messing up few files and it saves the unix based files in LF format in the system.

There is also few of the windows file that gets saved in CR LF format.

I need to differentiate between the UNIX based file and Windows based file.

I was able to successfully write the below code for 1 text file. The below returned TRUE as it is a windows file.

PS C:\Desktop\SecretSauce> (GET-CONTENT 'HIDEME.TXT' -raw) -match "\r\n$"

Question:

There are 1000's of files in different format(txt, cpp, hpp, sql) in both LF and CR LF format in the same location.

I need to get the output with the path of file, filename with extension and True (If it is CR LF) and False (if it is LF).

when I execute this command to check for multiple files, output is not returning any result.

(Get-Content -Path 'C:\Desktop\SecretSauce\*.*' -raw) -match "\r\n$"

What is the best approach for this using powershell ?

CodePudding user response:

It's an expensive approach, because each file is read in full, but the following should do what you want:

Get-ChildItem -File -Path H:\Desktop\Parent_Folder\Sub-Folder2\*.* |
  ForEach-Object {
    [pscustomobject] @{
      HasCRLF = (Get-Content -Raw -LiteralPath $_.FullName) -match '\r\n'
      Name = $_.Name
      FullName = $_.FullName
    }
  }

You'll see output such as the following:

HasCRLF Name     FullName
------- ----     --------
  False foo.txt  H:\Desktop\Parent_Folder\Sub-Folder2\foo.txt
   True bar.txt  H:\Desktop\Parent_Folder\Sub-Folder2\bar.txt
  • Related