Batch script closing parenthesis issue - batch-file

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.

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 not outputting files with this code

This script should output each word in a 10-word sentence stored in line 1 (When called with SeparateLine.bat (filename) 1, but it shows no errors nor outputs or makes the file.
I have already tried removing the parenthesis in the (echo %%a > 1.txt) and the ones below, with no avail. I've also tried different formats for where the outputs go, still no avail.
#echo off
if not exist "%~1" echo file not found & exit /b 1
if "%~2"=="" echo line not defined & exit /b
if "%~2"=="1" set line=1 & goto start
set /a line=%~2-1
:start
For /f "tokens=1,2,3,4,5,6,7,8,9,10* usebackq skip=%line% delims= " %%A in ("%~1") do (
if not "%%A"=="" (echo %%A > 1.txt)
if not "%%B"=="" (echo %%B > 2.txt)
if not "%%C"=="" (echo %%C > 3.txt)
if not "%%D"=="" (echo %%D > 4.txt)
if not "%%E"=="" (echo %%E > 5.txt)
if not "%%F"=="" (echo %%F > 6.txt)
if not "%%G"=="" (echo %%G > 7.txt)
if not "%%H"=="" (echo %%H > 8.txt)
if not "%%I"=="" (echo %%I > 9.txt)
if not "%%J"=="" (echo %%J > 10.txt)
GOTO endforloop
)
:endforloop
With the file that contains one, two, three... ten separated by spaces, it should output 10 files (maximum word limit is 10) each containing word 1-10 of the line (I specified line 1 in the second parameter as the words are on the first line), but the actual results are... nothing. Literally, it acts as if it completed successfully but no files are made and, well, no error codes.
I think this will be what you are looking for. Note you cannot use the pre-defined variable in skip=%line% you need to instead use more +%line%, there is not a need to define each token token=1,2,3,4... etc. simply use tokens1-10 and and last, but not least no need to use delims= as whitespace is the default delimeter:
#echo off
if not exist "%~1" echo file not found & exit /b 1
if "%~2"=="" echo line not defined & exit /b
if "%~2"=="1" set line=1 & goto start
set /a line=%~2-1
:start
For /f "tokens=1-10" %%A in ('type "%~1" ^| more +%line%') do (
if "%%A" NEQ "" echo %%A
if not "%%B"=="" echo %%B
if not "%%C"=="" echo %%C
if not "%%D"=="" echo %%D
if not "%%E"=="" echo %%E
if not "%%F"=="" echo %%F
if not "%%G"=="" echo %%G
if not "%%H"=="" echo %%H
if not "%%I"=="" echo %%I
if not "%%J"=="" echo %%J
goto :EOF
)
There is a flaw however with your line variable.
if a user selects 0 it will do 0 - = -1and then set the lines to skip-1if a user selects2it will set it to skip1` which is not what a user requested, so I suggest you clarify what your intensions are with the skip lines, but perhaps just get rid of it completely, like:
#echo off
if not exist "%~1" echo file not found & exit /b 1
if "%~2"=="" echo line not defined & exit /b
set /a line=%~2
:start
For /f "tokens=1-10" %%A in ('type "%~1" ^| more +%line%') do (
if "%%A" NEQ "" echo %%A
if not "%%B"=="" echo %%B
if not "%%C"=="" echo %%C
if not "%%D"=="" echo %%D
if not "%%E"=="" echo %%E
if not "%%F"=="" echo %%F
if not "%%G"=="" echo %%G
if not "%%H"=="" echo %%H
if not "%%I"=="" echo %%I
if not "%%J"=="" echo %%J
goto :EOF
)

Rename files using cmd and allow duplicates file name

I have my batch file which consists of the following command.
REN "H:\April2012\A04\mr_sudheendra_holla_vaderhobli.pdf.1335780379203.ver1" "mr_sudheendra_holla_vaderhobli.pdf"
But if duplicate files exist, the command will not execute. I would like my command to rename the file name to *(1).pdf and *(2).pdf etc. if there are duplicates. How can I do that?
renDup.bat
#echo off
setlocal disableDelayedExpansion
ren %1 %2 2>nul && echo %1 --^> "%~n2(%max%)%~x2"|| call :renDup %1 %2
exit /b
:renDup
set max=0
for /f "delims=" %%F in (
'dir /b "%~dp1%~n2(*)%~x2" 2^>nul ^| findstr /ri "([1-9][0-9]*)\%~x2$"'
) do call :getMax "%%~nF"
set /a max+=1
ren %1 "%~n2(%max%)%~x2" && echo %1 --^> "%~n2(%max%)%~x2"
exit /b
:getMax
set "name=%~1"
set "name=%name:~0,-1%"
for %%N in ("%name:(=.%") do set num=%%~xN
set /a num=%num:~1%
if %num% gtr %max% set "max=%num%"
exit /b
usage:
renDup "H:\April2012\A04\mr_sudheendra_holla_vaderhobli.pdf.1335780379203.ver1" "mr_sudheendra_holla_vaderhobli.pdf"

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