0

I am trying to write a batch file to find and replace a string in multiple files within a folder. But I am getting this error:

Cannot perform a cyclic copy

Any idea why that happens?

@echo off
SETLOCAL
for %%* in (.) do set foldername=%%~n*
SET stringtofindreplace=XXXX
for %%f in (*.fmw) do (
    echo Processing %%f...
    fOR /F "delims=" %%l IN (%%f) DO (
         SET "line=%%l"
         SETLOCAL ENABLEDELAYEDEXPANSION 
         set "x=!line:%stringtofindreplace%=%foldername%!" 
         echo(!x!
         ENDLOCAL)
    )>%%~nf.new
)
GOTO:EOF
user1879324
  • 5
  • 1
  • 5

2 Answers2

0
@ECHO OFF
SETLOCAL

:: no idea what this is aimed at doing...??

for %%* in (.) do set new=%%~n*
SET new=newstring
SET old=XXXX
for %%f in (*.fmw) do (
    echo Processing %%f...
    (
    FOR /F "delims=" %%l IN (%%f) DO (
        SET "line=%%l"
        SETLOCAL ENABLEDELAYEDEXPANSION 
        set "x=!line:%old%=%new%!" 
        ECHO(!x!
    ENDLOCAL
    )
    )>%%~nf.new
)
GOTO :EOF

I've no idea what you are trying to do with the first for, so I just made an obvious replacement string.

You need to add the "delims=" option to deliver the entire line to %%l.

Make sure there are no trailing spaces on the ECHO(!x!

This will make a new file called *.new from each *.fmw file.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • I appreciate your help.the first 'for' gives the name of current directory that I can use it to replace "XXXX". that is great and working but it should make a new file called *.fmw from each edited *.fmw file. – user1879324 Mar 07 '14 at 13:35
  • The magic variable `%cd%` will give you the name of the current directory. Batch cannot update in-place; a new file must be created with upated contents. The `*.new` files created can then be compared easily. When you've verified that the processing is correct, a line suuch as `for %%f in (*.new) do move /y "%%f" "%%~nf.fmw"` should move the new files over the old. – Magoo Mar 07 '14 at 15:28
  • '%CD%' gives me the full path where i just need the name of directory.as you suggested,i use another batch file to move new files over the old.thank you – user1879324 Mar 07 '14 at 16:34
0

Cannot perform a cyclic copy error occurs when the source folder includes the target folder, and so is trying to copy all the files, including the files it has already copied.

This will give you that error.

xcopy c:\apple\*.* c:\apple\backup\ /s
foxidrive
  • 40,353
  • 10
  • 53
  • 68