How to make a converter?

Discussion and questions - donor version.
Post Reply
Message
Author
Florent
Posts: 17
Joined: 27.11.2018, 22:43

How to make a converter?

#1 Post by Florent » 01.12.2018, 21:46

Hello,

I am trying to view the content of a zip file (i.e. folders and files) in the Quick View panel instead of the crypto characters that are displayed in standard.
I thought one way to achieve this was to use 7z.exe (command line version of 7z) as a converter like xdoc2text.exe in “Configure Viewer” settings. But after several attempts, it doesn’t seem to work.
After reading this help file page a couple times (https://freecommander.com/fchelpxe/en/F ... erter.html) I am starting to think that converters are more specific than just a command line that gives text as output from any given input file.

Is it possible to create (easily) a converter for any given purpose?
What are the rules to follow?

Marek
Author
Author
Posts: 3973
Joined: 10.04.2006, 09:48
Location: Germany
Contact:

Re: How to make a converter?

#2 Post by Marek » 01.12.2018, 23:00

You can see the content of the archive file simply with ENTER or double click.

Florent
Posts: 17
Joined: 27.11.2018, 22:43

Re: How to make a converter?

#3 Post by Florent » 02.12.2018, 13:45

Thank you Marek but my problem is not in viewing or extracting zip files, I just want to know more about the inner working of converters and how to make one.
The zip file is an example of what I think/hope is possible to do with them in Quick View panel.

I have been using FC for years now only for the tabs and dual window until I finally read a bit of the manual and discovered I was merely using 1% of the capabilities of the program.
It really is a powerful tool with great features: rename files in batch using regex, search content of PDF files, configure favorite tool shortcuts, etc. and I am just starting to use them.

I still have plenty to tinker with in the thousands of parameters and, at the moment, I am trying to make the most out of converters.
They are mentioned in "Configure Viewer" and "Search files/folders" parameters but the only example is xdoc2txt.exe which takes only one line in the parameters table.
So my guess is that other converters are possible.

Where can I find other examples of converters?
And what are the requirements to make a converter?

Florent
Posts: 17
Joined: 27.11.2018, 22:43

Re: How to make a converter?

#4 Post by Florent » 02.12.2018, 14:04

To take the "zip file" example again, I tried to configure a converter with 7z.exe (which is the command line version of 7zFM.exe we currently use with GUI).
Writing the following code in a console displays the content of "test.zip":

Code: Select all

PS D:\folder> .\7z.exe l test.zip
I thought it would be easy to have the Quick View panel display the content of a zip file but I cannot make it work.

I also tried to make a simple PowerShell script to output text but cannot make it work either.
Here is (one of) the configuration(s) I tried:
Image
Image
http://www.noelshack.com/2018-48-7-1543 ... verter.jpg

Hence the questions I asked on the previous post.

Florent
Posts: 17
Joined: 27.11.2018, 22:43

Re: How to make a converter?

#5 Post by Florent » 13.12.2018, 14:48

up

I can't believe there is only one converter compatible with FreeCommander: xdoc2text.exe.
There got to be other converters or a way to make one.

Please give me some hints.

Marek
Author
Author
Posts: 3973
Joined: 10.04.2006, 09:48
Location: Germany
Contact:

Re: How to make a converter?

#6 Post by Marek » 13.12.2018, 20:39

Let's say you want to write a converter for the zip files and then use it in the viewer.
You have to write a program for this purpose which has a zip file as input and a text file as output.
In the text file, all folders and files from the zip file should be listed. The call of the program could look like this

Code: Select all

ZipConverter2Txt.exe pathZipFile pathTxtFile
Integrated in Viewer would display the text file with the Zip content.

Florent
Posts: 17
Joined: 27.11.2018, 22:43

Re: How to make a converter?

#7 Post by Florent » 14.12.2018, 23:07

OK, so I did what you explained: I created an exe file that takes a filename as input and creates a text file as output with the same filename (but with a .txt extension).
I did it with a simple (quick and dirty) C# class just for testing.

Code: Select all

using System;
using System.IO;

namespace CreateTxt
{
	class WriteTextFile
	{
		static void Main(string[] args)
		{
			string pathName = Path.GetDirectoryName(args[0]);
			string fileName = Path.GetFileNameWithoutExtension(args[0]);
			string[] lines = { args[0], pathName, fileName };
			System.IO.File.WriteAllLines(pathName + @"\" + fileName + ".txt", lines);
		}
	}
}
I compiled it to "create_txt.exe" using csc.exe (available with Microsoft .NET framework).
This exe works as expected in command line (I guess). I uploaded it here: https://ufile.io/lw06n

The thing is, I still cannot make it work. I think I fail in configuring the parameters (I do not quite get your "ZipConverter2Txt.exe pathZipFile pathTxtFile" since both input and output files are given in input parameters to the exe). Here are the parameters I tried in FreeCommander.ini:

Code: Select all

[TfcViewerForm_Converter]
Enabled1=1
Filter1=.zip.xyz.zim
Prg1=D:\Test\Converters\CSharp\create_txt.exe
TargetExt1=.txt
Param1="%ConvertSourceFile%" >%ConvertTargetFile%
I also tried to change the last line without the quotes, without ">", without %ConvertTargetFile%, etc. Nothing worked.
Any idea on what I am missing?

Marek
Author
Author
Posts: 3973
Joined: 10.04.2006, 09:48
Location: Germany
Contact:

Re: How to make a converter?

#8 Post by Marek » 15.12.2018, 12:31

You should use args[0] for input file and args[1] for output file.
The example must work first:

D:\Test\Converters\CSharp\create_txt.exe "d:\test\myArchive.zip" "c:\temp\myArchive.txt"

arg[0] = "d:\test\myArchive.zip"
arg[1] = "c:\temp\myArchive.txt"

For Param1:
Param1="%ConvertSourceFile%" "%ConvertTargetFile%"
or
Param1="%ConvertSourceFile%" %ConvertTargetFile%

Florent
Posts: 17
Joined: 27.11.2018, 22:43

Re: How to make a converter?

#9 Post by Florent » 15.12.2018, 13:12

OK, I tried it this morning when I looked more carefully to the meaning of your previous post, but I still can't make it work.
Here is the code:

Code: Select all

// first argument is not used, second argument is used as name of the created (dummy) file
using System;
using System.IO;

namespace CreateTxt2
{
	class WriteTextFile
	{
		static void Main(string[] args)
		{
			string[] lines = { "Line 1", "Line 2" };
			System.IO.File.WriteAllLines(args[1], lines);
		}
	}
}
Here is the exe: https://ufile.io/maue6

It works as expected in command line:
.\create_txt2.exe D:\Test\Converters\tost.zip C:\temp\tost.txt

I really think I have problems with parameters (I noticed some problems with persistence of parameters in "Configure viewer" panel, that's why I change them directly in the ini file while FreeCommander is closed).

Code: Select all

[TfcViewerForm_Converter]
Enabled1=1
Filter1=.zip.xyz.zim
Prg1=D:\Test\Converters\CSharp\create_txt2.exe
TargetExt1=.txt
Param1="%ConvertSourceFile%" "%ConvertTargetFile%"
Can you try to make this example work on your PC and send me your full settings (maybe some parameters other than [TfcViewerForm_Converter] have a negative impact on my testings)?
PS: I use "FreeCommanderXE-64-donor_portable770" version (if that is relevant with my problem)

Marek
Author
Author
Posts: 3973
Joined: 10.04.2006, 09:48
Location: Germany
Contact:

Re: How to make a converter?

#10 Post by Marek » 15.12.2018, 20:40

Here is an example that works

Code: Select all

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestConverter2Txt{
    class Program    {
        static void Main(string[] args)        {

            ConvertToTxt(args[0], args[1]);
        }


        static void ConvertToTxt(string inputFile, string outputFile)        {
            if (File.Exists(inputFile))            {
                // read the file and extract what you want and write to the output file

                File.WriteAllLines(outputFile, new string[] { "Line1", "Line2", "Line3", "Line4" });
            }
        }

    }
}

Code: Select all

[TfcViewerForm_Converter]
Enabled1=1
Filter1=*.zip
Prg1=C:\Users\XXXXXXXXX\source\repos\TestConverter2Txt\TestConverter2Txt\bin\Debug\TestConverter2Txt.exe
TargetExt1=
Param1=%ConvertSourceFile% %ConvertTargetFile%
Additionally you must activate in Viewer "View->Show converted file".

Florent
Posts: 17
Joined: 27.11.2018, 22:43

Re: How to make a converter?

#11 Post by Florent » 15.12.2018, 23:05

That's it, the "View->Show converted file" parameter is the key.
Using 7z.exe (refer to post#4) works like a charm with these parameters:

Code: Select all

[TfcViewerForm_Converter]
Enabled1=1
Filter1=.zip.7z
Prg1=C:\Program Files\7-Zip\7z.exe
TargetExt1=.txt
Param1=l "%ConvertSourceFile%" >%ConvertTargetFile%
The output is not exactly what I wanted but it's a start.
There is nothing a C# program (and some time/motivation) can't do.

Anyway, thank you very much Marek for the answers and for having clarified it in the on-line help documentation.

Free4all
Posts: 428
Joined: 30.09.2014, 21:12

Re: How to make a converter?

#12 Post by Free4all » 25.12.2018, 21:41

Looking forward to converters you create, Florent!

Florent
Posts: 17
Joined: 27.11.2018, 22:43

Re: How to make a converter?

#13 Post by Florent » 26.12.2018, 09:59

Still using 7z.exe, I made this:

Code: Select all

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;

namespace Converter2Txt
{
    class Program
    {
        static void Main(string[] args)
        {
            ConvertToTxt(args[0], args[1]);
        }

        static void ConvertToTxt(string inputFile, string outputFile)
        {
            if (File.Exists(inputFile))
            {
				// Launch 7z.exe in command line to retrieve the desired output data
				System.Diagnostics.Process process = new System.Diagnostics.Process();
				process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
				process.StartInfo.UseShellExecute = false;
				process.StartInfo.RedirectStandardOutput = true;
				process.StartInfo.FileName = "powershell.exe";
//				process.StartInfo.Arguments = String.Format(@"/C & 'C:\Program Files\7-Zip\7z.exe' l '{0}'", inputFile);
				process.StartInfo.Arguments = String.Format(@"& 'C:\Program Files\7-Zip\7z.exe' l '{0}'", inputFile);
				process.Start();
				
				// Read the output stream
				string output = process.StandardOutput.ReadToEnd();
				
                // Convert the output of 7z.exe in meaningful lines and then write to the outputFile
                File.WriteAllLines(outputFile, ReorderLines(output));
            }
        }
		
		static List<string> ReorderLines(string output)
		{
			string[] brut = output.Split(new[] { "\r\n" }, StringSplitOptions.None);
			
			// Get only the interesting part of the data
			List<string> lines = new List<string> ();
			bool copyLine = false;
			string path;
			foreach (string line in brut)
			{
				if (copyLine)
				{
					path = line.Substring(53);
					if (line.Substring(20, 1) == "D")
						path += "\\";
					lines.Add(path);
				}
				if (line.StartsWith("----------"))
					copyLine = !copyLine;
			}
			
			// Remove last item which is "--------------"
			if (lines.Count > 1)
				lines.RemoveAt(lines.Count - 1);
			
			return lines;
		}
    }
}
It works fine on my PC but this way is not ideal and the best way to do it would be to use a dedicated zip library or the fcZip plugin but I have not looked into it.

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 31 guests