I'm trying to run a command line argument through VB.NET using the Shell() command.
I'm trying to use this piece of code:
FOR /R %I in (*.pdf) DO @pdf2swf.exe "%~fI" -o "%~dpI%~nI.swf" -f -T 9
-t -G
Using this:
Shell("FOR /R %I in (*.pdf) DO @pdf2swf.exe "%~fI" -o "%~dpI%~nI.swf" -f -T 9
-t -G ")
However, the interpreter is giving me this error:
Character is not valid. (BC30037)
For the %~
part.
I also tried created a string and passing the argument to the Shell() command by using Shell(StringName)
but I still get the same error in the string.
How could I fix this issue?
This is not proper use of the Shell Method:
Public Shared Function Shell (PathName As String, [...]) As Integer
Parameters
PathName
Type: System.String
Required. String. Name of the program to execute, together with any required arguments and command-line switches. PathName can also include the drive and the directory path or folder.
The first parameter is supposed to be the name of a program to execute. FOR
is not a program, it's a built-in feature of the cmd.exe
command line interpreter.
As far as I can see, you have the following options:
Option 1: Explicitly call cmd.exe
and pass the string that you want to execute with the /c
parameter:
Shell("cmd.exe /c for /R %I ...")
Don't for get to duplicate quotation marks ("
) to escape them.
Option 2: Create a batch file and call the batch file using Shell
.
Option 3: Don't use FOR
to find the files you need, but use the methods of the System.IO namespace, e.g. Directory.EnumerateFiles, instead.
Escape your internal quote marks like this.
Shell("FOR /R %I in (*.pdf) DO @pdf2swf.exe ""%~fI"" -o ""%~dpI%~nI.swf"" -f -T 9 -t -G ")
As I recall, in VB.Net you escape double quote marks by doubling them.
EDIT:
It might help if you do the iteration outside of the Shell. (Certainly to debug)
Dim sourceFolder As String = "c:\Your call"
Dim sourceFiles As String[] = Directory.GetFiles(sourceFolder, "*.pdf")
ForEach file As String In sourceFiles
Dim justName As String = Path.GetFileNameWithoutExtension(file)
Dim shellCall As String = _
String.Format("pdf2swf.exe ""{0}"" -o ""{1}.swf"" -f -T 9 -t -G", _
file, justName)
Shell(shellCall)
EndFor
You could also cosider using System.Diagnostics.Process
instead of Shell
Try escaping the quotes (2 quote marks - "" - in VB.NET, IIRC):
Shell("FOR /R %I in (*.pdf) DO @pdf2swf.exe ""%~fI"" -o ""%~dpI%~nI.swf"" -f -T 9 -t -G ")
If pdf2swf.exe is not in the same folder your program's executable is running from, that could be a reason you're getting the error.
Also, you'll have the same issue with the *.pdf files if they are in a different folder other than where your executable is. You can specify the drive and path to search:
Shell ("FOR /R C:\SomeFolder %I in (*.pdf) DO @pdf2swf.exe ""%~fI"" -o ""%~dpI%~nI.swf"" -f -T 9 -t -G ")
精彩评论