Find & Replace string in all files within the current folder

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Dmmn
Posts: 5
Joined: 14 Nov 2012 04:00

Find & Replace string in all files within the current folder

#1 Post by Dmmn » 14 Nov 2012 04:15

Hi guys,
first of all, please excuse my numb language in batch scripting, I'm obviously newbie :).
What I'm trying to do is do a find&replace string on some specified files within the current directory. (the directory may contain more files than the ones I'm writing in). Basically run the batch in that folder (so I don't need to enter any input, inside the batch I will specify what file type I want to change).
So far I came up with a trivial code that seeks some file types I mention:

Code: Select all

@cls
@for /r %%x in (*.txt) do @echo. && @echo %%x
@for /r %%y in (*.xxx) do @echo. && @echo %%y
@for /r %%z in (*.yyy) do @echo. && @echo %%z
@pause


Next, I found the following link http://www.dostips.com/?t=batch.findandreplace that does a simple find & replace in a specified text file.
I wanted to combine the two as it follows: search for all specified file types, put the results in an array, then with a for, take all the elements and do a find & replace on them using the code in the script. Easy to say than done, the syntax is killing me, maybe a little help? please? thanks!

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Find & Replace string in all files within the current fo

#2 Post by foxidrive » 14 Nov 2012 06:15

The funny bit is that you are telling us how to do it, but you have no idea how to do it.
Why use an array? is my first question.

If you tell us exactly what you want to do, and what filetypes to use, and the directory depth, then let us decide how to do it.

In your example you use a recursive scan but in your explanation you say only the current directory.
Be accurate and complete and you will get the best batch for the job.

Dmmn
Posts: 5
Joined: 14 Nov 2012 04:00

Re: Find & Replace string in all files within the current fo

#3 Post by Dmmn » 14 Nov 2012 06:40

foxidrive wrote:The funny bit is that you are telling us how to do it, but you have no idea how to do it.
Why use an array? is my first question.

If you tell us exactly what you want to do, and what filetypes to use, and the directory depth, then let us decide how to do it.

In your example you use a recursive scan but in your explanation you say only the current directory.
Be accurate and complete and you will get the best batch for the job.


Hi Foxi, you have a point there, my bad, sorry for the confusing phrasing. Here are the details you requested:

Why use an array?

By default, in my thinking, I wanted to exclude the store in file method (using an output file). The first option that came into my mind was an array.

If you tell us exactly what you want to do, and what filetypes to use

Best way to explain it is this example:
I have a folder which stores in it 10 application files (.exe, .msi), for each file there is a description file, or a review. Its usual extension is .log, .txt, .rev, .rep (usually they are chosen by a third party) or other easy-to-read-as-text file (which can be interpreted as a text file). I want to replace in all these description files various key words like author (Jean instead of John). (I can define the keyword to replace inside the script)


and the directory depth,

Indeed, another mistake of mine, I saw that I'm searching recursive in the directory. Usually the folders don't contain recursive directories, but good point, I'm now considering this specification.

In your example you use a recursive scan but in your explanation you say only the current directory.

As stated above, the directory may contain child folders, so yes, I would make my script cover this option as well.

Be accurate and complete

Always doing my best to improve this aspect.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Find & Replace string in all files within the current fo

#4 Post by foxidrive » 14 Nov 2012 06:46

Dmmn wrote:
If you tell us exactly what you want to do, and what filetypes to use

Best way to explain it is this example:
I have a folder which stores in it 10 application files (.exe, .msi), for each file there is a description file, or a review. Its usual extension is .log, .txt, .rev, .rep (usually they are chosen by a third party) or other easy-to-read-as-text file (which can be interpreted as a text file). I want to replace in all these description files various key words like author (Jean instead of John). (I can define the keyword to replace inside the script)


So you have a set of application files. And an associated set of text files.

Does each application file have only one text file?

Is the application and text file the same filename, with different extensions?

Can you use a free third party tool for the search/replace?

Dmmn
Posts: 5
Joined: 14 Nov 2012 04:00

Re: Find & Replace string in all files within the current fo

#5 Post by Dmmn » 14 Nov 2012 07:03

foxidrive wrote:
Dmmn wrote:
If you tell us exactly what you want to do, and what filetypes to use

Best way to explain it is this example:
I have a folder which stores in it 10 application files (.exe, .msi), for each file there is a description file, or a review. Its usual extension is .log, .txt, .rev, .rep (usually they are chosen by a third party) or other easy-to-read-as-text file (which can be interpreted as a text file). I want to replace in all these description files various key words like author (Jean instead of John). (I can define the keyword to replace inside the script)


So you have a set of application files. And an associated set of text files.

Does each application file have only one text file?

Is the application and text file the same filename, with different extensions?

Can you use a free third party tool for the search/replace?


Please find answers below:

So you have a set of application files. And an associated set of text files.
Does each application file have only one text file?

It's odd that you're focusing on that aspect :) No, it's not necessarily a must that each application file has a corresponding text file. I'm only focusing on the text-like files and disregarding the application-type ones.

Is the application and text file the same filename, with different extensions?

Nope

Here's a more detailed example:

002495.exe
rev-002495.01042012.txt
002234.exe
BI_NJ_984_CI_S.msi
app_log_rev.BI_NJ_984_CI_S.22102011.log
BI_NJ_989_DI_T.msi
app_log_rev.BI_NJ_984_CI_S.10122011.log
fxfv995310900.exe
2nd_results_attempt_fxfv995310900_15092012.rep
review_this.please.80041993.exe

Now, let's say all non-executable files contain a similar structure (this is not relevant). I want to collect them all in a search:
rev-002495.01042012.txt
app_log_rev.BI_NJ_984_CI_S.22102011.log
app_log_rev.BI_NJ_984_CI_S.10122011.log
2nd_results_attempt_fxfv995310900_15092012.rep

now, in all of these I would want to find the string "John" and replace it with "Jean".

Inside the script I will search for all extension types, and collect whatever I find, I wouldn't find a breaking point the fact that one of my file types was not found in the initial search.

Can you use a free third party tool for the search/replace?

Nope, tried, searched for such, no luck, total commander only finds the string and displays the results, other can do this but process one file at a time, which is killing me in case of hundreds of files. I'm planning on using this on win OS, where no perl/python/unix is present so my tool-script must be an executable (.bat, .cmd, .exe) file.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Find & Replace string in all files within the current fo

#6 Post by foxidrive » 14 Nov 2012 07:18

Dmmn wrote:
Here's a more detailed example:

002495.exe
rev-002495.01042012.txt
002234.exe
BI_NJ_984_CI_S.msi
app_log_rev.BI_NJ_984_CI_S.22102011.log
BI_NJ_989_DI_T.msi
app_log_rev.BI_NJ_984_CI_S.10122011.log
fxfv995310900.exe
2nd_results_attempt_fxfv995310900_15092012.rep
review_this.please.80041993.exe

Now, let's say all non-executable files contain a similar structure (this is not relevant). I want to collect them all in a search:
rev-002495.01042012.txt
app_log_rev.BI_NJ_984_CI_S.22102011.log
app_log_rev.BI_NJ_984_CI_S.10122011.log
2nd_results_attempt_fxfv995310900_15092012.rep

now, in all of these I would want to find the string "John" and replace it with "Jean".


So you want to *exclude* all executable types and process every other type. Why didn'cha say so? :D

Can you use a free third party tool for the search/replace?


This was a question regarding GNUsed. Can you download that for use in the script?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Find & Replace string in all files within the current fo

#7 Post by foxidrive » 14 Nov 2012 07:54

For all the non-executable files in the folder, and in directories below it, do you want to make the same change to every file??

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Find & Replace string in all files within the current fo

#8 Post by Aacini » 14 Nov 2012 07:55

The Batch file below is a quick and dirty solution that find all files in current directory that have anyone of several extensions given in a FOR command set, and process each file found with the program you indicate in your link above:

Code: Select all

@echo off
for %%e in (log txt rev rep) do (
   for %%f in (*.%%e) do (
      call BatchSubstitute "OldStr" "NewStr" "%%f"
   )
)

Several improvements may be done to this program. For example, to also process subfolders, just add /R option to second FOR command.

I hope it helps...

Dmmn
Posts: 5
Joined: 14 Nov 2012 04:00

Re: Find & Replace string in all files within the current fo

#9 Post by Dmmn » 15 Nov 2012 01:42

Aacini wrote:The Batch file below is a quick and dirty solution that find all files in current directory that have anyone of several extensions given in a FOR command set, and process each file found with the program you indicate in your link above:

Code: Select all

@echo off
for %%e in (log txt rev rep) do (
   for %%f in (*.%%e) do (
      call BatchSubstitute "OldStr" "NewStr" "%%f"
   )
)

Several improvements may be done to this program. For example, to also process subfolders, just add /R option to second FOR command.

I hope it helps...


Hi Aacini, yes, it gave a clue of how to use the replace script and that's great, thank you.
One more question,
can you help me identify in the replace script below the in parameters? I would like to have your part of code along with the script in one batch file. Therefore, I was thinking of inputting the oldstr and newstr manually (hardcode them) and the "File" param would be the output one in your for, the %%f variable I suppose. So I cannot identify in the actual script the three variables :).
Thanks for your efforts!

Code: Select all

@echo off
REM -- Prepare the Command Processor --
SETLOCAL ENABLEEXTENSIONS
SETLOCAL DISABLEDELAYEDEXPANSION

::BatchSubstitude - parses a File line by line and replaces a substring"
::syntax: BatchSubstitude.bat OldStr NewStr File
::          OldStr [in] - string to be replaced
::          NewStr [in] - string to replace with
::          File   [in] - file to be parsed
:$changed 20100115
:$source http://www.dostips.com
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
    )
Last edited by Dmmn on 15 Nov 2012 02:24, edited 1 time in total.

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Find & Replace string in all files within the current fo

#10 Post by Aacini » 15 Nov 2012 03:47

Dmmn wrote:can you help me identify in the replace script below the in parameters? I would like to have your part of code along with the script in one batch file. Therefore, I was thinking of inputting the oldstr and newstr manually (hardcode them) and the "File" param would be the output one in your for, the %%f variable I suppose. So I cannot identify in the actual script the three variables :).
That is precisely the goal of this line:

Code: Select all

      call BatchSubstitute "OldStr" "NewStr" "%%f"
Perhaps I should write it this way?

Code: Select all

      call BatchSubstitute "hardcode here your OldStr" "hardcode here your NewStr" "%%f"
:)
To complete your Batch file, follow these steps:
    - Place my code first. Below first line insert SETLOCAL DISABLEDELAYEDEXPANSION line.
    - Insert a colon before subroutine name in my call (CALL :BATCHSUBSTITUTE)
    - IMPORTANT: insert GOTO :EOF line at end of my code!
    - Then insert the replace string script. In the line with the subroutine name, delete one colon and fix the subroutine name: :BatchSubstituTe
    - Insert EXIT /B line at end

And that is it! (please, don't ask me to show you the complete final script) :wink:

Antonio

Dmmn
Posts: 5
Joined: 14 Nov 2012 04:00

Re: Find & Replace string in all files within the current fo

#11 Post by Dmmn » 15 Nov 2012 03:52

Aacini wrote:
Dmmn wrote:can you help me identify in the replace script below the in parameters? I would like to have your part of code along with the script in one batch file. Therefore, I was thinking of inputting the oldstr and newstr manually (hardcode them) and the "File" param would be the output one in your for, the %%f variable I suppose. So I cannot identify in the actual script the three variables :).
That is precisely the goal of this line:

Code: Select all

      call BatchSubstitute "OldStr" "NewStr" "%%f"
Perhaps I should write it this way?

Code: Select all

      call BatchSubstitute "hardcode here your OldStr" "hardcode here your NewStr" "%%f"
:)
To complete your Batch file, follow these steps:
    - Place my code first. Below first line insert SETLOCAL DISABLEDELAYEDEXPANSION line.
    - Insert a colon before subroutine name in my call (CALL :BATCHSUBSTITUTE)
    - IMPORTANT: insert GOTO :EOF line at end of my code!
    - Then insert the replace string script. In the line with the subroutine name, delete one colon and fix the subroutine name: :BatchSubstituTe
    - Insert EXIT /B line at end

And that is it! (please, don't ask me to show you the complete final script) :wink:

Antonio

Got it, works like a charm, many thanks!
I'm used to declaring procedures, functions and so on in other languages, so I didn't know how they are declared in batch.
Again, thank you!

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Find & Replace string in all files within the current fo

#12 Post by Aacini » 27 Nov 2012 05:30

Hey Dmmm, did you deleted your story? Did you requested someone to delete your story?

In this topic there was a funny story that Dmmm posted as a joke. I entered here to read it again, but it was deleted! I understand that normal users can NOT delete their own posts if someone has replied to they, so I must do this question again: What happened here? Did someone deleted Dmmm's reply with no authorization nor any explanation?

Please, don't answer that this situation "happens... all the time" in this site like before. If this is true, I will no longer post new topics/replies in this site anymore!!!

(I hope this post will not be deleted by someone!)

Antonio

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Find & Replace string in all files within the current fo

#13 Post by foxidrive » 27 Nov 2012 06:49

Nothing related to batch files is removed, with the exception of many of billrich's posts which are often irrelevant.

I expect your post above will be removed, along with this one too as they aren't batch related.
You can always PM the fellow for the joke.

Post Reply