1

I am attempting to create a batch for loop (Windows XP and newer command prompts) that iterates through a string containing one or more asterisks. How can I do this? Here is an example:

@FOR %%A IN (A* B) DO @ECHO %%A

The expected output (what I am trying to get) is the following:

A*  
B

However, what I am actually getting with the command above is B and only B. For some reason, everything with an asterisk is being ignored by the loop. I have attempted escaping the asterisk with 1-4 carets (^), backslashes (\), percent signs (%), and other asterisks (*), all to no avail. Thanks in advance for illuminating me.

IN CASE YOU WANT MORE INFORMATION:

The purpose of this is to parse a path out of a list of space-separated partial paths. For example, I want to copy C:\Bar\A.txt, C:\Bar\B.txt, and C:\Bar\C*.txt to C:\Foo\ using the following approach:

@SET FILE_LIST=A B C*  
@FOR %%A IN (%FILE_LIST%) DO @COPY C:\Bar\%%A.txt C:\Foo\

If there is another alternative way to do this (preferably without typing each and every copy command since there are ~200 files, which is the same reason I don't want to store the full path for every file), I would appreciate the help. Thanks again,

-Jeff

1
  • Do you need to copy ALL files in the Bar folder? Or do you have only certain files you wish to copy? If there are only certain files, do they share a common name? Are you moving all .txt files? You can explain to us what your real-life application is. We promise we won't track you down to steal your data =D. Except maybe Jeb =/ Commented Sep 23, 2011 at 14:11

2 Answers 2

2

the asterisks works the way its intended, in your case,

@FOR %%A IN (A* B) DO @ECHO %%A

expands A* to all the files that begin with A.

A possible way to do what you want, is just to use this expansion

@ECHO off
PUSHD C:\bar
SET FILE_LIST=A.txt B.txt C*.txt
FOR %%A IN (%FILE_LIST%) DO (
  IF EXIST %%A COPY %%A C:\Foo\
)
POPD
Sign up to request clarification or add additional context in comments.

Comments

1

This may help:

@echo off
set "it=a*b .txt-b*.txt-c*.txt-d.txt"
set /a i=0,fn=3
:startLoop
set /a i=i+1
for /f "tokens=%i%delims=-" %%m in ("%it%") do echo %%m
if %i% lss %fn% goto:startLoop

Comments

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.