Jump to content
VIP

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

Recommended Posts

VIP

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,
 

Share this post


Link to post
Share on other sites
VIP

I've realized that the text file does not contain signatures!

I checked and the only signatures of coding!

Sign_Encoding.png

Edited by Trong

Regards,
 

Share this post


Link to post
Share on other sites
VIP

:>

Edited by Trong

Regards,
 

Share this post


Link to post
Share on other sites
iamtheky

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.


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

Share this post


Link to post
Share on other sites
kadanefewi
34 minutes ago, Trong said:

bump :>

Try reading all files in binary (will work for binary and text).

 

Local $hfile = FileOpen($filePath, 16)
    Local $data = FileRead($hfile)
    FileClose($filePath)

 

Share this post


Link to post
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

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.