Jump to content

[Solved] How to check a file is a Plain text file?


Recommended Posts

My script shall have to converting all text files to UTF-8 and Line Breaks into CRLF!
The problem is that if the file is not a text file it will be broken!

#include <File.au3>
Local $hOpen, $sNewContent, $sLine
Local $sFileList = _FileListToArrayRec(@ScriptDir, "*.*", 1, 1, 1, 2)
If IsArray($sFileList) Then
    For $i = 1 To $sFileList[0] - 1
        ConsoleWrite($sFileList[$i] & " >" & FileSetAttrib($sFileList[$i], "-RASHNOT") & @CRLF)
        $sNewContent = ""
        $sLine = FileReadToArray($sFileList[$i])
        If IsArray($sLine) Then
            For $x = 0 To UBound($sLine) - 1
                $sNewContent &= $sLine[$x] & @CRLF
            Next
        EndIf
        $hOpen = FileOpen($sFileList[$i], 2 + 256)
        FileWrite($hOpen, $sNewContent)
        FileClose($hOpen)
    Next
EndIf

The file is treated as plain text like: .txt, .ini, .inf, .csv, .css, .js, .html, .cpp, .h, .sln, .rc, .cs .......
The files are not regarded as plain text like: .doc, .rtf, .obj, .res,  pch, .suo, .lib, .pdf, .jpg, .........

The problem is that the content can not be determined through the extension of the file!

Any ideas? Thanks

Edited by Trong

Regards,
 

Link to comment
Share on other sites

By reading it in binary and testing the bytes for characters outside of parameters? My eof thread from last week was solved with something similar.

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...