Solved

How to truncate a string from one range to another range in DOS batch

Posted on 2006-07-23
6
1,994 Views
Last Modified: 2008-01-09
Hi there,
I am trying a simple thing, i wish to truncate a string, for example:
Set name=123456789

i want to truncate the above string to:
Set name=12345678, just remove the last character? I have no idea how to start, because it is limited to Dos batch for me in the execte environment, anyone can help?
Thank you  very much
0
Comment
Question by:AlexKhaw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
6 Comments
 
LVL 8

Expert Comment

by:KelvinY
ID: 17185340
Hi Alex

I'm not sure from you statement exactly how restricted you are within the DOS environment. If you don't mind I'd like to put a few questions to clarify your situation.

1) Which version of DOS are you using?
2) Where does your input "Set name=123456789" come from? Is this text typed at the command prompt, or is text within a file?
3) Are you restricted to only using DOS commands to handle your string, or can you create something like a VBS file? If you're restricted to DOS only I don't think it can be done. If you can create VBS files the problem is simple.

Regards
  Kelvin
0
 
LVL 22

Accepted Solution

by:
JesterToo earned 500 total points
ID: 17186547
See if this short code fragment is what you need.  If not, please ask more questions (and provide more information about your question).

Regards,
Lynn


::=======================================================
::==                                                   ==
::==  TruncVar.bat                                     ==
::==                                                   ==
::==    This script reads tan environment variable     ==
::==    named "name" and creates a new environment     ==
::==    variable named "newname" with a truncated copy ==
::==    of the data from the first env. var.  The      ==
::==    value is shortened by 1 character which is     ==
::==    removed from the right end of the string.      ==
::==                                                   ==
::=======================================================

@echo off

:main
   set name=123456789
   echo %name%

   set newname=%name:~0,-1%
   echo %newname%

0
 
LVL 24

Expert Comment

by:SunBow
ID: 17266860
>  I have no idea how to start, because it is limited to Dos

Then you should ask Q in DOS TA, which is not a programming language even in the general sense.
In programming you can deal with string lengths or use left or right side, etc. Each language may be different in how that is done, but as a standalone, DOS is an OS, not a language

If you have more than dos, such as XP, then alternatives like vbs may be available if enabled
0
 
LVL 22

Expert Comment

by:JesterToo
ID: 17268360
Based on other questions Alex has asked (regarding XP and Perl), it appears that he used the term "DOS" to actually mean "command shell".  And, while I'll certainly agree that the command shell scripting capabilities of Windows don't constitute a very robust "language" there are some people that can do amazing things with it... it's just not very intuitive!

The script I provided above accomplishes what was asked.  The next-to-last line actually does all the work... the other lines just setup the problem and display the before/after values to demonstrate that it works.

As for MSDOS being an OS... I think that's being pretty charitable!  I'd consider it more of a "control program" than an Operating System.  It was hardly more advanced than CP/M which preceeded it.

Regards,
Lynn
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question