Batch

Batch

Mate de Vita
Kelli

2008 Oct 4 • 2453
159 ₧
I've done a bit of scripting in Bash in one of my classes but I'm pretty much completely new to batch scripts. I would like to run the following command from a batch script:
code
java -cp "C:\path to external jar;C:\Path to Java project bin" main.MP3Edit [-p path_to_target_dir] [File1 ...]

by allowing the user (me) to input the path to target directory and the file names into the command prompt terminal (yes, I could read the input in the Java program itself but let's say I'm running someone else's program and can't change it).

I've written the following batch script using a few things I found on Google:
code
@echo off
set /p "arg=Input the path to target directory (defaults to current directory if left empty): "
if not [%arg%]==[] set "arg=-p %arg%"

echo Input file names one per line. If no file names are input, all files in the target directory will be used.
:loop
set /p "line=Input next file name (leave empty to end list of files): "
if [%line%]==[] goto run
set "arg=%arg% %line%"
set line=
goto loop

:run
REM java -cp "C:\path to external jar;C:\Path to Java project bin" main.MP3Edit %arg%
echo %arg%
pause


Now the problem with this is that the path and the file names can have spaces in them, which means each of them should in the command be surrounded by quotation marks. In bash iirc you can achieve that by using a combination of single quotes and double quotes in the script, but single quotes don't seem to do anything in batch. How do I get each argument separately properly surrounded by quotation marks?

Also incidentally, is there a way to make set /p recognize an empty string as an input? Because in the above example, if I remove the "set line=" command, the loop doesn't exit even on empty input because the set /p (first line of the loop) doesn't seem to do anything if it receives empty input.
...and that's the bottom line because Mate de Vita said so.
 
 
 
2015 Mar 7 at 18:51 UTC — Ed. 2015 Mar 7 at 21:26 UTC
SuperJer
Websiteman

2005 Mar 21 • 6666
Just put the quotes around %line%. The set command reads them literally if they are after the equals sign:

code
set arg=%arg% "%line%"


I don't see any way to get set /p to accept an empty line. I think what you are already doing is as good as it gets.

Thanks BTW for helping me relive the horror of batch. Next time I'm annoyed with Bash I'll remember this and feel much better.
 
 
 
2015 Mar 8 at 22:41 UTC — Ed. 2015 Mar 8 at 22:46 UTC
Mate de Vita
Kelli

2008 Oct 4 • 2453
159 ₧
To be fair, Windows does have Powershell, which is slightly better than the standard command prompt.
...and that's the bottom line because Mate de Vita said so.
 
 
 
2015 Mar 9 at 22:10 UTC — Ed. 2015 Mar 9 at 22:11 UTC
SuperJer
Websiteman

2005 Mar 21 • 6666
Powershell... ewwww.

Why take all the time to type grep x y when you can type Get-Content y | Select-String -Pattern x?

Kill me with fork. Or just install Cygwin or MSYS.
 
 
 
2015 Mar 9 at 22:20 UTC — Ed. 2015 Mar 9 at 22:22 UTC
Page [1]