Uzi
Uzi

Reputation: 453

How to parse and echo multiple file paths using batch

I created a batch file that will echo or print the path of a file. Apparently, I am unable to print the path when I send multiple files as parameters to the said batch file.

@echo off

setlocal ENABLEDELAYEDEXPANSION
set string=%1
set string=!string:\=%%5C!

@echo %string% > D:\Playground\test.txt

Any help would be much appreciated. If there's anything unclear please let me know. Thanks.

Note: I send files to the batch file using the procedure below:

I don't know if this info helps but I didn't want to leave it out of my question.

Upvotes: 0

Views: 140

Answers (1)

user7818749
user7818749

Reputation:

%* will catch each input string passed separated by whitespace, enabledelayedexpansion should be used as you do set inside of a code block (). We also enclose the variables, including variable name with double quotes:

@echo off
setlocal enabledelayedexpansion
for %%a in (%*) do (
 set "string=%%~a"
 set "string=!string:\=%%5C!"
 echo !string! >> D:\Playground\test.txt
)

As you can see, you need to use the for loop to iterate each of the input strings i.e %1 %2 and %3 etc.

As a Side note You can also drag and drop files onto the batch file to get results.

EDIT

Added the quote removal as requested set "string=!string:"=!" but note that using the strings as paths without quoting them will cause an issue in future if the paths contains whitespace.

To pipe to file without newline:

@echo off
setlocal enabledelayedexpansion
for %%a in (%*) do (
 set "string=%%~a"
 set "string=!string:\=%%5C!"
 echo|set /p="!string! " >> D:\Playground\test.txt
)

Upvotes: 1

Related Questions