Hi I am trying to search and replace a string containing the username in a series of batch files in a series of folders. I.e. I have a series of batch files on a Network drive and need to transfer them to a local pc drive for some co-workers and in the process I need to change the username in the batch file to match the username of the local PC. And the username must be in all uppercase letters. To do this I use a piece of code I used to do the same for a single file, but I have problems converting the code to do the same for a series of files in a series of folders. I am working on a windows 7 machine. The code I have tried is as follows (please not that the first piece of code mirrors the tree structure of the Network drive (O:-drive) to the local drive (B:-drive) and copies the files that don't need to be changed in the process of moving. The part giving me troubles starts from the line SETLOCAL ENABLEDELAYEDEXPANSION):
for /f "delims=" %%a in ('dir /b/ad "o:\afstem*" ') do (
echo m | xcopy /t /e /y "o:\%%a\Ta" "b:\%%a\Ta"
mkdir "b:\%%a\Nyta"
echo f | xcopy /s /y /d "o:\%%a\Ta\*moms" "b:\%%a\Ta\*moms"
SETLOCAL ENABLEDELAYEDEXPANSION
SET String=%username%
CALL :UpCase String
set txtfile=o:\%%a\Ta\*.bat
set newfile=b:\%%a\Ta\*.bat
if exist "%newfile%" del /f /q "%newfile%"
set search=SHL
set replace=%String%
for /f "tokens=*" %%a in (%txtfile%) do (
set newline=%%a
call set newline=%%newline:%search%=%replace%%%
call echo %%newline%% >>%newfile%
)
ENDLOCAL
GOTO:EOF)
:UpCase
:: Subroutine to convert a variable VALUE to all upper case.
:: The argument for this subroutine is the variable NAME.
SET %~1=!%1:a=A!
SET %~1=!%1:b=B!
SET %~1=!%1:c=C!
SET %~1=!%1:d=D!
SET %~1=!%1:e=E!
SET %~1=!%1:f=F!
SET %~1=!%1:g=G!
SET %~1=!%1:h=H!
SET %~1=!%1:i=I!
SET %~1=!%1:j=J!
SET %~1=!%1:k=K!
SET %~1=!%1:l=L!
SET %~1=!%1:m=M!
SET %~1=!%1:n=N!
SET %~1=!%1:o=O!
SET %~1=!%1:p=P!
SET %~1=!%1:q=Q!
SET %~1=!%1:r=R!
SET %~1=!%1:s=S!
SET %~1=!%1:t=T!
SET %~1=!%1:u=U!
SET %~1=!%1:v=V!
SET %~1=!%1:w=W!
SET %~1=!%1:x=X!
SET %~1=!%1:y=Y!
SET %~1=!%1:z=Z!
GOTO:EOF
Thank you in advance for any help/suggestions
Update
Thanks to Dmitry Sokolow for the code below which solved my problem
set txtfile=o:\%%a\Ta\*.bat
set newfileprefix=b:\%%a\Ta\
set search=SHL
set replace=!String!
for %%F in (!txtfile!) do (
if exist "!newfileprefix!%%~nxF" del /f /q "!newfileprefix!%%~nxF"
for /f "tokens=*" %%Z in (%%F) do (
set newline=%%Z
call set newline=%%newline:!search!=!replace!%%
echo !newline! >>!newfileprefix!~nx%%F
)
)