How to import the information of selected files into a python script

English support forum

Moderators: Hacker, petermad, Stefan2, white

Post Reply
raytc
Senior Member
Senior Member
Posts: 274
Joined: 2004-06-28, 11:03 UTC

How to import the information of selected files into a python script

Post by *raytc »

I'm trying to import the information from a few selected files into a python script.

Does anyone know the best way to do that?

My python script: pyth1.py

I tried things like this:

Code: Select all

command: cmd
parameters: /c "%L"|clip|python d:\pyth1.py
And in my python script:

Code: Select all

import pyperclip #to import from clipboard
selected_files = pyperclip.paste()
User avatar
Dalai
Power Member
Power Member
Posts: 10022
Joined: 2005-01-28, 22:17 UTC
Location: Meiningen (Südthüringen)

Re: How to import the information of selected files into a python script

Post by *Dalai »

Why the detour with CMD and even the clipboard? Python can read files directly.

Code: Select all

Command: python.exe
Parameters: your_script.py "%L"
Don't know how to do that in Python because I don't know how to write in this language (can read it given enough time).

Apart from that I'm pretty certain that clip.exe doesn't output anything into the pipe, because why should it?

Regards
Dalai
#101164 Personal licence
Ryzen 5 2600, 16 GiB RAM, ASUS Prime X370-A, Win7 x64

Plugins: Services2, Startups, CertificateInfo, SignatureInfo, LineBreakInfo - Download-Mirror
raytc
Senior Member
Senior Member
Posts: 274
Joined: 2004-06-28, 11:03 UTC

Re: How to import the information of selected files into a python script

Post by *raytc »

To import a variable from the command line in python, sys.argv can be used.

However, "%L" is empty.
No matter what I do %L remains empty.
User avatar
Dalai
Power Member
Power Member
Posts: 10022
Joined: 2005-01-28, 22:17 UTC
Location: Meiningen (Südthüringen)

Re: How to import the information of selected files into a python script

Post by *Dalai »

%L (and their Unicode counterparts %WL and %UL and %WF and %UF) are only empty when the cursor in TC is on [..] (one level up) when calling the command. Test with this button:

Code: Select all

TOTALCMD#BAR#DATA
notepad.exe
"%L"
shell32.dll


0
-1
(Copy the code above and right click on an empty space of the button bar to paste it.)

Adding a single question mark at the start of the parameters (?"%L") is another way to test how TC behaves and to find out when the variable contains something.

Regards
Dalai
#101164 Personal licence
Ryzen 5 2600, 16 GiB RAM, ASUS Prime X370-A, Win7 x64

Plugins: Services2, Startups, CertificateInfo, SignatureInfo, LineBreakInfo - Download-Mirror
raytc
Senior Member
Senior Member
Posts: 274
Joined: 2004-06-28, 11:03 UTC

Re: How to import the information of selected files into a python script

Post by *raytc »

Thank you Dalai.

Indeed, it works great in Notepad.
Unfortunately still not with a Python script.
cmd python.exe
parameters: d:\mypythonscript.py "%L"

I checked it with the question mark at the start of the parameters.
I noted that %L puts the temporary directory in the arguments list (?C:\Users\xxx\AppData\Local\Temp\CMDD5E9.tmp) and not the names of the selected files.

However it is the right way to pass arguments with a python script:
https://www.tutorialspoint.com/python/python_command_line_arguments.htm
User avatar
Stefan2
Power Member
Power Member
Posts: 4281
Joined: 2007-09-13, 22:20 UTC
Location: Europa

Re: How to import the information of selected files into a python script

Post by *Stefan2 »

raytc wrote: 2022-02-10, 09:09 UTC
I checked it with the question mark at the start of the parameters.

I noted that %L puts the temporary directory in the arguments list (?C:\Users\xxx\AppData\Local\Temp\CMDD5E9.tmp)
and not the names of the selected files.


"%L" and related parameters are replaced by the path to a temp file which contains all selected files,
line by line. Folders has trailing backslash there.

You have to parse that content with your script and act line by line.


Right click at an button in the buttonbar and chose Modify...
In that dialog press F1-key for to read the help with all possible parameters.
Like %S - insert the names of all selected files into the command line. Names containing spaces will be surrounded by double quotes.
Please note the maximum command line length of 32767 characters.




Example parsing code in VBS:

Code: Select all

Call as:   path\myscript "%L" "%T"

    sTCtempList = Wscript.arguments.Item(0) ' The TC temp file due to the "%L" parameter
   'strTCTarget = Wscript.arguments.Item(1) ' The Target panel due to the "%T" parameter
   

Set FSO = CreateObject("Scripting.FileSystemObject")

Set objFile = FSO.OpenTextFile(sTCtempList, 1)

Do While Not objFile.AtEndOfStream

    strCurrItem = objFile.ReadLine

    If(strCurrItem <> "") Then 

			If Right(strCurrItem,1) = "\"  Then 
				Create_FolderStructure(strCurrItem)

			ElseIf InStr(strCurrItem, "\") > 0 Then 
				sPartPath = FSO.GetParentFolderName(strCurrItem)
				sPartFile = FSO.GetFileName(strCurrItem)
				Create_FolderStructure(sPartPath)
				FSO.CreateTextFile(sPartPath&"\"&sPartFile)

			Else 
				FSO.CreateTextFile(strCurrItem)

			End If
	End If
Loop

Function Create_FolderStructure
...
End Function

Example only, many things may be missed here.




 
raytc
Senior Member
Senior Member
Posts: 274
Joined: 2004-06-28, 11:03 UTC

Re: How to import the information of selected files into a python script

Post by *raytc »

Thanks Stefan.

Why doesn't it work like in Notepad?
It parse directly into notepad.

If I use my python script with arguments like this:
mypythonscript.py "argument1", "argument2", "argument3"
and import it in my python script

Code: Select all

import sys
for arg in sys.argv:
    print(arg)
it works great.

Importing the TEMP file in my python script makes it too complicated and how do I use %P%S in my python script? :?
raytc
Senior Member
Senior Member
Posts: 274
Joined: 2004-06-28, 11:03 UTC

Re: How to import the information of selected files into a python script

Post by *raytc »

Found it.
%L is not the right one.
%P%S does the job.
User avatar
Stefan2
Power Member
Power Member
Posts: 4281
Joined: 2007-09-13, 22:20 UTC
Location: Europa

Re: How to import the information of selected files into a python script

Post by *Stefan2 »

raytc wrote: 2022-02-10, 10:43 UTC Why doesn't it work like in Notepad?
It parse directly into notepad.

Notepad just opens that temp text file and display the content.
That content you can parse line by line with an script.




raytc wrote: 2022-02-10, 10:43 UTC If I use my python script with arguments like this:
mypythonscript.py "argument1", "argument2", "argument3"
and import it in my python script
it works great.

Maybe try
mypythonscript.py "argument1", "argument2", "argument3"
mypythonscript.py %S


CMD= mypythonscript.py
PARAM=%S

But TC don't have comas in between the single file names.


TEST:

Code: Select all

TOTALCMD#BAR#DATA
cmd /k 
ECHO myScript %S
C:\WINDOWS\system32\cmd.exe
ECHO %S


-1



Or maybe the other way around:
CMD= cmd /k
Param= FOR /F "tokens=*" %%x IN (' type "%L" ') DO @myScript "%%x"



 
User avatar
Dalai
Power Member
Power Member
Posts: 10022
Joined: 2005-01-28, 22:17 UTC
Location: Meiningen (Südthüringen)

Re: How to import the information of selected files into a python script

Post by *Dalai »

I strongly advise against using %S! The command line length in TC is limited to 2048 characters (IIRC) and even in Windows it's 8192 characters. That is the whole command line, including the called program and since %P%S passes each name with its full path, you're at the limit very quickly.

It's a lot better to change the Python script to read from a temp file passed by TC via %L or similar.

Regards
Dalai
#101164 Personal licence
Ryzen 5 2600, 16 GiB RAM, ASUS Prime X370-A, Win7 x64

Plugins: Services2, Startups, CertificateInfo, SignatureInfo, LineBreakInfo - Download-Mirror
Post Reply