| View previous topic :: View next topic |
| Author |
Message |
toralf
Joined: 31 Jan 2005 Posts: 3841 Location: Bremen, Germany
|
Posted: Fri Apr 15, 2005 12:05 pm Post subject: How to distinguish between binary and ascii files? |
|
|
I want my script to treat binary files differently than ASCII files . How can I find out if a file contains binary or ASCII data? _________________ Ciao
toralf  |
|
| Back to top |
|
 |
BoBo Guest
|
Posted: Fri Apr 15, 2005 12:37 pm Post subject: |
|
|
| D'ya wanna transfer (FTP) your files via an AHK script ? |
|
| Back to top |
|
 |
toralf
Joined: 31 Jan 2005 Posts: 3841 Location: Bremen, Germany
|
Posted: Fri Apr 15, 2005 2:39 pm Post subject: |
|
|
No, i want to compress the files differently. _________________ Ciao
toralf  |
|
| Back to top |
|
 |
TeknoMusicMan
Joined: 14 Apr 2005 Posts: 188 Location: Wisconsin, USA
|
Posted: Fri Apr 15, 2005 2:44 pm Post subject: |
|
|
try using FileRead to read the file, then check it against a IF statement to see if any of the characters are other then 0 or 1, if not its binary, if it does its ascii.
unless you mean like executables and such, in that case would the .XXX extension be enough to tell you what it is. You could setup a list of binary extensions and a list of ascii extensions and filter them that way. |
|
| Back to top |
|
 |
Titan
Joined: 11 Aug 2004 Posts: 5390 Location: /b/
|
Posted: Fri Apr 15, 2005 3:50 pm Post subject: |
|
|
^ example:
| Code: | FileRead, FileVar, FILE.txt
nobin = 2,3,4,5,6,7,8,9,.
if FileVar is integer
{
if FileVar not contains %nobin%
Msgbox Binary file
else
Msgbox Not binary file: contains illegal digits
}
else
Msgbox Not binary file: contains ASCII (letters) |
_________________
 |
|
| Back to top |
|
 |
toralf
Joined: 31 Jan 2005 Posts: 3841 Location: Bremen, Germany
|
Posted: Fri Apr 15, 2005 3:58 pm Post subject: |
|
|
Thanks you all for your comments.
FileRead is not an option, since there are several hundered files with up to 3 GB (!) filesize. So a loop over all files will be too costly.
From all your post I assume the only way to do is to look directly at the contens. So I will try to read 5 or 10 of the first lines (do they exist in binary format?) and guess for the rest. _________________ Ciao
toralf  |
|
| Back to top |
|
 |
Titan
Joined: 11 Aug 2004 Posts: 5390 Location: /b/
|
Posted: Fri Apr 15, 2005 4:03 pm Post subject: |
|
|
Hmm, reading only a few random lines of a file would be a clever idea. Why couldn't you just take .bin files for binary? _________________
 |
|
| Back to top |
|
 |
Chris Site Admin
Joined: 02 Mar 2004 Posts: 10480
|
Posted: Fri Apr 15, 2005 4:07 pm Post subject: |
|
|
If it weren't for the fact that some of your files are so large, the following FileRead idea would probably work. But since they are so large, I think it's impossible to do it directly inside a script. You would probably need an external utility that scans the file for the first binary zero and stops there, reporting the file as binary.
Method that won't help for huge files, but you could use it for ones that pass a smallness filter:
Due to the fact that binary zeroes are used as string terminators in AutoHotkey, if you use FileRead to read in the file and StringLen says the variable's contents are lot shorter than FileGetSize says they should be, that probably means the file is binary.
Last edited by Chris on Fri Apr 15, 2005 4:07 pm; edited 1 time in total |
|
| Back to top |
|
 |
toralf
Joined: 31 Jan 2005 Posts: 3841 Location: Bremen, Germany
|
Posted: Fri Apr 15, 2005 4:07 pm Post subject: |
|
|
The files I need to process are created by another software that doesn't give extensions to them. _________________ Ciao
toralf  |
|
| Back to top |
|
 |
TeknoMusicMan
Joined: 14 Apr 2005 Posts: 188 Location: Wisconsin, USA
|
Posted: Fri Apr 15, 2005 4:47 pm Post subject: |
|
|
Does this software have any sort of naming standard for the files?
what i mean is there any pattern in the filenames or the binary files and non-binary files?
IE: 04152005bin or anything of that nature. If so you could read all the filenames in a directory into an array and read each name to see if it fits the pattern for a bin or non-bin |
|
| Back to top |
|
 |
BoBo Guest
|
Posted: Sat Apr 16, 2005 6:28 am Post subject: |
|
|
This might be of help: [file - Dateityp ermitteln] (German). Now you've to check if file is available as an Windows based ---> UnixTool, like [here]
 |
|
| Back to top |
|
 |
BoBo Guest
|
Posted: Sun Apr 17, 2005 8:55 pm Post subject: |
|
|
If Rabiator is right, as said in [this post] AHK's FileRead could be an option.  |
|
| Back to top |
|
 |
toralf
Joined: 31 Jan 2005 Posts: 3841 Location: Bremen, Germany
|
Posted: Tue Apr 19, 2005 3:14 am Post subject: |
|
|
Thanks BoBo,
Chris suggested the same thing 4 posts above yours. :) _________________ Ciao
toralf  |
|
| Back to top |
|
 |
BoBo Guest
|
Posted: Tue Apr 19, 2005 6:11 am Post subject: |
|
|
I've realized that Chris has mentioned that "workarround"
hm - while Chris pointed out to check for the vars length (StringLen), I've assumed Rabiator's hint is about FileRead's errorlevel. But - who cares  |
|
| Back to top |
|
 |
toralf
Joined: 31 Jan 2005 Posts: 3841 Location: Bremen, Germany
|
Posted: Tue Apr 19, 2005 11:25 am Post subject: |
|
|
When I read the manual for FileRead I don't think the Errorlevel will do th etrick, because AHK "thinks" it had read the file successfully when it reaches the first binary 0.
But thanks for pointig that out, I'll look deeper into this soon.
Thank you all, very much appreaciated. _________________ Ciao
toralf  |
|
| Back to top |
|
 |
|