Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a file rev.c which have a content be like that :
C++
///some commented lines
//.....................
//.....................

#include<string.h>
#include<abc.h>



const int dem=10



#if ((REV >= 100) && (REV <= 200) )         
const char ver1 = {"v1.8"};     
#endif
#if( (REV >= 300) && (REV <= 400) )
const char ver1 = {"v1.8"};        
#endif

so I replace the value inside the condition from {"v1.8"}; to {"v1.9"};

C++
#if ((REV >= 100) && (REV <= 200) )         
const char ver1 = {"v1.8"};

I can able to do that but the space between the lines are removed from the line
BAT
SetLocal EnableDelayedExpansion

Set _PathtoFile=D:\headercpp\rev.c
Set _OldLine=ver1 = {"v1.8"};
Set _NewLine=ver2 = {"v1.9"};

:: End of Search parameters
Call :_Parse "%_PathtoFile%"
Set _Len=0
Set _Str=%_OldLine%
Set _Str=%_Str:"=.%987654321
:_Loop

If NOT "%_Str:~18%"=="" Set _Str=%_Str:~9%& Set /A _Len+=9& Goto _Loop
Set _Num=%_Str:~9,1%
Set /A _Len=_Len+_Num
PushD %_FilePath%
If Exist %_FileName%.new Del %_FileName%.new
If Exist %_FileName%.old Del %_FileName%.old
Set _LineNo=0
For /F "Tokens=* Eol=" %%I In (%_FileName%%_FileExt%) Do (
Set _tmp=%%I
Set /A _LineNo+=1
If /I "!_tmp:~0,%_Len%!"=="%_OldLine%" (
>>%_FileName%.new Echo %_NewLine%
) Else (
If !_LineNo! GTR 1 If "!_tmp:~0,1!"=="[" Echo.>>%_FileName%.new
SetLocal DisableDelayedExpansion
>>%_FileName%.new Echo %%I
EndLocal
))
Ren %_FileName%%_FileExt% %_FileName%.old
Ren %_FileName%.new %_FileName%.c

PopD

Goto :EOF

:_Parse
Set _FilePath=%~dp1
Set _FileName=%~n1
Set _FileExt=%~x1
::Set _UpdatedFileName=Newtextupdated
Goto :EOF


What I have tried:

I tried to replace the particular string from c file it replace it the string but also remove the empty lines from the file while running .batch file.I just want to replace the string and doesn't affect other code.How to handle it ?
Posted
Updated 28-Oct-22 0:26am
v2

1 solution

You could do it quite easily with this Powershell script. It creates a new file (rev.2.c) to ensure the original does not get destroyed.
PowerShell
$oldversion = "v1.8"
$newversion = "v1.9"

Get-Content -Path "rev.c" | ForEach { $_ -replace $oldversion, $newversion } | Set-Content -Path "rev2.c"
 
Share this answer
 
Comments
Radha sharma 2022 28-Oct-22 6:48am    
@richard MacCutchan Thanks for the solution, IS it not possible through to replace the particular string from .bat file without affecting other code ?
or we can call powershell by passing arguments from .bat file.
I am new for .batch file and powershell and I don't have much knowledge about it
Richard MacCutchan 28-Oct-22 7:44am    
I cannot find any batch command that will do it. In the old MS-DOS days you could use edlin, but that does not seem to be available any more. A rather wider question for me is what actual problem are you trying to solve? Changing some text in a single file is a trivial issue for any editor such as Notepad.
Radha sharma 2022 28-Oct-22 7:56am    
I am trying to replace a string from .C file and my code is work for it but it changes file formatting(like removing space from the file) and take time to read the file.
Richard MacCutchan 28-Oct-22 8:05am    
Then just use an editor, it will take a couple of seconds.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900