Is it possible to detect file format and encoding of file using batch files? - batch-file

Is it possible to detect file format and encoding of file using batch files? And if a particular file is not of intended format, throw an error?

As a *nix guy, I'd want to jump for something more powerful than a batch file, such as Python. (or a shell script, but I'm assuming you're using Windows --- you might look into PowerShell, but I've never tried it.)
Unix has a great utility for this sort of thing, it's named file. There appears to be a Windows version here: http://gnuwin32.sourceforge.net/packages/file.htm
Basically, you run file [your filename here] and file spits out a blurb about the file. For example:
$ file zdoom-2.4.1-src.7z
zdoom-2.4.1-src.7z: 7-zip archive data, version 0.3
It's not always right, and it doesn't mean that if file says "this is a JPEG" that the file is actually a JPEG: it could be corrupt, etc.
Also, if I rename the above 7z archive to "foo":
$ file foo
foo: 7-zip archive data, version 0.3
... file will still get it.

Related

Batch export in SAS for spk file

I need to export/import my spk file via windows Batch script.
For which I have been referring this document
But this document does not mention how to save the file (I mean with which extension .bat or .sas)
My command:
ExportPackage -profile "SAS_MW_TEST" -package "F:\mypath\Package4.spk" -objects "/_Applications/_05_MW/_01_SAS_MW/_20_Processes/savedesk(Folder)" -subprop -types "Condition,BusinessRuleFlow,ExternalFile,Cube,SearchFolder,Table,GeneratedTransform,OLAPSchema,InformationMap.OLAP,Measure,Column,Job.CubeBuild,Action,Library,MiningResults,DeployedJob,CalculatedMeasure,Hierarchy,InformationMap.Relational,RootFolder,Prompt,Document,ConditionActionSet,DecisionLogic,Dimension,Note,StoredProcess,PromptGroup,Job,OrchestrationJob,MessageQueue,Service.SoapGenerated,Level,SharedDimension,DeployedFlow"
ExportPackage is an executable program that you can run out of a batch file.
Use Notepad or any other text editor (which includes any SAS code editor), place the ExportPackage program command in the editor and use the File/Save As feature to save the file as something like myPackageExporter.bat
If the items in the package are say stored processes whose metadata says the source code is in an file system folder (aka source code repository), you will probably also want to zip up the folder.
This macro can help you prepare the batch script, ready for export: https://core.sasjs.io/mm__spkexport_8sas.html

Huffman compressed files with my own extension

I am working on a project that uses Huffman algorithm to compress files, and I am doing my project using Java, what I want is to create my own file extension say (.huff) for the compressed file, and when I right click a file if it has the (.huff) extension, I want to add a new option which decompresses it, I searched the web but I did not find anything useful.
Any help would be appreciated.
To set the file extension just use one of the String methods like append(".yourExtension") (append it to the filename) and set as filename. Simple as that.
String filename = filename.append(extension);
To decompress the compressed file, I suggest you write a metod to which you provide a path to file as argument, check if the file extension is correct and then in another method you decompress this file.
There is nothing special about a file extension, it's just a part of the file name. To create a .huff file extension, just add .huff to the end of the file name.
To add the windows context menu, that's explained in the question linked in the comments How can I add a context menu to the Windows Explorer for a Java application?
I would recommend creating a batch script that will launch your program taking in the file to decompress as an argument.
Something similar to:
#echo off
java -cp <path-to-jar> <decompression main class> %1
Adding in any other setup or program arguments you need. Then a registry entry might look like.
HKEY_CLASSES_ROOT\.huff\shell\Decompress huffman encoded file\command
"<path to batch file>" "%1"

Is it possible to embed an executable inside a batch file?

There are many programs which help me write more efficient code (NirCmd etc.), but they can't run properly if they aren't installed in the computer. So is there a way to, for example, in the temp folder, extract the program from a batch program and use it.
I tried reading the executables with a hex editor, putting the hex code into another file and saving it as an executable. But this failed. So, is there any way to efficiently store an executable inside a batch file, create it and then run it?
Yes, there is one that I know of and works perfectly.
It is a program called bhx.exe (link to its site here).
It can also embed other file types.
The usage is quite simple:
(optional) Create a cabinet (.cab) file from the original .exe using this command: makecab yourexe.exe yourexe.cab. For better compression you can use the /D switch in this way: makecab /D CompressionType=LZX yourexe.exe yourexe.cab
CD to the directory bhx is in and do this: bhx yourexe.cab. Other switches are described in the website.
There you go, the mybin.cmd file is generated.

Code Injection in .png file

I am using the libpng- a c library to check the valid .png file. If a file is valid it passes the test. I want to inject shell code in it. How can I craft a .png file, so that it is still a valid image file and also contains some shell code in it. Please tell me how is it possible. Thanks.
Well, AFAIK there is no way to inject code into a png file and execute it. But you can inject your png file into a shell script, and after view it. But you must convince the one you hack to make the png file executable and to open so-called png file through terminal.
The procedure is:
Create a text file, call it executeme.png
Paste the following code into it, note that there shouldn't be any new line at the end of the file.
#!/bin/bash
PNG_FILE=$(mktemp /tmp/hack.XXXXXXX.png)
ARCHIVE=$(awk '/^__ARCHIVE_BELOW__/ {print NR + 1; exit 0; }' $0)
tail -n+$ARCHIVE $0 > "$PNG_FILE"
# whatever you want to do is here!
xdg-open $PNG_FILE
exit 0
__ARCHIVE_BELOW__
Append your original png file using cat injectme.png >> executeme.png.
Make executeme.png executable.
If you run the executeme.png from terminal, the original png file will be shown using the default image viewer, and your injected code will be run.
Note: I don't believe there is someone so stupid to execute that file.
Note2: On Ubuntu, executeme.png cannot be executed from file managers because it's tried to be opened using the file manager due to the png extension. You may rename file executeme.png to execute.\rpng (append a carriage return before png after dot) so at first it looks like a png file, since its extension is not png it will be executed with double click if it's executable. To make that renaming, you may need to use terminal.
Have a good time hacking! :D
Further reading: Linux journal, making installers

File encoding format through command prompt : Windows

I have a file which may be in ASCII or UTF-8 format. I can know which format it is through Notepad++. But can some one tel me a tool that could show me in which format the file is through command prompt.
Example:
Open Command Prompt,
C:><Some Command> FileName
which should give me the file format like ASCII or UTF-8.
Install Python 3.x
Run command in cmd.exe (Administrator): pip install chardet
Write a small python script that read a file, detect the encoding, and print the encoding using the newly installed module chardet. See here for help.
Put the script somewhere under PATH
Suppose you create ec.py doing the job. Then you can invoke ec FileName on command line to get the encoding. If you do a good job writing the python script, you can invoke something like ec *.txt to get the encodings of multiple files.
This is a duplicate of this question here which has a great answer (not by me I might add)
EDIT
I am pretty sure there is not way that is reliable to do this, usually you are told the encoding of a file, sure you can look for a Byte Order Mark (BOM) at the start of the file but its not mandatory and so is not a true indicator unless you know for SURE its supposed to be there.
Unless someone knows differently I don't think its possible to work out from scratch you have to have some clue about the encoding used.

Resources