0

I have a batch script which moves, and unzips and activates an excel add in. This script works fine when run on a computer with a user with no spaces in the name however fails when a user with a space attempts to run it.

Here is what I have:

Echo Copying PyXLL files...
xcopy \\server\s\SCIA\Operations\Temp\Peter\pyxll\deployment\PyxllAddIn.zip %APPDATA%\PyXLL\ /y

Echo Unzipping files...
powershell -command "Expand-Archive %APPDATA%\PyXLL\PyxllAddIn.zip %APPDATA%\PyXLL"

Echo Installing add-in...
%APPDATA%\PyXLL\python-3913\python.exe -m pyxll activate --non-interactive "%APPDATA%\PyXLL\pyxll.xll

Echo Done!
pause
Pete
  • 5
  • 4
  • 2
    Have you tried putting all the paths in `"` quotes? In the case of the powershell line, since the command itself is already in quotes you can use the backtick to escape out the inner quote so they're not parsed in that line, but are used when the line is executed, eg ``"%APPDATA – Keith Langmead May 11 '23 at 19:04
  • I tried putting all the paths in quotes and you are right the powershell line was the issue... in the case of the powershell line do I wrap `%APPDATA%\PyXLL\PyxllAddIn.zip` with ``" – Pete May 11 '23 at 19:21
  • Here is each of your main commands correctly quoted: `xcopy "\\server\s\SCIA\Operations\Temp\Peter\pyxll\deployment\PyxllAddIn.zip" "%APPDATA%\PyXLL\" /y`, `powershell -command "Expand-Archive '%APPDATA%\PyXLL\PyxllAddIn.zip' '%APPDATA%\PyXLL'"`, `"%APPDATA%\PyXLL\python-3913\python.exe" -m pyxll activate --non-interactive "%APPDATA%\PyXLL\pyxll.xll"`. As an alternative to using single quotes in the PowerShell command: `powershell -command "Expand-Archive \"%APPDATA%\PyXLL\PyxllAddIn.zip\" \"%APPDATA%\PyXLL\""`. – Compo May 11 '23 at 20:26
  • the alternative to using single quotes worked! Thank you – Pete May 11 '23 at 20:27
  • Just to be clear @Pete, both the single quoted, and the escaped doublequoted, versions work. – Compo May 11 '23 at 21:21
  • Please note that the variant with `'` for quoting the file and folder names for *PowerShell* and `"` for the *Windows Command Processor* is the better one. It has the advantage that the entire command line for *PowerShell* is interpreted by `cmd.exe` as one argument string which is not the case with the other variant with `\"`. That makes a big difference if `%APPDATA%` expands to something like `C:\Users\Asterix & Obelix\Application Data` as with `\"` the ampersand is interpreted now by `cmd.exe` as command operator and everything after it as one more command to run by `cmd.exe`. – Mofi May 12 '23 at 11:24

1 Answers1

0

I'll put my answer to the Powershell bit here, as the comments won't let me show a backtick properly. So the Powershell line would be :

powershell -command "Expand-Archive `"%APPDATA%\PyXLL\PyxllAddIn.zip`" `"%APPDATA%\PyXLL`""
Keith Langmead
  • 785
  • 1
  • 5
  • 16
  • I think it is leaving a ` in the path string.... here was part of the error: Expand-Archive : The path 'C:\Users\Peter\AppData\Roaming\PyXLL\PyxllAddIn.zip C:\Users\Peter\AppData\Roaming\PyXLL`' either does not exist or is not a valid file system path. – Pete May 11 '23 at 19:41
  • That command line does not work if `APPDATA` is defined with `C:\Users\Asterix & Obelix\Application Data`. I have seen in the last 30 years users creating an account containing an ampersand which is not forbidden as being a valid character in account and file/folder names. The best solution would be that PowerShell itself gets the value of environment variable `APPDATA` instead of `cmd.exe` is expanding first `%APPDATA%` before parsing the command line to determine what to finally execute. The command line works fine if the problem to solve is just spaces in application data folder path. – Mofi May 12 '23 at 11:26