I tried to automate archiving folders inside a directory with Winrar through Windows 10 command line either by Python or VBA codes. Considering that there might be spaces, I enclosed all paths in quotations.
However I ran in to the same error in both Python or VBA;
" 'C:/Program' is not recognized as an internal or external command, operable program or batch file."
Here is a sample uncompleted Python code attempt;
def folders_2_Winrar(source_path:str, archive_path:str, password:str='', winrar_path:str='"C:/Program Files/WinRAR/winrar.exe"')->None:
import os
folder_list = path_entries(source_path)['folders'] ## separate function that returns a list of folders in a directory
for f in folder_list:
rar = '"'+ archive_path + os.path.basename(f) +'.rar'+'"'
f = r'"'+f+'\*.*"'
cmd = winrar_path+' a -hp' + password + ' -ep1 ' + rar + ' ' + f
os.system(cmd)
# test the function
folders_2_Winrar("E:/Test/Source", "E:/Test\\Archive/","123")
what's the problem I could not grasp??!!
Short answer: Use a shorthanded version of the full path:
"C:/Progra~1/WinRAR/winrar.exe"Longer answer: Command prompts don't like space in between paths. Usually within the prompt itself you will need to use quotation marks to signify the full path that contains space, or use a shorthand convention of the ~1 (tilde notation).
Here is a web archived Microsoft knowledge base that I dug up, which I'll include here as a copy in case it goes down:
Alternative answer: you can try adding quotes in the
cmdthat you intend to run: