Find and Replace the contents of text file.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Johan
Posts: 2
Joined: 27 Jan 2014 13:35

Find and Replace the contents of text file.

#1 Post by Johan » 27 Jan 2014 13:59

Hi,
In a folder I have lots of subfolders and my range to modify the text file starts from subfolder 000 to 050 & leave all other subfolders. I want to modify the text file named data.txt from this subfolders 000-050
Folder D:\Receipts
folder- 000
folder- 001
folder- 002
...
Needs to modify the text file from line no. 2 (leaving the first line its header). To modify the first 3 digits if its 302 then its fine and if its something else then 302 then modify it to 302.
Example:
some company xyz 27012013
30287921xxxx
30287822xxxx
10387823xxxx <---- Replace the first three digits to 302.

Help.

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

Re: Find and Replace the contents of text file.

#2 Post by foxidrive » 27 Jan 2014 18:00

Try this in a folder of test folders and files.

This uses a helper batch file called `repl.bat` - download from: https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat
Place `repl.bat` in the same folder as the batch file or in a folder that is on the path.

Code: Select all

@echo off
setlocal enabledelayedexpansion
for /L %%a in (1000,1,1050) do (
set "num=%%a"
set "folder=folder- !num:~-3!"
if exist "%folder%\data.txt" type "%folder%\data.txt"|repl "^[0-9][0-9][0-9]" "302" >"%folder%\data.txt.tmp"
move "%folder%\data.txt.tmp" "%folder%\data.txt"
)

Johan
Posts: 2
Joined: 27 Jan 2014 13:35

Re: Find and Replace the contents of text file.

#3 Post by Johan » 29 Jan 2014 00:11

Hello,

Thanks for your kind response, Foxidrive.

Infact I tested the script with repl.bat and it replaces the first 3 digits in folder but not in sub-folders.

This is my folder tree.

D:\Recipts\Zone\dd-mm-yy\Report [here the dd-mm-yy is current system date]
000 [folder named '000' containing text file named data.txt]
001 [folder named '001' containing text file named data.txt]
002 [folder named '002' containing text file named data.txt]
modification of text files upto folder named '050'

I modified it to suit my condition but unable to achieve the goal.
Please help.

Post Reply