0

Below is the .bat code I have for replacing the "/" occurrences of the first argument with "\", then executing that command with parameters %2, all redirected to %3:

@ECHO OFF

if exist %1 (
    set a=%1:/=\%
    %a% %2 > %3
) else (
    Echo "File not found"
)

The output of the above is:

The system cannot find the path specified.

What is wrong with the above? Also, how could I split the second argument (%2) into words (given that it is a sentence, ie., a collection of words)?

1 Answers1

0

You can't do substring replacement with parameter variables (%1) or for variables (%%a), only with "normal" environment variables.

@ECHO OFF
if not exist "%1" goto :notfound
set "a=%~1"
set "a=%a:/=\%"
%a% %2 > %3
goto :eof
:notfound
Echo "File not found"

I changed your logic a bit to avoid delayed expansion

Community
  • 1
  • 1
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • Thanks! The correct function from %1 seems to be invoked now. The problem is that the argument %2 is now considered as one string, but it is a collection of words that need to be squeezed between %a% and %3. What would be the easiest way to achieve this? – user3560285 Jan 25 '16 at 07:48
  • can you [edit](http://stackoverflow.com/posts/34986333/edit) your question with an example, please? May there be `/` in `%2`? – Stephan Jan 25 '16 at 07:57
  • I provided a separate question, to avoid clutter: https://stackoverflow.com/questions/34987704/split-the-passed-parameter-then-access-all-elements – user3560285 Jan 25 '16 at 09:29