Search And Replace Strings In Files

Dell / OPTIPLEX 210L
July 26, 2009 at 11:00:44
Specs: Microsoft Windows XP Professional, 2.992 GHz / 1014 MB
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


See More: Search And Replace Strings In Files

Report •


#1
July 28, 2009 at 13:14:27
Hi Gurushab

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


Report •

#2
July 28, 2009 at 21:36:02
Hello Dtech10,

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


Report •

#3
July 28, 2009 at 21:36:27
and only one BEGIN in each file

Report •

Related Solutions

#4
July 29, 2009 at 10:28:22
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.


Report •

#5
August 2, 2009 at 03:30:00
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



Report •

#6
August 2, 2009 at 11:54:25
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


Report •

#7
August 3, 2009 at 03:48:48
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


Report •


Ask Question