Computing.Net > Forums > Programming > Search And Replace Strings In Files

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Search And Replace Strings In Files

Reply to Message Icon

Name: Gurushab
Date: July 26, 2009 at 11:00:44 Pacific
OS: Microsoft Windows XP Professional
CPU/Ram: 2.992 GHz / 1014 MB
Product: Dell / OPTIPLEX 210L
Subcategory: Batch
Comment:

I have multiple files in a DIR with one common extension .f. I have to do 3 things.
1) I have to partly rename the file
EX: If the file name is 0104_184934604_C529400896.f then i have to change it to C529400896.f.

2) The i have to erase a content inside the file:
EX: If i have
1
2
3
4
BEGIN

Then i have to search for the begin word and erase everything above it,

3) I have a common text in all the files and i have to replace with a New text
EX: I have to find this common text {{Findme}} and replace with {{Replace Me}}

Im a novice when it comes to scripting.
I would prefer to do this using a batchfile and any help would be greatly appreciated.


Please...............

Thanks
GT



Sponsored Link
Ads by Google

Response Number 1
Name: dtech10
Date: July 28, 2009 at 13:14:27 Pacific
Reply:

Hi Gurushab

Do all the files have BEGIN in them, and only one BEGIN not more.


0

Response Number 2
Name: Gurushab
Date: July 28, 2009 at 21:36:02 Pacific
Reply:

Hello Dtech10,

Yes, all the files have {{BEGIN}} in them.


0

Response Number 3
Name: Gurushab
Date: July 28, 2009 at 21:36:27 Pacific
Reply:

and only one BEGIN in each file


0

Response Number 4
Name: dtech10
Date: July 29, 2009 at 10:28:22 Pacific
Reply:

Hi Guru

I forgot to ask if the filename(s) are the same format
ie. If the file name is 0104_184934604_C529400896.f then i have to change it to C529400896.f.

Is there always underlines in the filenames or to make it easier to write is the C529400896 always the same length.
Both are OK.


0

Response Number 5
Name: Gurushab
Date: August 2, 2009 at 03:30:00 Pacific
Reply:

Hi Dtech10,

Yes, The file names are in the same format and and same length with Underscores. The only Constant value would be C5.

And

Yes, The length of values on the renamed files are always the same: For example:

Original File Name: 0104_184934604_C529400896.f
Renamed File should look like this: C529400896.f

Here is what i have so far:

@echo off
Title Que To Toll-Free
cd C:\testing\Q2F
echo Re-sending files to QueToTollFree
echo.
echo Listing all *.nt2 files
DIR
echo.
echo Copying files to Archive
copy /Y *.nt2 C:\testing\Q2F\Archive
echo.
echo Renaming files to *.F
ren *.nt2 *.F
echo.
echo.
echo Renaming files to C5
call renamer.bat
echo.
echo.
echo Changing file content (Yet to be implemented)
Pause

As you can see above i'm calling another batchfile called renamer.bat which looks like this:


@echo off

:main
for %%F in (*.f) do call :renamer %%F
goto :eof

:renamer
set old=%1
ren %1 %old:~-12%
:: end

Let me know if you have any questions. and thank you for looking into this.

Guru
GT



0

Related Posts

See More



Response Number 6
Name: dtech10
Date: August 2, 2009 at 11:54:25 Pacific
Reply:

Hi Guru

Try this.

@echo off
SetLocal EnableDelayedExpansion

dir /b *.f | find "_" > Files.txt

for /f "tokens=1-3 delims=_" %%a in (Files.txt) do (
set OrgName=%%a_%%b_%%c
set NewName=%%c
ren !OrgName! !NewName!
)


set Flag=0
dir /b *.f > Files.txt
for /f "tokens=* delims=" %%a in (Files.txt) do (
set Flag=0
for /f "tokens=* delims=" %%b in (%%a) do (
set Line=%%b
if "!Line!" EQU "BEGIN" (set Flag=1)
if !Flag! EQU 1 (echo %%b >> Tmp.txt)
)
del %%a
ren Tmp.txt %%a
)
del Files.txt
exit /b


0

Response Number 7
Name: Gurushab
Date: August 3, 2009 at 03:48:48 Pacific
Reply:

Hello Dtech10,

Thanks a lot for your reply and script. It worked. I have made some minor changes to suit my system settings and dir settings.

Once again thanks a lot

Regards,
Guru


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Search And Replace Strings In Files

find and replace string in a file www.computing.net/answers/programming/find-and-replace-string-in-a-file/19987.html

Search and Replace String in variable www.computing.net/answers/programming/search-and-replace-string-in-variable/19677.html

Perl search and replace www.computing.net/answers/programming/perl-search-and-replace/14586.html