| View previous topic :: View next topic |
| Author |
Message |
roland65 General User

Joined: 13 Dec 2007 Posts: 22
|
Posted: Mon Sep 05, 2011 5:17 am Post subject: Shell function for file paths with spaces on Windows |
|
|
Hi there,
my problem is a little long to explain, so I'll try to use a simple example to clarify the issue.
On Windows, I'd like to execute a batch file in a macro using the Shell() function. The key point is that there is a redirection within the batch commands.
Here is the example :
Batch file : C:\Test Dir\example.bat contains the following commands :
| Code: | @echo off
cd c:\Temp
C:\Temp\helloworld.exe > C:\Temp\output.txt |
The helloworld.exe program is the traditional C program that displays the string "Hello World!" to the standard output.
Then, I created the following macro in Openoffice or Libreoffice :
| Code: | Sub Main
Dim str as String
str = ConvertToURL("C:\Test Dir\example.bat")
Shell(str, 4, "", True)
End Sub |
If I execute the macro, I get an empty output.txt in C:\Temp. If I change the path name of the batch file to a name without space (ex: C:\Test_Dir) then executing the macro gives a C:\Temp\output.txt file that contains the Hello World! string, as expected.
It appears that in the first case (path name with spaces) the string is written to the standard output instead of the output.txt file.
I tried a workaround (found elsewhere on this forum) by replacing the call to the Shell() function with these lines :
| Code: | Dim shellServ As Object
shellServ = CreateUnoService("com.sun.star.system.SystemShellExecute")
shellServ.execute(str,"",0)
|
and it works (the output.txt file is no more empty) but this *does not allow* to wait for the end of the batch file before continuing the program. Thus, in a real application this solution is not usable because in most cases, one wants to be sure that the batch file was executed before further processing.
Of course, the reason why I need to deal with file paths with spaces is that I'd like to execute a batch file located in a C:\Documents and Settings\<username>\Application Data\Openoffice.org\... directory.
I have checked that the problem exists on both OpenOffice 3.3.0 and Libreoffice 3.4.3.
So, I'll be very grateful to anyone who could show me a workaround that allows to execute correctly a batch file with spaces in its name and a redirection within it *and* allows to wait for the execution of the batch file.
Perhaps there is a service that allows to wait for the execution of a command file?
Any help would be much appreciated...
Roland
Moderation probe1: set CODE tags |
|
| Back to top |
|
 |
covar General User

Joined: 17 Feb 2007 Posts: 25
|
Posted: Tue Sep 06, 2011 10:49 pm Post subject: |
|
|
Copying output.txt from TmpDir to Tmp Dir (with spaces):
example2.bat
| Code: | @echo off
cd c:\TmpDir
C:\TmpDir\helloworld.exe > C:\TmpDir\output.txt
copy /b "C:\TmpDir\output.txt" "C:\Tmp Dir\output.txt" |
| Code: | Sub Main
Dim str as String
Shell(ConvertToURL("C:\TmpDir\example2.bat"), 4, "", True)
End Sub |
|
|
| Back to top |
|
 |
roland65 General User

Joined: 13 Dec 2007 Posts: 22
|
Posted: Wed Sep 07, 2011 1:45 am Post subject: |
|
|
Yes, the solution you give works but my requirements are that the .bat file and the output.txt are both located in the C:\Documents and Settings branch. This is because my extension ins installed there for each user.
So, copying these files to another directory without spaces is not the way I'm looking for in a multiuser perspective.
Anyway, thank for your answer.
RB |
|
| Back to top |
|
 |
Axel Richter General User

Joined: 14 Aug 2011 Posts: 5
|
Posted: Wed Sep 07, 2011 2:44 am Post subject: |
|
|
Hi Roland,
the VBA and OOBasic SHELL command needs no URL. It works with a string containing a file path.
The problem is, that the URL is URL encoded. So the space is encoded as %20 and the windows cmd fails at 'C:/Test%20Dir/example.bat'.
Try:
| Code: | Sub Main
Dim str as String
str = "C:\Test Dir\example.bat"
Shell(str, 4, "", True)
End Sub |
regards
Axel |
|
| Back to top |
|
 |
roland65 General User

Joined: 13 Dec 2007 Posts: 22
|
Posted: Wed Sep 07, 2011 4:28 am Post subject: |
|
|
Sorry, this is not the problem : the example.bat is executed (with or without encoded URL) but the redirection doesn't work properly .
Thanks anyway,
RB |
|
| Back to top |
|
 |
roland65 General User

Joined: 13 Dec 2007 Posts: 22
|
Posted: Tue Sep 20, 2011 5:14 am Post subject: |
|
|
OK, I'll fill a bug report for this problem.
RB |
|
| Back to top |
|
 |
|