I'd like to avoid using a batch file, if possible.

Based on this answer to a question about recursive renaming or moving, I've came up with the following command (for copying all files named web.foo.config to web.config in the same directory):

for /r %x in (*web.foo.config) do copy /y "%x" web.config

However, that just caused every instance of web.foo.config to create and then overwrite .\web.config, not the web.config in the found path. So I tried:

for /r %x in (*web.foo.config) do (SET y=%x:foo.config=config% && CALL copy /y "%x" "%y")

This has the unfortunate effect of copying the files to a file named "%y". Is there a way to force %y to be evaluated after it's set... or a better method altogether?


Copying all files named web.foo.config to web.config in the same directory

Since you don't want to use a batch file for this operation then from an elevated command prompt, you can use the below to complete this.

This assumes the directory you are in when you run the command from the command prompt is the one which will be traversed through recursively doing the copy command of the found files.

I left the asterisk (*) from the beginning of the web.foo.config file name but you can add that where needed if it's really needed to find files with that naming pattern.

Using Copy Example

FOR /F "TOKENS=*" %F IN ('DIR /B /S web.foo.config') DO COPY /Y "%~F" "%~DPFweb.config"

Using Xcopy Example

FOR /F "TOKENS=*" %F IN ('DIR /B /S web.foo.config') DO ECHO F | XCOPY /Y /F "%~F" "%~DPFweb.config"

Further Resources

  • FOR /F
  • FOR /?

    In addition, substitution of FOR variable references has been enhanced. You can now use the following optional syntax:

    %~I         - expands %I removing any surrounding quotes (")
    %~fI        - expands %I to a fully qualified path name
    %~dI        - expands %I to a drive letter only
    %~pI        - expands %I to a path only
    %~nI        - expands %I to a file name only
    %~xI        - expands %I to a file extension only
    %~sI        - expanded path contains short names only
    %~aI        - expands %I to file attributes of file
    %~tI        - expands %I to date/time of file
    %~zI        - expands %I to size of file
    %~$PATH:I   - searches the directories listed in the PATH
                   environment variable and expands %I to the
                   fully qualified name of the first one found.
                   If the environment variable name is not
                   defined or the file is not found by the
                   search, then this modifier expands to the
                   empty string
  • 2
    Thanks, much better than the solution I was about to post: FOR /R %x IN (*web.foo.config) DO FOR /F "delims=." %y IN ("%x") DO COPY /Y "%x" "%y.config", which works as long as there are no periods in the path. – Mike Godin Jan 20 '17 at 17:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.