How do I pull the middle of a string variable in batch? - string

I need to get a certain part of a string as one variable, and the rest as another variable. Ignor the "Script.CreateSub", that doesn't matter.
#Echo Off
Set String=Script.CreateSub(Start)
I need a variable named ComName to equal the first part of the input (Script.CreateSub) and a variable named Name that equals whatever is in between the parenthesis.

This is much the same answer as above but doesn't use delayed expansion, as it is not needed, and it will handle many poison characters.
#echo off
Set "String=Script.CreateSub(Start)"
for /f "tokens=1,2 delims=()" %%a in ("%String%") do set "ComName=%%a" & set "Name=%%b"
echo "%ComName%" , "%Name%"

This should do it:
#echo off
setlocal enabledelayedexpansion
Set String=Script.CreateSub(Start)
for /f "tokens=1,2 delims=()" %%a in ("%String%") do set function=%%a&set param=%%b
echo function=%function%
echo param=%param%
pause
See http://www.dostips.com/DtTipsStringManipulation.php#Snippets.SplitString for more info on this technique.

Related

Cannot set environmental variable inside a loop in a batch script

I have a batch script that works great and it does the following:
download EDI files from sftp server
generate a dynamic ftp scripts based on the filenames that were downloaded
uploads the file to an internal ftp server via FTP script
I need to modify the batch file to look at the content as some data coming in maybe missing a 0. I do have the logic on how to do this, however, I cannot add the logic to the loop I have as variables do not work.
Below is loop where I want to add the part to correct missing leading zero (that would be in position 4):
for /F "tokens=*" %%A in (c:\scripts\permitsin.txt) do (
echo put %%A /qsys.lib/MAEPFTDTA.lib/PERMITIN.file/%%A >> c:\scripts\ftp-to-scoopsoft.ftp1
)
What I need to do is add an environment variable which will be the content of the file and below is the sample code:
set /p string =< %%A
echo Value for string is set to %string%
echo %string:~9,1% | find "ECHO is on." > nul
if not errorlevel 1 goto Found0
echo no space found
goto End
:Found0
echo found space
echo below is what the correct number needs to be
echo %string:~0,3%0%string:~3% > %%A
:End
I need this to work in batch file as security requirements does not allow PowerShell on the servers.
for /F "tokens=*" %%A in (c:\scripts\permitsin.txt) do (
for /F "delims=" %%S in (%%A) do (
setlocal enabledelayedexpansion
set "string=%%S"
if "!string:~9,1!"==" " echo !string:~0,3!0!string:~3!> %%A
endlocal
)
echo put %%A /qsys.lib/MAEPFTDTA.lib/PERMITIN.file/%%A >> c:\scripts\ftp-to-scoopsoft.ftp1
)
should do what you appear to need. Test on sample data first, of course.
Here's the how : Stephan's DELAYEDEXPANSION link
And it saves a lot of work if you tell us what you want to do rather than how you want to do it.

Replace %%20 in string using windows batch script

I have a use case where I want to replace %%20 which is part of a string for example: "Calculation%%20One". I want to replace this with "Calculation One".
This where I am stuck:
#echo off
setlocal enableextensions disabledelayedexpansion
>"temp" (
echo !Option1!
echo !Option2!
echo !Option3!
)
set "pattern=%%20"
for /f "usebackq delims=" %%a in ("temp") do (
echo data: %%a
set "line=%%a"
setlocal enabledelayedexpansion
if "!line:%pattern%=!"=="!line!" (
set string=!Option1!
set string2=%!string1!:%%20= %
) else (
set string2=%%a
)
endlocal
)
del /q tempFile
Can someone please help me with this? I have a program which is a combination of batch and python.
Thanks
It is unclear for me why the options are written into a temporary file for processing the values of the environment variables Option1, Option2 and Option3. And it would have been good to see the lines which define the environment variables Option1, Option2 and Option3 for the real values assigned to them.
So I suggest here a batch file which replaces %20 as found for example in HTML files or emails by a space character in all Option environment variables.
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "Option1=Calculation%%20One"
set "Option2=Calculation %%%%20Two!"
set "Option3=Any other string"
cls
echo The options before processing:
echo/
set Option
for /F "delims==" %%I in ('set Option 2^>nul') do call :ProcessOption "%%I"
echo/
echo The options after processing:
echo/
set Option
echo/
endlocal
pause
goto :EOF
:ProcessOption
setlocal EnableDelayedExpansion
set "ModifiedOption=!%~1:%%20= !"
endlocal & set "%~1=%ModifiedOption%"
goto :EOF
It is necessary to use delayed expansion on replacing all occurrences of %20 by a space character because otherwise Windows command interpreter would not know where the environment variable reference with string substitution ends. Replacing a string with percent sign is not possible using immediate expansion for that reason.
The command set Option outputs alphabetically sorted all environment variables starting case-insensitive with the string Option in format variable=value as it can be seen twice on running this batch file.
FOR executes this command with using a separate command process started in background and captures all lines written by command SET to handle STDOUT.
The string 2^>nul is executed finally as 2>nul and redirects the error message output by command SET to handle STDERR to the device NUL to suppress it. SET would output an error message in case of no environment variable starting with Option is defined in current environment. This error condition does not occur with this batch file. Read the Microsoft article about Using Command Redirection Operators. The redirection operator > must be escaped here with ^ to be interpreted first as literal character when Windows command interpreter processes the entire FOR command line before executing the command FOR which executes cmd.exe /c set Option 2>nul in background.
FOR processes the captured output of set Option line by line and splits each line up into substrings (tokens) with using the equal sign as delimiter as specified with delims==. The string left to first equal sign on each line is assigned to loop variable I. This is the name of the environment variable, i.e. Option1, Option2 and Option3 for this batch file.
With each OptionX environment variable name the subroutine ProcessOption is called for replacing all %20 in its value by a space character.
Here is a solution for environment variables with one or more exclamation marks in variable name.
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "!Option1!=Calculation%%%%20One"
set "!Option2!=City%%20Country"
set "!Option3!=State%%%%20Country"
set "!Option4!=Any other address/string"
cls
echo The options before processing:
echo/
set !Option
for /F "delims==" %%I in ('set !Option 2^>nul') do call :ProcessOption "%%I"
echo/
echo The options after processing:
echo/
set !Option
echo/
endlocal
pause
goto :EOF
:ProcessOption
call set "ModifiedOption=%%%~1%%"
setlocal EnableDelayedExpansion
set "ModifiedOption=!ModifiedOption:%%%%20= !"
set "ModifiedOption=!ModifiedOption:%%20= !"
endlocal & set "ModifiedOption=%ModifiedOption%"
set "%~1=%ModifiedOption%"
set "ModifiedOption="
goto :EOF
This batch code replaces %20 AND %%20 in all environment variables starting with !Option in name by a space character in comparison to above replacing just %20 by a space in environment variables beginning with Option in name.
It is of course a bad idea to use characters in variable names which have a special meaning for Windows command interpreter like the characters &()[]{}^=;!'+,`~|<>%. It is possible as demonstrated above, but it is definitely not a good idea.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
cls /?
echo /?
endlocal /?
for /?
goto /?
set /?
setlocal /?
See also answer on Single line with multiple commands using Windows batch file for an explanation of & operator.
And read DosTips forum topic ECHO. FAILS to give text or blank line - Instead use ECHO/ why it is better to use echo/ instead of echo. to output an empty line.

Batch Manipulating String by Input Variable Using "=&rem."

Using Server 2016 here with simple batch-file programming (for now).
I have what I think is an easy problem, just that I am not seeing a workable solution in front of me right now. I'm going to limit my request right down to a testing scenario to keep it simple. What I am trying to do is trim a string from the right until it hits the current year in the format "_%year%-" where the year does come from a variable elsewhere, but is set static in my example. Where I am running into issues is referring to a variable from within the code I have.
This is a working example NOT using a variable on the rem line, and gives the desired output of "Machined_Cam-2286:"
#ECHO OFF
SETLOCAL DisableDelayedExpansion
set "testString=Machined_Cam-2286_2017-09-08.slddrw - SOLIDWORKS"
set "systemYear=2017"
set "yearModified=_%systemYear%-"
echo "%testString%" | find "%yearModified%" >NUL || goto :EOF
set testString=%testString:_2017-=&rem.%
echo %testString%
pause
You can see that "_2017-" hard-coded in on the 10th line. What I am looking to do in a purely logical sense on the rem line specifically:
set testString=%testString:%yearModified%=&rem.%
Because of the way this command modifies testString in-line, it makes it difficult to inject a variable into it. I have tried a huge combination of escapes and expansion settings to get the variable to take with no success so far. I have also tried to "build" the command as a string and attempt to call it and pipe the output to a variable:
#ECHO OFF
SETLOCAL DisableDelayedExpansion
set "testString=Machined_Cam-2286_2017-09-08.slddrw - SOLIDWORKS"
set "systemYear=2017"
set "yearModified=_%systemYear%-"
echo "%testString%" | find "%yearModified%" >NUL || goto :EOF
set "callCMD=%%testString:%yearModified%=^&rem.%%"
call %callCMD% > %testString%
echo %testString%
pause
This seems like such a simple issue, but my lack of understanding of string manipulation under DOS is apparent. As is stands, the rest of the script is running perfectly with the "_2017-" hard-coded. It would be nice to eliminite that bit of maintenance.
Any help or direction is appreciated.
Thank you.
May I show you another method of splitting. Replace your "Delimiterstring" with a proper Delimiter and use a for to split (for uses single-letter delimiters). Enabling delayed expansion helps, but is not neccessary. As you explicitely disabled it (you may have your reasons), I'll show you both:
#echo off
set "TestString=Machined_Cam-2286_2017-09-08.slddrw - SOLIDWORKS"
set "CutHere=_2017"
setlocal enabledelayedexpansion
for /f "delims=§" %%a in ("!TestString:%CutHere%=§!") do set Result=%%~a
echo enabled: %Result%
endlocal
setlocal disabledelayedexpansion
for /f "delims=§" %%a in ('call echo "%%TestString:%CutHere%=§%%"') do set Result=%%~a
echo disabled: %Result%
endlocal
Just be sure, you use a delimiter (§ here) that surely won't be in your string(s).
you are trying to control what you call using the contents of a variable
try using a temp file instead -
echo %%testString:%yearModified%=^&rem.%%" > temp.bat
call temp.bat > %testString%
You NEED delayed expansion for this type of operation, so enabe it.
I suggest to first get the remainder of the string
and remove this from the original string.
#ECHO OFF
SETLOCAL EnableDelayedExpansion
set "testString=Machined_Cam-2286_2017-09-08.slddrw - SOLIDWORKS"
set "systemYear=2017"
set "yearModified=_%systemYear%-"
echo "%testString%" | find "%yearModified%" >NUL || goto :EOF
set tempString=!testString:*%yearModified%=!
set testString=!testString:%yearModified%%tempstring%=!
echo %testString%
pause
Sample output:
Machined_Cam-2286
#echo off
setlocal EnableDelayedExpansion
set "testString=Machined_Cam-2286_2017-09-08.slddrw - SOLIDWORKS"
set "systemYear=2017"
set "yearModified=_%systemYear%-"
echo "%testString%" | find "%yearModified%" >NUL || goto :EOF
set testString=!testString:%yearModified%=^&rem.!
echo %testString%
pause
You shoud use a carrot(^) in Delayed Expansion mode.

How to find and replace specifc string in txt using .bat?

I have a xml:
<?xml version="1.0"?>
<arquivoposicao_4_01 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<fundo xmlns="http://tempuri.org/">
<header>
And i just want to replace de string "xmlns=" for anything, like: "dog=".
I am using this .bat:
#echo off
REM -- Prepare the Command Processor --
SETLOCAL ENABLEEXTENSIONS
SETLOCAL DISABLEDELAYEDEXPANSION
if "%~1"=="" findstr "^::" "%~f0"&GOTO:EOF
for /f "tokens=1,* delims=]" %%A in ('"type %3|find /n /v """') do (
set "line=%%B"
if defined line (
call set "line=echo.%%line:%~1=%~2%%"
for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X
) ELSE echo.
)
and i use: file.bat "xmlns=" "dog=" nome.txt
but it replace "xlmns=" for "=dog=".
PS: i just want replace the third line, the second not.
#ECHO Off
REM -- Prepare the Command Processor --
SETLOCAL ENABLEEXTENSIONS
SETLOCAL DISABLEDELAYEDEXPANSION
REM if "%~1"=="" findstr "^::" "%~f0"&GOTO:EOF
:: Replacing parameters with fixed strings for testing
SET "p1=xmlns"
SET "p2=dog"
SET "p3=q24511441.txt"
for /f "tokens=1,* delims=]" %%A in ('"type %p3%|find /n /v """') do (
set "line=%%B"
if defined line (
ECHO("%%B"|FIND "%p1%="
IF ERRORLEVEL 1 (ECHO(%%B) ELSE (
call set "line=%%line:%p1%=%p2%%%"
for /f "tokens=1*delims==" %%X in ('set line') do ECHO(%%Y
)
) ELSE ECHO(
)
GOTO :EOF
Here's a way to do something similar to what you appear to want.
The problem appears to be that the replace-text syntax uses = to separate the target string from the replacement.
Your routine seems to be aimed at being a universal replace-one-string-with-another routine. Batch is not relly suited to this task, but it can be done within limits.
I've replaced your parameters %1..%3 with fixed text for testing. The routine would be used to replace %1= with %2= and is not perfect. It will leave xmlns alone on lines that do not contain xmlns= and replace any xmlns on a line which contains xmlns= with dog.
I used a file named q24511441.txt containing your data for my testing.
Replacing a string like this containing an "=" is very problamatic in windows batch as theres no way of escaping or quoting it (that i know of).
See here for some ideas: Escaping an equals sign in DOS batch string replacement command
Also parsing/modifying XML in batch scripts using search/replace and regex for parsing is generally a bad idea, loads of possible problems and issues. Best to write a program in a language that provides XML parsing libraries (e.g java, perl etc).

Reading multiple text files and replacing a certain type of string in each of them using a batch file

I need to read all the text files from a directory and then replace a certain type of string (that comes in a line having 'volumelabel') with another (replacewith) in each of them. Following is the code snippet:
for /r %%g in (*.txt) do (
set filename=%%~nxg
for /F "tokens=3 delims=<>" %%i in ('findstr "volumelabel" !filename!') do (
set tobereplaced=%%i
)
echo !filename! has !tobereplaced! to be replaced by %replacewith%
for /F "tokens=*" %%a in (!filename!) do (
set str=%%a
set str=!str:!tobereplaced!=%replacewith%!
echo !str!>>new!filename!
)
)
Now the problem I am facing is it prints only tobereplaced (literally) in every line of the new files when
set str=!str:!tobereplaced!=%replacewith%!
echo !str!>>new!filename!
is used and prints tobereplaced=replacewith (values) when
set str=%str:!tobereplaced!=%replacewith%%
echo !str!>>new!filename!
is used. Can someone help me?
The least convoluted solution (in my humble opinion) is to use a subroutine to set str. Going more than one level deep of delayed expansion tends to cause severe brain hurt. Oh, you could probably fix your set str line by doing something like
call call call set str=%%%%str:%%tobereplaced%%=%replacewith%%%%%
...or similar. See what I mean about brain hurt? It's hard to follow the recursion.
So here's my suggestion for a solution. I also fixed another potential problem or two while I was at it. Since you're doing a recursive search for *.txt, I made the for loops able to work with whatever text files they find within subdirectories. I haven't tested this, so let me know if you get any grotesque errors.
#echo off
setlocal enabledelayedexpansion
set replacewith=whatever
for /r %%g in (*.txt) do (
set newfile=%%~dpng.new%%~xg
for /F "tokens=3 delims=<>" %%i in ('findstr "volumelabel" "%%g"') do (
set "tobereplaced=%%i"
echo %%~nxg has !tobereplaced! to be replaced by %replacewith%
rem combining your for loops this way makes the second only fire if the first is true
rem using "findstr /n" in your for loop preserves blank lines
for /F "delims=" %%a in ('findstr /n "^" "%%g"') do (
rem ...but you have to strip off the line numbers
set "str=%%a" && set "str=!str:*:=!"
rem "call :repl" to work around the delayed expansion conundrum
call :repl "!str!" "!tobereplaced!" "%replacewith%" str
echo !str!>>!newfile!
)
)
)
goto :EOF
:repl <line> <find> <replace> <var>
setlocal enabledelayedexpansion
set "line=%~1"
set "line=!line:%~2=%~3!"
set "%4=%line%"
goto :EOF
Caveat: If your text file contains exclamation marks, equal signs or carats, they might not make it into textfile.new.txt.
For what it's worth, if I were in your position, instead of using a batch file I would probably use sed (the binaries should be all you need). You wouldn't even need a script. You could do it as a one liner like this:
for /r %I in (*.txt) do sed -r "s/volumelabel/replacement/ig" "%I" > "%~dpnI.new%~xI"
By the way, see the last couple of pages of help for for an explanation of the %~dpnI sort of notation.

Resources