1

I have a list of folders listed below:

  • C:\Program Files
  • C:\temp
  • C:\John Snow

I have to pass comma separated arguments to scripts like given below:

script.bat C:\Program Files,C:\temp,C:\John Snow

After passing to the script I need to filter out comma separated argument and apply for each loop and echo the argument value and execute dir command to list files in that director

4
  • you need to iterate arguments in the script.bat or you want to call script .bat with each of the items? Commented May 19, 2016 at 11:15
  • I need to iterate arguments in script.bat . And inside for loop, I need to execute DIR command for each arguments Commented May 19, 2016 at 12:03
  • You can pass the list of arguments surrounded by double quotes and access this list with %1 inside the script. From there you can iterate using a for /f command giving "," as delimiter. Check here. Commented May 19, 2016 at 12:15
  • @Danial Luz, If you could write it for me it will be better because I'm not that good in batch scripting.. Commented May 19, 2016 at 12:26

2 Answers 2

1
@ECHO OFF
SETLOCAL EnableExtensions
set "_params=%*"
if not defined _params (
  echo %~nx0: nothing to do
  pause
  exit /B
)
set "_params=%_params:,=","%"
for %%G in ("%_params%") do (
  echo dir "%%~G"
)

Output

==> 37321331.bat
37321331.bat: nothing to do
Press any key to continue . . .

==> 37321331.bat C:\Program Files,C:\temp,C:\John Snow
dir "C:\Program Files"
dir "C:\temp"
dir "C:\John Snow"

==>

Above script requires some elaborating: it would fail if some argument is enclosed in a pair of double quotes:

==> 37321331.bat C:\Program Files,C:\temp,"C:\John Snow"
dir "C:\Program Files"
dir "C:\temp"
dir ""C:\John"
dir "Snow"""

Edit. Use next code snippet to fix it: set "_params=%_params:"=%" would remove all " double quotes from supplied string.

set "_params=%_params:"=%"
set "_params=%_params:,=","%"
for %%G in ("%_params%") do (
  echo dir "%%~G"
)

Resources (required reading, incomplete):

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

3 Comments

For Me its coming like this: c:\batch>com.bat "c:\Program Files,c:\John Snow" dir ""c:\Program" dir "Files","c:\John" dir "Snow"""
@ThejeshPR This behaviour I have admonished already: please read entire answer.
@ThejeshPR fixed for double quotes. Please consider marking most helpful answer as accepted. See this page for an explanation of why this is important.
0
set "args=%*"
set "args=%args:,=","%"
for %%# in ("%args%") do (
  dir "%%~#"
)

try this.

3 Comments

%%# value is coming NULL, its just listing current directory files.
Its coming Like this(i have use ech dir "%%~#"): c:\batch>com.bat "c:\Program Files,c:\John Snow" dir ""c:\Program" dir "Files","c:\John" dir "Snow""" Its taking space as delimiter.
@ThejeshPR are you passing the line surrounded by double quotes?

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.