2

Hi I have a script I made using information on this page.

I realize that running two separate batch files (:SetVarand:Brains) will work but when I try to combine the two into one It doesn't work.

I'd be happy if anyone can explain to me why mine does not work and show me what I can do to make mine work.

:SetVar
Brains "Hello" "Goodbye" "input.txt" > "newfile.txt"
GOTO :Brains

:Brains
SETLOCAL ENABLEEXTENSIONS
SETLOCAL DISABLEDELAYEDEXPANSION
if "%~1"=="" findstr "^::" "%~f0"&GOTO:EOF
for /f "tokens=1,* delims=]" %%A in ('"type %3|find /n /v """') do (
    set "line=%%B"
    if defined line (
        call set "line=echo.%%line:%~1=%~2%%"
        for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X
    ) ELSE echo.
)
PAUSE

When I use the Call command:

call :SetVar
call :Brains
goto :eof
pause

:SetVar
:Brains "Hello" "Goodbye" "input.txt" > "newfile.txt"
goto :eof

:Brains
SETLOCAL ENABLEEXTENSIONS
SETLOCAL DISABLEDELAYEDEXPANSION
if "%~1"=="" findstr "^::" "%~f0"&GOTO:EOF
for /f "tokens=1,* delims=]" %%A in ('"type %3|find /n /v """') do (
    set "line=%%B"
    if defined line (
        call set "line=echo.%%line:%~1=%~2%%"
        for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X
    ) ELSE echo.
)
goto :eof

I get no error but I get:

C:\Users\MALKAVIAN\Batch files\change words in file>CALL :SetVar

C:\Users\MALKAVIAN\Batch files\change words in file>goto :eof

C:\Users\MALKAVIAN\Batch files\change words in file>CALL :Brains

C:\Users\MALKAVIAN\Batch files\change words in file>goto :eof

C:\Users\MALKAVIAN\Batch files\change words in file>pause
Press any key to continue . . .
MALKAVIAN
  • 131
  • 14
  • What specifically "does not work" ? – geisterfurz007 Sep 20 '16 at 07:46
  • @geisterfurz007 - I would like to run the two scripts (in one `.bat` file instead of two) and have the same effect. – MALKAVIAN Sep 20 '16 at 07:52
  • I understood that part, but what does the wrong effect look like? Does the programm do nothing, brings it up wrong results/different from the other version. Btw I am curious what the second line is supposed to do. I am not really into batch, so this might be a dumb question... You could try to place a call in front of that second line (not instead of `GOTO` but the line above) – geisterfurz007 Sep 20 '16 at 07:59
  • Blank cmd prompt. I tried using the `call` command to call `:SetVa` and `:Brains` and i get press any key to continue but no errors – MALKAVIAN Sep 20 '16 at 08:10
  • I ment as followed: In the first codeblock of your question you have this code: `Brains "Hello" "Goodbye" "input.txt" > "newfile.txt"` Try changing it to this: `call :Brains "Hello" "Goodbye" "input.txt"` EDIT: I tested it and it echoes all lines from the input.txt. I am not sure if that is the desired outcome, but that is what it does with the change. – geisterfurz007 Sep 20 '16 at 08:22
  • @geisterfurz007 - I get a infinite loop of `The system cannot find the path specified. The process cannot access the file because it is being used by another process.` – MALKAVIAN Sep 20 '16 at 08:28
  • Would you mind updating your question with the current code? – geisterfurz007 Sep 20 '16 at 08:29
  • Sure if you think so! The code I have works for me. If you are not interested in an answer based on the age of the person answering you mind want to consider deleting this question. – geisterfurz007 Sep 20 '16 at 08:35

3 Answers3

2

You can try this batch file :

I added a fourth argument to output the results of substitution in new text file :

Edit

So as an example, you can find the substring "http" to be replaced by "https"

@echo off
set "NewStr=https"
set "InFile=%~dp0%~n0_InputTest.txt"
set "OutPutFile=%~dp0%~n0_OutPutTest.txt"
If Exist "%InFile%" Del "%InFile%"
If Not Exist "%InFile%" Call :CreateExampleInputFile
Call :FindReplace "" "%NewStr%" "%InFile%"
pause
set "OldStr=http"
Call :FindReplace "%OldStr%" "%NewStr%" "%InFile%" "%OutPutFile%"
Start "" "%OutPutFile%"
Pause & Exit /b
::**************************************************************
:FindReplace <OldStr> <NewStr> <InFile> <OutPutFile>
REM -- Prepare the Command Processor --
SETLOCAL ENABLEEXTENSIONS
SETLOCAL DISABLEDELAYEDEXPANSION
::FindReplace - parses a File line by line and replaces a substring"
::syntax: FindReplace <OldStr> <NewStr> <InFile> <OutPutFile>
::          OldStr [in] - string to be replaced
::          NewStr [in] - string to replace with
::          InFile [in] - file to be parsed
::          OutFile[out] - New output file
if "%~1"=="" findstr "^::" "%~f0"&GOTO:EOF
(
    for /f "tokens=1,* delims=]" %%A in ('"type %3|find /n /v """') do (
        set "line=%%B"
        if defined line (
            call set "line=echo.%%line:%~1=%~2%%"
            for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X
        ) ELSE echo.
    )
)>%4
Exit /b
::**************************************************************
:CreateExampleInputFile
(
    echo http://www.hyperdebrid.com
    echo http://www.fakirdebrid.net
    echo http://www.keepfiles.fr
    echo http://www.4shared.com 
    echo http://1fichier.com
    echo http://www.mega.co.nz
    echo http://www.mediafire.com
    echo http://www.uploaded.net
    echo http://www.oboom.com
    echo http://www.letitbit.net
    echo http://www.keep2share.cc
    echo http://alfafile.net
    echo http://www.bigfile.to
    echo http://www.dailymotion.com
    echo http://www.datafile.com
    echo http://www.Depfile.com
    echo http://www.Dropbox.com
    echo http://www.Extmatrix.com
    echo http://www.Fboom.me
    echo http://www.Filefactory.com
    echo http://www.Filesmonster.com
    echo http://www.Fshare.vn
    echo http://www.Keep2share.com
    echo http://www.Mega.nz
    echo http://www.Rapidgator.net
    echo http://www.Scribd.com
    echo http://www.Soundcloud.com
    echo http://www.Speedyshare.com
    echo http://www.Turbobit.net
    echo http://www.Vimeo.com
 )>"%InFile%"
 ::**************************************************************
Hackoo
  • 18,337
  • 3
  • 40
  • 70
1

I hope this helps you with your problem.

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS DISABLEDELAYEDEXPANSION

CALL :Brains "Hello" "Goodbye" "input.txt"
GOTO :EOF

:Brains
IF "%~1"=="" (FINDSTR/R "^::" "%~f0"
    PAUSE
    GOTO :EOF)
(FOR /F "TOKENS=1* DELIMS=]" %%A IN ('FIND /N /V ""^<%3') DO (IF "%%~B" EQU "" (
            ECHO=) ELSE (SET "line=%%B"
            CALL ECHO=%%line:%~1=%~2%%)))>"newfile.txt"
PAUSE
Compo
  • 36,585
  • 5
  • 27
  • 39
0
@echo off

:SetVar
call :Brains "Hello" "Goodbye" "input.txt"
GOTO :Brains

:Brains
SETLOCAL ENABLEEXTENSIONS
SETLOCAL DISABLEDELAYEDEXPANSION
if "%~1"=="" findstr "^::" "%~f0"&GOTO:EOF
for /f "tokens=1,* delims=]" %%A in ('"type %3|find /n /v """') do (
    set "line=%%B"
    if defined line (
        call set "line=echo.%%line:%~1=%~2%%"
        for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X
    ) ELSE echo.
)
PAUSE

One last try cause I am a friendly person that wants to help... Take this code, place it in a batch-file and place the batch-file in the same directory as input.txt . Place text in that text-file, save it and run the script. It should echo all lines that are written in the document and it changes Hello to Goodbye in the output as well.

geisterfurz007
  • 5,292
  • 5
  • 33
  • 54