0

I am using a VB to run .bat file and to pass arguments to it. Right now I managed to run it and to send the arguments to it, but ran into a problem. My arguments might contain spaces inside. I was trying to use quotes, but it didn't seem to work as I expected. So what I am doing:

  1. Running this code: System.Diagnostics.Process.Start("C:\Users\XXXXXXX\Desktop\New.bat", """"+data+"""") where 'data' is the argument I am sending. For testing it contains the value:

Hel loo

Inside the .bat file I have a code, that opens notepad and writes the argument inside it. With this code I have managed to pass the argument as one with spaces, but the result is:

"Hel loo"

Any ideas how to get rid of the quotes on each side, while still passing the argument as one with spaces? I cannot escape them or replace with another symbol. This solution needs to pass the argument as one with spaces inside. Is this possible? The program I am working with is not important.

EDIT

This is the content of the .bat file:

set directory_Rexe="C:\Users\XXXXXXX\Desktop\testBat.txt"

set var=%1


echo %var%>%directory_Rexe%

%directory_Rexe%
4
  • Show us the contents of the batch file. Also, how do you run the batch file from a command window and successfully write text containing spaces? Commented Sep 11, 2017 at 9:15
  • I updated the question with the content of .bat file, however that is relevant if the catching of variables is not the problem . I am using a program, that executes VB code. I am not using command window to run the .bat file. Commented Sep 11, 2017 at 9:23
  • Well, for one, your batch file is broken already; the first line should read set directory_Rexe="C:\Users\XXXXXXX\Desktop\testBat.txt". Commented Sep 11, 2017 at 9:27
  • @Joey Thank you for the correction, you are right, however this is not the issue. It is setting the directory correctly and writing into the file. Made the changes, still can't get rid of the quotes. Commented Sep 11, 2017 at 9:31

1 Answer 1

1

You have three options here:

  1. Use %~1, which will strip the quotes.
  2. Don't care about putting everything into argument 1 and quoting and use %* instead. You mentioned not wanting that, though.
  3. Don't pass the string as an argument, but as an environment variable instead. This also helps a lot when you have a number of characters in it that need to be escaped.

All options require you to change the batch file, though.

I'd also question the need for a batch file when you have a perfectly capable programming language already at your fingertips. Writing text to a file should actually be easier from VB.

Sign up to request clarification or add additional context in comments.

6 Comments

Thank You, this is working quite fine. One concern though. Does the %~1 remove quotes from beginning and end, or from the whole argument even if they are in the middle?
Only the surrounding ones. The full list of such replacements can be found in help for at the end. It applies to for variables and numbered arguments.
I tested it with the %~1 method and it worked for arguments, that did not contain quotes them selves. I will mark your answer as correct, since I was not asking to send arguments with quotes too, but I would be grateful, if You could help me solve this one too. Basically, the argument could contain any symbol and there is no way of knowing exactly which ones.
The most robust option would be (3). Or not using the batch file at all of course. Embedded quotes will probably only work correctly in the cases outlined in cmd /? and I think they have to be balanced. Otherwise it's not %~1 that's your problem, but rather argument splitting done by cmd.
To be honest, my aim is not to write into .txt file. It is just for testing the arguments. I need the .bat file to launch R script and pass these arguments to it. Can't really run the R script from my program, so using .bat files. Any who, all of your answers are great. Not sure how environment variables work with .bat, but that's just a thing I have to read through I guess.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.