Jump to content

Read Encoding


Recommended Posts

Hello,

I'm stuck, again. I'm not grasping why FileGetEncoding works on one folders contents and not others. Why it works partly on one system (The best I've seen) and on other systems it fails on any file. Likely I've made a mistake and probably I'm lucky it is working at all. I have a problem in that the only way I can search for a file is to identify it by its file name because people have named the file differently on many systems and they have even tampered with the file extension.

So I thought to see if it had any unique file encoding value that set itself apart from the rest of the files in the folder. As luck would have it, that has turned out to be the case; well, it looks that way because on the couple of systems that this works on, it is returned as 2048 and the other files are a mix of other encoding options. I just don't know where to go from here since the results are hit an miss and I have about 400 systems I need to do a software upgrade on. That upgrade requires me to backup and restore configuration files that are important to registers working. Here is what I have so far, nothing fancy. Any insight would be appreciated. I struggle to read behind the lines so I may not have inferred something from the help file correctly.

#RequireAdmin
#include <Array.au3>
#include <MsgBoxConstants.au3>

;Local $aArray_Base[1][2] = [["File Name","Encoding"]]

Local $aArray_Base[0][2]

    ;_ArrayDisplay($aArray_Base, "2D - Base array")


; Change working directory and FileSelectFolder doesn't change working directory on its own or leave it set if it does

FileChangeDir ( FileSelectFolder ( "Select Folder", "C:\" ))


; Define a variable to hold a search "handle" as we loop through the files in a directory

$FileSearch = FileFindFirstFile(@WorkingDir & "\*.*")

            ; Check if the search was successful
            If $FileSearch = -1 Then
                MsgBox(0, "Error", "No files/directories matched the search pattern")
                Exit
            EndIf

            ;A while loop where we identify each file in the directory, then read and get its file encoding, and then push the file name and encodinng into an arry

            While 1
                $AFile = FileFindNextFile($FileSearch)
                If @error Then ExitLoop
                ;Exclude some known directory names that should not be terminal file names
                If $AFile == "backup" Or $AFile == "log" Or $AFile == "OUT" Or $AFile == "TR" Then
            Else

                    ;Get encoding of file
                    $AFile_Encoding = FileGetEncoding ( "C:\CRS\" & $AFile)

                    ;Pass file and and encoding $result to a variable
                    $sFill = $AFile & '|' & $AFile_Encoding

                    ;Use that variable to add data to the array
                    _ArrayAdd($aArray_Base, $sFill)

                EndIf
            WEnd

            ; Close the search handle
            FileClose($FileSearch)

; Display the results to see what was found.
_ArrayDisplay($aArray_Base, "2D - Base array")

; On a few systems this will return an array with file encodings

; On many systems this will return a list of file names but encoding is all -1

; I do not understand encoding enough to understand why the latter happens

; The systems where this works the most have AutoIt installed so I don't

; Know if that has any bearing. Even on those systems it only works on

; a handful of folders, not all. Why would that be? 

; Antivirus is disabled and it was compiled as an admin and run as an admin.

 

Link to comment
Share on other sites

Can you post an example file where FileGetEncoding fails?

Which AutoIt version do you use?

Also beware of comparing strings with == instead of = (see help).

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Because I was having problems reading anything, I downloaded the current stable and beta from the site today and re-installed. My testing has been on current stable.

As far as posting an example file, that's a broad ask in that I could post any file. I've got one directory I can read files one one system and the same directory on another system returns all -1. On the same system that could read the directory, if I try another directory, I get all -1.

In my understanding, or expectation if you will, I should be able to select any folder on a Windows system provided I have read rights and this would return me an array of encodings for file in the folder. If you test 10 folders across your system and find that they all work; Say some from program files, some folder hanging off root, then you're lucky. I cannot. With such a broad range of failures, I am left thinking that something is left out during execution. Like, the FileGetEncoding code isn't available each time it is run from the editor and isn't making its way into the compiled executable even though I select include all UDFs.

I've tried this on SVR 2008R2, Win8.1, Win7 with the same results. I will change the == to see if that is creating a side effect.

Thanks for the feedback. Worth a try.

Link to comment
Share on other sites

And then the light came on. Your note caused me to see my immediate failing. A hard coded path from initial testing. Grrr. I will test and return results in a couple of minutes. Humbled.

Link to comment
Share on other sites

Don't worry, help forum is for asking and getting help. What's important is that the solution finally pops up.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

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...