Windows Batch:string replacement - batch-file

The following batch file accepts a parameter which is a path and filename.
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET filename=%~1
echo !filename!
ENDLOCAL
The filename, when received as a parameter will always be formatted using forward slashes.
In order to replace the forwardshlashes with backslashes, I tried this:
SET filename=!filename:/=\!
But that's not working.
What is the simplest way to do string replacement in a windows batch file?
Thanks

First of all you need to remove the space after =:
SET filename=%~1
Otherwise the space will become part of your variable.
To replace / with \ you have to use % instead of !:
SET filename=!filename:/=\!
Further, there is nothing in your code that would require ENABLEEXTENSIONS so you can skip it.
EDIT:
This is my code of something.bat:
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET file=%~1
SET file=!file:\=/!
ECHO !file!
Calling the something.bat ABC/DEF/GHI results in the output ABC\DEF\GHI.

You have a problem when you set the variable
v...v. Initial and ending spaces included in value
SET filename = %~1
^........ Space included in variable name
As the variable is not %filename%, but %filename %, your replacement fails. For a string replacement approach you can use
#echo off
setlocal enableextensions disabledelayedexpansion
set "filename=%~1"
set "filename=%filename:/=\%"
echo %filename%
or, still better, this case can be solved using argument modifiers
#echo off
setlocal enableextensions disabledelayedexpansion
set "filename=%~f1"
echo %filename%

Related

Batch file - can not read a variable

Making batch which generate previews (everything is fine with this part of code) and also rename files deleting everything after "_" in filename. For example ABAB_abab.png > ABAB.png
My code does not see a variable yy in the string: set zz=!xx:yy=! Perceives it like just two letters yy, not a variable. How to fix that?
Here is the script
setlocal enabledelayedexpansion
for %%a in ("*.png") do (
set xx=%%~na
set yy=_!xx:*_=!
set zz=!xx:yy=!
echo xx= !xx! #rem (okay, returns ABAB_abab)
echo yy= !yy! #rem (okay, returns _abab)
echo zz= !zz! #rem (wrong, returns ABAB_abab without any substitutions)
pause
)
endlocal
Thank you for help
Here's a quick example to show you a method of achieving another layer of expansion:
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
For %%G In ("*.png") Do (
Set "xx=%%~nG"
SetLocal EnableDelayedExpansion
Set "yy=_!xx:*_=!"
For %%H In ("!yy!") Do Set "zz=!xx:%%~H=!"
Echo xx = "!xx!"
Echo yy = "!yy!"
Echo zz = "!zz!"
EndLocal
Pause
)
The doublequotes are included in the Echo commands only for better visualization should there be any spaces in your strings, they're not needed for any other purpose.
Please note, that this will not achieve your intention with any .png files whose basename begins with one or more underscores, _.

Batch file: dealing with a parameter that can contain spaces and ampersand

I've got a simple batch file that places the parameter it's called with into a text file:
setlocal ENABLEDELAYEDEXPANSION
set filename=%~n1
set pathname=%~p1
set letter=%~d1
>>path.txt echo %letter%%pathname%%filename%
(it does more, but this is sufficient to show the problem)
The parameter is a full path:
C:\te st\file & name.xml
This batch file works as long as there's no & in the path name. But the above path results in filename=file
and the & is interpreted as an argument.
I tried using set "filename=%~n1"
but that results in
>>path.txt echo C:\te st\"file & name.xml"
which is incorrect. I can't get rid of the quotes.
I tried:
>>path.txt echo %letter%%pathname%!filename!
but that results in
>>path.txt echo C:\te st\!filename!
How do I get the correct path in my text file?
setlocal DisableDelayedExpansion
set "filename=%~n1"
set "pathname=%~p1"
set "letter=%~d1"
setlocal EnableDelayedExpansion
>>path.txt echo !letter!!pathname!!filename!
After assigning the content into variables, only delayed expansion should be used, because delayed expansion never changes or try to parse the content.
The setlocal DisableDelayedExpansion at the beginning ensures, that exclamation marks are preserved while assigning the arguments to variables.

Use Bat file to split a string with semi-colon as delimiter

I want to delete a special path using batch.
I cannot use set somevar=" %path:specialstr=%",because the specialstr has dynamic part. Can I use batch to remove strings which are produced by %cd% from a large string like %path%?
Follow Dennis van Gils' advice, use Delayed Expansion as follows:
SETLOCAL EnableExtensions EnableDelayedExpansion
set "specialstr=%CD%\"
set "somevar=!path:;%specialstr%;=;!"
If you can't use Delayed Expansion for some reason, use call set (read Advanced usage : CALLing internal commands):
warning: some partial paths in %path% variable contain facultative trailing backslash whereas other do not.
call set "somevar=%%path:;%specialstr%;=;%%" most common usage.
call set "somevar=%%path:;%specialstr%=%%" use if ;%specialstr% is trailing part of %path% but be aware that Variable Edit/Replace is greedy and replaces all occurences of ;%specialstr% in %path%.
call set "somevar=%%path:%specialstr%;=%%" use if %specialstr%; is leading part of %path% but be careful for the same caution.
A sample script:
#ECHO OFF
SETLOCAL EnableExtensions DisableDelayedExpansion
where p.bat
pushd "C:\Utils"
set "specialstr=%CD%\"
popd
echo specialstr=%specialstr%
echo path end=...%path:~-46%
call set "somevar=%%path:;%specialstr%=%%"
echo somevar end=...%somevar:~-36%
SETLOCAL
set "path=%somevar%"
echo path end=...%path:~-36%
where p.bat
ENDLOCAL
where p.bat
Output (shows only some few trailing characters of %path% and %somevar% variables for better readability):
==> D:\bat\SO\38017804.bat
C:\Utils\p.bat
specialstr=C:\Utils\
path end=...Microsoft SQL Server\120\Tools\Binn\;C:\Utils\
somevar end=...Microsoft SQL Server\120\Tools\Binn\
path end=...Microsoft SQL Server\120\Tools\Binn\
INFO: Could not find files for the given pattern(s).
C:\Utils\p.bat

How to add unknown string to file?

For example my string is : Hello [ ^ "World"
I want to add it to file, is it possible? (sure echo will not work)
Without more information it is not clear where the problem is. This "should" work.
#echo off
setlocal enableextensions disabledelayedexpansion
set "input="
set /p "input=string?"
if not defined input exit /b
setlocal enabledelayedexpansion
>>"outputFile.txt" echo(!input!
endlocal
The data is captured with delayed expansion disabled to avoid parser problems in the value assignment (in this case should not happen). Then, to avoid parser interferences in the output, delayedexpansion is activated to echo the data.

Replace string with a new line in Batch

set NLM=^
set NL=^^^%NLM%%NLM%^%NLM%%NLM%
SET memoli=%token:QMZ=%NL%%%
echo %memoli%>>%tmp%\list2.txt
I cant change the string "QMZ" with a new line. How to do that?
Very simple
setlocal EnableDelayedExpansion
set "token=HelloQMZworld"
echo !token:QMZ=^
!
It works as the batch parser parses first the multiline caret and replace it with a single linefeed.
Then in the delayed expansion phase it replaces the QMZ with a single linefeed, which is legal in that phase.
To set a new variable with the replaced string simply use
setlocal EnableDelayedExpansion
set "token=HelloQMZworld"
set newVal=!token:QMZ=^
!
echo !newVal!
set LF=^
rem ** Two empty lines required
FOR /F "delims=" %%a in ("%token:QMZ=!LF!%") do (
echo %%a>>%tmp%\list2.txt
)
I was just wandering in the codes and I just did this unconsciously. But it does the trick.

Resources