Right click context menu passes only one file as argument - python

I added a new right-click context menu for my own file type (ftype) under HKCU\SOFTWARE\Classes\ftype\shell as do something with ftype and command as "path\ftype.bat" "%1".
I was able to read command-line arguments from ftype.bat when I right click on a .ftype file.
But am facing issues when I select multiple .ftype files and right click to select my command !
For each selected file, my bat is called like,
ftype.bat file1.ftype
ftype.bat file2.ftype
Any ways to make it to work the below way?
ftype.bat file1.ftype file2.ftype
I have tried replacing "%1" in command field with "%*" and with "%1" "%2". But I was able to see spaces being passed to executable !
Note:
Actual regedit entry is a python file and not a bat file as below:
<python_exe_path> "<ftype_handler.py>" "%1"

You can't with a classic static verb using %1. You need to use a drop handler or DelegateExecute shell extension.

Related

Can't run my script from command prompt - "too many values to unpack" [duplicate]

How can I use spaces in the Windows Command Line?
cmd /C C:\Program Files (x86)\WinRar\Rar.exe a D:\Hello 2\File.rar D:\Hello 2\*.*
Single quotation marks won't do in that case. You have to add quotation marks around each path and also enclose the whole command in quotation marks:
cmd /C ""C:\Program Files (x86)\WinRar\Rar.exe" a "D:\Hello 2\File.rar" "D:\Hello 2\*.*""
I just figured out that for a case where the path involves the use of white space characters, for example, when I need to access the app xyz which location is :
C:\Program Files\ab cd\xyz.exe
To run this from windows cmd prompt, you need to use
C:\"Program Files"\"ab cd"\xyz.exe
or
"C:\Program Files\ab cd\xyz.exe"
If double quotes do not solve the issue then try e.g.
dir /X ~1 c:\
to get a list of alternative file or directory names. Example output:
11/09/2014 12:54 AM 8,065 DEFAUL~1.XML Default Desktop Policy.xml
06/12/2014 03:49 PM <DIR> PROGRA~1 Program Files
10/12/2014 12:46 AM <DIR> PROGRA~2 Program Files (x86)
Now use the short 8 character file or folder name in the 5th column, e.g. PROGRA~1 or DEFAUL~1.XML, in your commands. For instance:
set JAVA_HOME=c:\PROGRA~1\Java\jdk1.6.0_45
I prefer to enclose the command in () which is valid batch which makes it a bit easier to read:
cmd /C ("C:\Program Files (x86)\WinRar\Rar.exe" a "D:\Hello 2\File.rar" "D:\Hello 2\*.*")
Enclose the paths containing spaces with double quotes.
cmd /C "C:\Program Files (x86)\WinRar\Rar.exe" a "D:\Hello 2\File.rar" "D:\Hello 2\*.*"
Try to provide complex pathnames in double-quotes (and include file extensions at the end for files.)
For files:
call "C:\example file.exe"
For Directory:
cd "C:\Users\User Name\New Folder"
CMD interprets text with double quotes ("xyz") as one string and text within single quotes ('xyz') as a command.
For example:
FOR %%A in ('dir /b /s *.txt') do ('command')
FOR %%A in ('dir /b /s *.txt') do (echo "%%A")
And one good thing, cmd is not* case sensitive like bash. So "New fiLE.txt" and "new file.TXT" is alike to it.
*Note: The %%A variables in above case is case-sensitive (%%A not equal to %%a).
set "CMD=C:\Program Files (x86)\PDFtk\bin\pdftk"
echo cmd /K ""%CMD%" %D% output trimmed.pdf"
start cmd /K ""%CMD%" %D% output trimmed.pdf"
this worked for me in a batch file
Just add Quotation Mark
Example:"C:\Users\User Name"
Hope it got Solved!
Spaces in the Commend Prompt (in a VBA Shell command code line)
I had a very similar problem which ended up being a space in the command prompt when automating via VBA to get the contents from the command window into a text file. This Thread was one of many I caught along the way that didn’t quite get me the solution.
So this may help others with a similar problem: Since the syntax with quotes is always difficult to get right , I think showing some specific examples is always useful.
The additional problem you get using the command prompt in VBA via the Shell thing, is that the code line often won’t error when something goes wrong: in fact a blink of the black commend window misleads into thinking something was done.
As example… say I have a Folder, with a text file in it like at
C:\Alans Folder\test1.txt ( https://imgur.com/FELSdB6 )
The space there in the folder name gives the problem.
Something like this would work, assuming the Folder, AlansFolder, exists
Sub ShellBlackCommandPromptWindowAutomatingCopyingWindowContent()
Shell "cmd.exe /c ""ipconfig /all > C:\AlansFolder\test1.txt"""
End Sub
This won’t work. (It won’t error).
Sub ShellBlackCommandPromptWindowAutomatingCopyingWindowContent()
Shell "cmd.exe /c ""ipconfig /all > C:\Alans Folder\test1.txt"""
End Sub
Including quote pairs around the path will make it work
Sub ShellBlackCommandPromptWindowAutomatingCopyingWindowContent()
Shell "cmd.exe /c ""ipconfig /all > ""C:\Alans Folder\test1.txt"""""
End Sub
( By the way, if the text file does not exist, then it will be made).
With the benefit of hindsight, we can see that my solution does tie up approximately with some already given..
Converting that code line to a manual given command we would have
ipconfig /all > "C:\Alans Folder\test1.txt"
That seems to work
This works also
ipconfig /all > C:\AlansFolder\test1.txt
This doesn’t
ipconfig /all > C:\Alans Folder\test1.txt
This final form also works and ties up with the solution from sacra ….” You have to add quotation marks around each path and also enclose the whole command in quotation marks “ …..
cmd.exe /c "ipconfig /all > "C:\Alans Folder\test1.txt""
You should try using quotes.
cmd /C "C:\Program Files (x86)\WinRar\Rar.exe" a "D:\Hello 2\File.rar" "D:\Hello 2\*.*"
It can solve this problem by cd command, this command understand spaces without double quotes and you can call any program this way for example:
C:\Windows\system32>cd c:\Program Files\MongoDB\Server\3.2\bin
c:\Program Files\MongoDB\Server\3.2\bin>mongo
now command prompt call mongo.exe

Is there a way to make a VBA code run a Python code?

I have read all online questions on this problem and none of them seem to be working for me. I want to write a code in VBA such that when a button is pressed, my code in Python automatically starts running.
I am getting a file not found error when I run the code but I have checked the path and I know it is correct.
The code I am trying is:
Sub MyMacro()
Shell("C:/Users/RGilsburg/New folder/pythonw.exe" & "F:/Asset/Global/Port/untitled1.py")
End Sub
Can anyone tell me where the error is?
The shell command should work, however, you have some issues with the command to be executed:
You need to put a blank between the Python command and the python file.
Use the backslash, not the slash if you are on windows
If a file contains a blank, you have to put it in Double quotes ". When you want to have a quote character in a string in VBA, you have to double it.
Basically, the parameter to Shell should be the same as you would enter it on a command prompt. To check this, write the command into a string variable and write this to the immediate window (Ctrl+G). Copy it from there and paste it into a CMD-window.
Try
Dim python as string, script as string, cmd As String
python = """C:\Users\RGilsburg\New folder\pythonw.exe"""
script = "F:\Asset\Global\Port\untitled1.py"
cmd = python & " " & script
Debug.print cmd
Shell cmd
this may be a bit ignorant of me since i know nothing of python and little of vba but did you add python as a reference in the vba editor.
if not
go to the tools dropdown in the vba editor select references and see if you can find python.

Python Creating a 7zip archive with command line

So I am trying to run a command line argument to create a 7zip archive as below:
rc = subprocess.run([path, 'a', 'archive.7z', '-psecret', 'mhe', 'log.log'])
However, all it is doing is opening 7zip and not creating archive.7z as intended.
Any reason why?
Adding comment as answer in case others come across this:
When you say 'opening 7zip' I imagine you mean the file manager GUI, which means you are hitting the wrong executable. You want path to end in 7z.exe not 7zFM.exe.

unable to run python script from Batch file

Hello I Wish to run a python script, (I did not write), from a batch file I wrote.
I have 2 questions:
Running the command in the Command prompt works fine but from the batch file it fails with no error message; the python script creates an output file but it is not created when running through the batch file.
#echo off
SET /P PrePRD=Enter the Pre Production file name:
SET /P PostPRD=Enter the Post Production file name:
Set /p IdColumn= Enter the Name of the ID column (ID - Original Input):
cmd /k "cd /d C:\CSVComparison"
C:\CSVComparison\csv_diff.py -src "%PrePRD%" -dest "%PostPRD%" -keys "%IdColumn%"
I wish to open the output file which python created using Notepad++, but I'm not sure it will work this way:
start C:\Program Files (x86)\Notepad++\notepad++.exe C:\CSVComparison\results_details.json
notepad++.exe C:\CSVComparison\results_details.json
Thank you,
1) you start another instance with cmd /k, but the following command gets executed in the original instance (I guess, you want it to be executed in the second instance). Either execute it in the original instance (no cmd /... at all; you might need to call it)
pushd "C:\CSVComparison"
call "C:\CSVComparison\csv_diff.py" -src "%PrePRD%" -dest "%PostPRD%" -keys "%IdColumn%"
popd
or start it in another window:
start /d "C:\CSVComparison" "csv_diff" "C:\CSVComparison\csv_diff.py" -src "%PrePRD%" -dest "%PostPRD%" -keys "%IdColumn%"
2) if a folder or file contains space(s) , you need to enclose the whole thing in quotes. Better: get used to always enclose folders/filenames in quotes:
start "" "C:\Program Files (x86)\Notepad++\notepad++.exe" "C:\CSVComparison\results_details.json"
(start) takes the first quoted string as window title, so you need to give it a fake title ("")
Answering your first question - unfortunately Windows does (yet?) not know about shebangs, so you need to specify that you want to run your python script using python interpeter:
cmd /k "cd /d C:\CSVComparison"
C:\path\to\python.exe C:\CSVComparison\csv_diff.py -src "%PrePRD%" -dest "%PostPRD%" -keys "%IdColumn%"

Line comments in Pycharm

Hi when I comment out a line in Pycharm using the ctrl+slash keyboard shortcut, I get
# code
but instead I want
##Code
like in IDLE.
Is this possible?
You can register a new file type and at the time of registration specify comment characters. You need to reassign *.py to this new type from the default Python file type that comes with installation.
Step by step procedure:
Ctrl+Alt+S to open File Types dialogue.
Alt+Insert (or click on +) to create a new file type
In the new File Type dialogue for Syntax Highlighting -> Line comment: provide ##
Provide rest of the details (including Name and Description) as needed and click OK.
Select the newly created File Type and in Registered Patterns section click + to add a wildcard.
In Add wildcard dialogue box enter *.py and click Ok
Since *.py is already assigned to the default type you will be asked to confirm Reassign wildcard. Do so.
Apply and Ok
Note: We have to do all this because we can not edit default file types and Python is a default file type.
Now, open a Python file (.py extension) and check Ctrl+Slash on any line. You should now see ## prepended to the line.
Ctrl+Slash on any line comments the line i.e. Ctrl + /

Categories

Resources