Code: Select all
Test.bat 99 "Par 2" "Par 3"
Code: Select all
<Result><99><Two:Par 2><Three:Par 3>|
After this, the string should be written to a text file.
(Potentially there could be more than two parameters of the type "Par 2", "Par 3"...)
Moderator: DosItHelp
Code: Select all
Test.bat 99 "Par 2" "Par 3"
Code: Select all
<Result><99><Two:Par 2><Three:Par 3>|
Code: Select all
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
set str="<Result><%1><Two:%~2><Three:%~3>|"
echo %str%
echo %str% > Result.txt
REM cmd /u /c "echo %str% > Result.txt"
Code: Select all
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
set "str=<Result><%1><Two:%~2><Three:%~3>|"
echo !str!
>"Result.txt" echo !str!
Code: Select all
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
set "str=<Result><%1><Two:%~2><Three:%~3>|"
echo !str!
REM Works fine: >"Result.txt" echo !str!
REM Output to a Unicode file:
cmd /u /c "echo "!str!">ResultU.txt"
Code: Select all
:: make sure !str! ain't expanded in the parent cmd.exe instance
setlocal DisableDelayedExpansion
:: /v:on enables the delayed expansion in the new cmd.exe instance, also the redirection doesn't belong to the new cmd.exe instance
>"ResultU.txt" cmd /v:on /u /c "echo !str!"
Code: Select all
@echo off
setlocal DisableDelayedExpansion
if "%~1"=="" exit /b 1
set "str=<Result><%~1>"
setlocal EnableDelayedExpansion
set /a "num=2, cnt=2"
for %%i in (
"Two" "Three" "Four" "Five"
"Six" "Seven" "Eight" "Nine" "Ten"
"Eleven" "Twelve" "Thirteen" "Fourteen" "Fifteen"
"Sixteen" "Seventeen" "Eighteen" "Nineteen" "Twenty"
) do (
set "n_!num!=%%~i"
set /a "num+=1"
)
setlocal DisableDelayedExpansion
:loop
shift
if "%~1" neq "" if %cnt% lss %num% (
setlocal EnableDelayedExpansion
for /f "delims=" %%i in ("!str!") do for /f "delims=" %%j in ("!n_%cnt%!") do (
endlocal
set "str=%%i<%%j:%~1>"
)
set /a "cnt+=1"
goto loop
)
set "str=%str%|"
>"ResultU.txt" cmd /v:on /u /c "echo !str!"
exit /b 0
Code: Select all
@echo off
setlocal DisableDelayedExpansion
if "%~1"=="" exit /b 1
set "str=<Result><%~1>"
setlocal EnableDelayedExpansion
set /a "num=2, cnt=2"
for %%i in (
"Two" "Three" "Four" "Five"
"Six" "Seven" "Eight" "Nine" "Ten"
"Eleven" "Twelve" "Thirteen" "Fourteen" "Fifteen"
"Sixteen" "Seventeen" "Eighteen" "Nineteen" "Twenty"
) do (
set "n_!num!=%%~i"
set /a "num+=1"
)
setlocal DisableDelayedExpansion
:loop
shift
if "%~1" neq "" if %cnt% lss %num% (
setlocal EnableDelayedExpansion
for /f "delims=" %%i in ("!str!") do for /f "delims=" %%j in ("!n_%cnt%!") do (
endlocal
set "str=%%i<%%j:%~1>"
)
set /a "cnt+=1"
goto loop
)
set "str=%str%|"
:: save current OEM code page
for /f "tokens=*" %%a in ('chcp') do for %%b in (%%a) do set "oemcp=%%~nb"
:: change to UTF-7
>nul chcp 65000
:: characters ÿþ UTF-7-encoded
set "bom16_7_1252=+AP8A/g-"
:: change to Windows-1252 where ÿ is represented by byte 0xFF and þ by 0xFE which is the byte sequence of the UTF-16 LE BOM
>nul chcp 1252
:: write the BOM (the content of variable bom16_7_1252 was automatically converted to the current code page)
>"ResultU.txt" <nul set /p "=%bom16_7_1252%"
:: turn back to the default OEM code page
>nul chcp %oemcp%
>>"ResultU.txt" cmd /v:on /u /c "<nul set /p "=!str!""
exit /b 0