Batch replace a language file using grep? - batch-file

I am trying to find/replace a long language file:
$_LANG['accountinformation'] = "Account Information";
$_LANG['accountstatistics'] = "Account Statistics";
to become:
$_LANG['accountinformation'] = "Informations du compte";
$_LANG['accountstatistics'] = "Statistiques du compte";
knowing that I have the whole translation in a file like this:
Informations du compte
Statistiques du compte
Is there a way to replace these phrases with a text editor such as BBedit using grep or something instead of copy/pasting each one of them by hand?

Here is a way to do it using a batch/vbs hybrid script:
#echo off
setlocal
call :FindReplace "Account Statistics" "Statistiques du compte" Thefile.txt
call :FindReplace "Account Information" "Informations du compte" Thefile.txt
exit /b
:FindReplace <findstr> <replstr> <file>
set tmp="%temp%\tmp.txt"
If not exist %temp%\_.vbs call :MakeReplace
for /f "tokens=*" %%a in ('dir "%3" /s /b /a-d /on') do (
for /f "usebackq" %%b in (`Findstr /mic:"%~1" "%%a"`) do (
echo(&Echo Replacing "%~1" with "%~2" in file %%~nxa
<%%a cscript //nologo %temp%\_.vbs "%~1" "%~2">%tmp%
if exist %tmp% move /Y %tmp% "%%~dpnxa">nul
)
)
del %temp%\_.vbs
exit /b
:MakeReplace
>%temp%\_.vbs echo with Wscript
>>%temp%\_.vbs echo set args=.arguments
>>%temp%\_.vbs echo .StdOut.Write _
>>%temp%\_.vbs echo Replace(.StdIn.ReadAll,args(0),args(1),1,-1,1)
>>%temp%\_.vbs echo end with
Change thefile.txt to your filename. Save the script as replace.cmd from a text editor and run it from the command line.

If you are trying to say that the files are aligned -- there is exactly one string definition per line in the English file, and the French contains the translation equivalences in the same order, one per line -- then a simple paste with some postprocessing should do it.
paste strings.def fr.txt |
sed 's/"[^"]*";\t\(.*\)/"\1";/' >output.txt
If your sed dialect doesn't support \t for tab, you'll have to supply a literal tab (ctrl-v tab in many shells).

Related

Replace all spaces and symbols(!#.,;&()) by dash(-) in all file names of a folder with batch?

I'm dealing with a lot of files, mostly .jpg, that need to be batch renamed for ftp use (5k+ per day). Need a batch file to convert filenames to lowercase and replace all spaces and symbols(!#.,;&()) with dashes(-), as an example:
POS!99/6 09.JPG -> pos-99-6-09.jpg
75#0hu dfs*1010.jpg -> 75-0hudfs-1010.jpg
Please, help!
I just tried to set files in lowercase and dont know how to do the rest:
#echo off
setlocal
for /f "Tokens=*" %%f in ('dir /l/b/a-d') do (rename "%%f" "%%f")
for /f "tokens=1,2,* delims=_." %%F in ('dir /b "*_*.*"') do ren "%%F_%%G.%%H" "%%F.%%H"
endlocal
del *.bat
This can do the trick using VBScript Replace RegEx into a batch file :
Note : The pattern to validate a filename is [\\\/:*?\x22<>|], and I added in this class to yours for your request !#.,;&()#
#echo off
Color 0A & Title How to verify if variable contains valid filename in Windows Batch
echo(
Echo Enter filename for this project
set /p "my_filename="
echo(
echo Before Removing the special char the filename is like this : "%my_filename%"
pause
echo(
Call :Remove_Special_Char "%my_filename%" NewFileName
echo After Removing the special char the filename becomes like this : "%NewFileName%"
pause & exit
::-----------------------------------------------------------------------------------
:Remove_Special_Char <String> <Variable to Set>
(
echo WScript.StdOut.WriteLine Search_Replace(Data^)
echo Function Search_Replace(Data^)
echo Dim strPattern, strReplace, strResult,oRegExp
echo Data = wscript.Arguments(0^)
echo strPattern = "[\\\/:*?\x22<>|!#.,;&()# ]"
echo strReplace = "-"
echo Set oRegExp = New RegExp
echo oRegExp.Global = True
echo oRegExp.IgnoreCase = True
echo oRegExp.Pattern = strPattern
echo strResult = oRegExp.Replace(Data,strReplace^)
echo Search_Replace = strResult
echo End Function
)>"%tmp%\%~n0.vbs"
#For /f "delims=" %%i in ('cscript //nologo "%tmp%\%~n0.vbs" "%~1"') do ( Set "%2=%%i" )
If Exist "%tmp%\%~n0.vbs" Del "%tmp%\%~n0.vbs"
Exit /B
::----------------------------------------------------------------------------------
Or using Powershell to replace RegEx into a batch file :
#echo off
Color 0A & Title How to verify if variable contains valid filename in Windows Batch
echo(
Echo Enter filename for this project
set /p "my_filename="
echo(
echo Before Removing the special char the filename is like this : "%my_filename%"
pause
echo(
Call :Remove_Special_Char "%my_filename%" NewFileName
echo After Removing the special char the filename becomes like this : "%NewFileName%"
pause & exit
::-----------------------------------------------------------------------------------
:Remove_Special_Char <String> <Variable to Set>
Set "psCommand='"%~1"' -replace '[\\\/:*?\x22<>|!#.,;&()# ]','-'"
#for /f "usebackq delims=" %%i in (`Powershell -C "%psCommand%"`) do (Set "%2=%%i")
Exit /B
::----------------------------------------------------------------------------------
EDIT :
#echo off
Color 0A & Title Rename
Set "MainFolder=E:\Batch\Stack\Test"
CD /D "%MainFolder%"
#for /f "Tokens=*" %%f in ('dir /l /b /a-d') do (
SetLocal DisableDelayedExpansion
Call :Remove_Special_Char "%%~f" NewVar
SetLocal EnableDelayedExpansion
echo( rename "%%~f" "!NewVar!"
pause
rename "%%~f" "!NewVar!"
)
EndLocal & pause & exit
::-----------------------------------------------------------------------------------
:Remove_Special_Char <String> <Variable to Set>
Set "psCommand=""""%~1"""" -replace '[\\\/:*?\x22<>|!#.,;&()\x20#]','-'"
#for /f "usebackq delims=" %%i in (`Powershell -C "%psCommand%"`) do (Set "%2=%%i")
Exit /B
::----------------------------------------------------------------------------------

Batch script closing parenthesis issue

I have the following batch script to replace the supplied text with some other text.
#echo off
setlocal
call :FindReplace %1 %2 %3
exit /b
:FindReplace <findstr> <replstr> <file>
set tmp="%temp%\tmp.txt"
If not exist %temp%\_.vbs call :MakeReplace
for /f "tokens=*" %%a in ('dir "%3" /s /b /a-d /on') do (
for /f "usebackq" %%b in (`Findstr /mic:"%~1" "%%a"`) do (
echo(&Echo Replacing "%~1" with "%~2" in file %%~nxa
<%%a cscript //nologo %temp%\_.vbs "%~1" "%~2">%tmp%
if exist %tmp% move /Y %tmp% "%%~dpnxa">nul
)
)
del %temp%\_.vbs
exit /b
:MakeReplace
>%temp%\_.vbs echo with Wscript
>>%temp%\_.vbs echo set args=.arguments
>>%temp%\_.vbs echo .StdOut.Write _
>>%temp%\_.vbs echo Replace(.StdIn.ReadAll,args(0),args(1),1,-1,1)
>>%temp%\_.vbs echo end with
Now, I am trying to automate the build and set up of my application. I invoke the above script from following batch.
#echo off
set a=C:\Program Files (x86)\Apache Software Foundation\Apache2.2\conf\httpd.conf
call Text_Replacer "branch-1" "branch-2" "%a%"
Due to the ')' in the path, I get the following in the console.
\Apache was unexpected at this time.
Please help me to escape the ')'.
This is one problem: change "%3" to "%~3"
It is why the characters in the path string are not protected, because %3 is already double quoted.

Editing file with a batch file

I have a small script to edit text in a .conf file.
SETLOCAL=ENABLEDELAYEDEXPANSION
rename c:\users\administrator\desktop\httpd.conf text.tmp
for /f %%a in (text.tmp) do (
set foo=%%a
if !foo!=="### Section 3: Virtual Hosts" set foo="SSL Compression off"
echo !foo! >> c:\users\administrator\desktop\httpd.conf)
del text.tmp
It doesn't have the desired effect as it seems to delete quiet a lot of data from the file. Is there an alternative way I can do this?
I just need to replace ### Section 3: Virtual Hosts with SSL Compression off, whilst maintaining the integrity of the file. The current script seems to delete spaces also :(
Many thanks
Try this:
#echo off
setlocal
call :FindReplace "### Section 3: Virtual Hosts" "SSL Compression off" httpd.conf
exit /b
:FindReplace <findstr> <replstr> <file>
set tmp="%temp%\tmp.txt"
If not exist %temp%\_.vbs call :MakeReplace
for /f "tokens=*" %%a in ('dir "%3" /s /b /a-d /on') do (
for /f "usebackq" %%b in (`Findstr /mic:"%~1" "%%a"`) do (
echo(&Echo Replacing "%~1" with "%~2" in file %%~nxa
<%%a cscript //nologo %temp%\_.vbs "%~1" "%~2">%tmp%
if exist %tmp% move /Y %tmp% "%%~dpnxa">nul
)
)
del %temp%\_.vbs
exit /b
:MakeReplace
>%temp%\_.vbs echo with Wscript
>>%temp%\_.vbs echo set args=.arguments
>>%temp%\_.vbs echo .StdOut.Write _
>>%temp%\_.vbs echo Replace(.StdIn.ReadAll,args(0),args(1),1,-1,1)
>>%temp%\_.vbs echo end with
#ECHO OFF
SETLOCAL
SET originalpath=.
SET originalname=%originalpath%\q20189766.txt
rename %originalname% text.tmp
(for /f "delims=" %%a in (%originalpath%\text.tmp) do (
if "%%a"=="### Section 3: Virtual Hosts" (ECHO(SSL Compression OFF
) ELSE (ECHO(%%a)
)
) >%originalname%
del %originalpath%\text.tmp
GOTO :EOF
I've replaced your pathname and filename with names to suit my system. You would need to replace the values assigned to originalpath and originalname to suit your system.
Note that ECHO( is deliberate - the ( in this character-sequence does NOT affect statement-grouping.
This uses a helper batch file called repl.bat 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.
The search term is a regular expression but your term should work ok.
#echo off
type text.tmp | repl "### Section 3: Virtual Hosts" "SSL Compression off" > c:\users\administrator\desktop\httpd.conf

Batch Script to Update text in the properties file

I have a question regarding the batch.
I have a properties file in Directory. Which has a text "BuildNumber=0". I want to replace this "BuildNumber=0" with "BuildNumber=%BUILD_NUMBER%". I am using the following script to achieve this
#echo on
setlocal enabledelayedexpansion enableextensions
ATTRIB -r -s C:\bldarea\myfile\..\..\jenkinstest\abc.properties
CD C:\bldarea\myfile\file\Main_Releases\jenkinstest\
call :FindReplace "Buildnumber=0" "Buildnumber=%BUILD_NUMBER%" abc.properties
exit /b
:FindReplace <Buildnumber=0> <Buildnumber=%BUILD_NUMBER%> <abc.properties>
set tmp="%temp%\tmp.txt"
If not exist %temp%\_.vbs call :MakeReplace
for /f "tokens=*" %%a in ('dir "%3" /s /b /a-d /on') do (
for /f "usebackq" %%b in (`Findstr /mic:"%~1" "%%a"`) do (
echo(&Echo Replacing "%~1" with "%~2" in file %%~nxa
<%%a cscript //nologo %temp%\_.vbs "%~1" "%~2">%tmp%
if exist %tmp% move /Y %tmp% "%%~dpnxa">nul
)
)
del %temp%\_.vbs
exit /b
:MakeReplace
>%temp%\_.vbs echo with Wscript
>>%temp%\_.vbs echo set args=.arguments
>>%temp%\_.vbs echo .StdOut.Write _
>>%temp%\_.vbs echo Replace(.StdIn.ReadAll,args(0),args(1),1,-1,1)
>>%temp%\_.vbs echo end with
The problem with this script is , It is not converting BuildNumber=0 to current Build Number.
From the line in code:
:FindReplace <Buildnumber=0> <Buildnumber=%BUILD_NUMBER%> <abc.properties>
if I remove % symbol then it is printing the "BuildNumber=%BUILD_NUMBER%" but the "BuildNumber=0" is still present in the doc. Can someone please help me out to replace the text correctly.
Thanks.
Just insert double percents in this line:
call :FindReplace "Buildnumber=0" "Buildnumber=%BUILD_NUMBER%" abc.properties
this way:
call :FindReplace "Buildnumber=0" "Buildnumber=%%BUILD_NUMBER%%" abc.properties

replace a number in .properties file through batch file

I have a .properties file say abc.properties file. It has a text BuildNumber=0. I want to search the BuildNumber and replacw its value with current build number through batch file.
Please if someone can help. I am new to batch scripting.
Any help would be appreciated.
Thanks
You can do it pretty easily with sed
#!/bin/bash
BUILD="1.1"
sed "s/^BuildNumber=.*/BuildNumber=$BUILD/" abc.properties
This assumes that there are no spaces between BuildNumber and the = sign, otherwise you can use this one :
sed "s/^\(BuildNumber\s*=\s*\).*$/\1$BUILD/" abc.properties
Here is another way using a batch/vbs hybrid script.
#echo off
setlocal
call :FindReplace "Buildnumber=0" "Buildnumber=1.21" abc.properties
exit /b
:FindReplace <findstr> <replstr> <file>
set tmp="%temp%\tmp.txt"
If not exist %temp%\_.vbs call :MakeReplace
for /f "tokens=*" %%a in ('dir "%3" /s /b /a-d /on') do (
for /f "usebackq" %%b in (`Findstr /mic:"%~1" "%%a"`) do (
echo(&Echo Replacing "%~1" with "%~2" in file %%~nxa
<%%a cscript //nologo %temp%\_.vbs "%~1" "%~2">%tmp%
if exist %tmp% move /Y %tmp% "%%~dpnxa">nul
)
)
del %temp%\_.vbs
exit /b
:MakeReplace
>%temp%\_.vbs echo with Wscript
>>%temp%\_.vbs echo set args=.arguments
>>%temp%\_.vbs echo .StdOut.Write _
>>%temp%\_.vbs echo Replace(.StdIn.ReadAll,args(0),args(1),1,-1,1)
>>%temp%\_.vbs echo end with

Resources