Solved Reverse a string in DOS

July 18, 2011 at 00:30:01
Specs: Windows 7
The following line of code extracst 4 characters from the begininig of a string:

set str=%str:~0,4%

I would like to substitute a varable for the number of characters to extract. In this case substitute a variable for the number 4.

Pseudo code:
set str=%str-0,<variable>

This would help me parse through a string of variable length and content for a project I am working on.

Any help with this would be greatly appreciated!!


See More: Reverse a string in DOS

Report •


#1
July 18, 2011 at 04:28:43
@echo off
cls
setlocal enabledelayedexpansion

set str=123456

set var=!str:~0,%1!

echo %var% 


Please come back & tell us if your problem is resolved.


Report •

#2
July 18, 2011 at 11:20:26
✔ Best Answer
Hi tzagone

::Note: ReverseStr also calls StrLen
::and string length is not greater than 80 chars
:: but can be changed.

@echo off
SetLocal EnableDelayedExpansion
cls
set Str=9876543210
call :StrLen %Str%
echo Length=%Len%
call :ReverseStr %Str%
echo String=%Str%
echo Reverse Str=%Reverse%
exit /b

::----------------
:: Calc Var Length
::----------------
:: %*=Str to Check
:: Returns %Len%
:: ---------------
:StrLen %*
set Data=%*
for /L %%a in (0,1,80) do (
set Char=!Data:~%%a,1!
if not "!Char!"=="" (
set /a Len=%%a+1
) else (exit /b)
)
exit /b


::---------------
:: Reverse String
::---------------
:: %* Str to Reverse
:: Returns %Reverse%
::------------------
:ReverseStr %*
set Data=%*
call :StrLen %Data%
for /L %%a in (!Len!,-1,0) do (
set Char=!Data:~%%a,1!
set Reverse=!Reverse!!Char!
)
exit /b


Report •

#3
July 18, 2011 at 11:43:08
Thanks for the reply. This works for the reverse and I was able to use the for loop to do some additional parsing.

Thanks again!


Report •

Related Solutions


Ask Question