0

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
    )
)
Bris75
  • 3
  • 3
  • 1
    So the trouble part is 90% of the code... Try to narrow it down. – wOxxOm Nov 03 '15 at 13:11
  • 1
    You have `GOTO:EOF` inside the big loop but `goto` breaks loops. – wOxxOm Nov 03 '15 at 13:17
  • Thanks, removed the `GOTO:EOF` but still no success – Bris75 Nov 03 '15 at 13:22
  • If I replace the lines `set txtfile=o:\%%a\Ta*.bat` and `set newfile=b:\%%a\Ta*.bat` with `txtfile=o:\afstem05\Ta\name.bat` and `set newfile=b:\afstem05\Ta\name.bat` and get rid of the code preceding `SETLOCAL ENABLEDELAYEDEXPANSION` it Works just fine. The problems seems to be that I am not correctly formatting the code to search and replace in a series of files – Bris75 Nov 03 '15 at 13:24
  • Have a look at http://stackoverflow.com/questions/127318/is-there-any-sed-like-utility-for-cmd-exe – lit Nov 03 '15 at 13:36

1 Answers1

0

In the for loop you should use !var! to get the actual value of var, so

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=*" %%Z in (!txtfile!) do (
    set newline=%%Z
    call set newline=%%newline:!search!=!replace!%%
    echo !newline! >>!newfile!
)

Update.

set txtfile=o:\%%a\Ta\*.bat
set newfileprefix=b:\%%a\Ta\
...
for %%F in (!txtfile!) do (
    for /f "tokens=*" %%Z in (%%F) do (
        set newline=%%Z
        call set newline=%%newline:!search!=!replace!%%
        echo !newline! >>!newfileprefix!%%F
    )
)
Dmitry Sokolov
  • 3,118
  • 1
  • 30
  • 35
  • Thank you, that certainly helped a lot. There is still a problem though. I am very new to programming so I might be wrong but it seems to be a problem with `set txtfile=o:\%%a\Ta\*.bat` Maybe the set command doesn't Work with wildcards? – Bris75 Nov 04 '15 at 17:43
  • `set txtfile=o:\%%a\Ta\*.bat` sets the file mask. To process files by mask you need another loop. See update. – Dmitry Sokolov Nov 05 '15 at 09:14
  • Once Again thank you very much for your help. I have made two canges in the code to make it Work. In the line `echo !newline! >>!newfileprefix!%%F` the `%%F` refers to path and file name so I added `~nx` to just get the file name and extension. I also changed the code deleting the destination file if it already exists. Please see update. – Bris75 Nov 05 '15 at 12:54
  • Looks like changes were rejected. You should move your changes into your original post (question) as an update. – Dmitry Sokolov Nov 05 '15 at 16:12
  • I've added the changes as an update to my original post. Thank you very much for your help both with the code and how to post on stackoverflow – Bris75 Nov 06 '15 at 09:56