Solved

Strip underscores from variable

Posted on 2010-09-22
23
784 Views
Last Modified: 2012-06-21
Hi,

I've been working on updating one of our batch files to read/build menu's based on values held within an INI file... I've got the menu-build working a treat now but would like to tweak it a little and it's leaving me stumped!!

Current Code is as follows:
for /F "usebackq tokens=1-4* delims=,=" %%A in ("%MENU_INI_FILE%") do (
	set Name=%%~A
	if /I "!Name:~0,15!" EQU "SDQ_MAINMENUOPT" (
		if "!NewMenuName!" EQU "" (set NewMenuName=%%~B) else (set NewMenuName=!NewMenuName!,%%~B)
		if "!NewMenuLoca!" EQU "" (set NewMenuLoca=%%~C) else (set NewMenuLoca=!NewMenuLoca!,%%~C)
		if "!NewMenuAdm!" EQU "" (set NewMenuAdm=%%~D) else (set NewMenuAdm=!NewMenuAdm!,%%~D)
		REM CALL :check_filetype
	)
)

set /a i=0
for %%A in (%NewMenuName%) do (
	set /a i+=1
	echo.   [!i!] - %%A
)

Open in new window


The 1st part within the ini value will be the name of the option, which when input will have underscores under every word (to keep it all as one value when read).  When the script builds the menu, I want it to strip out the underscores and replace with a space...

I've done this before for filenames etc where I would use
newvariable=%oldvariable:_= %

Open in new window


But I can't seem to get this to work anywhere without either falling over, or repeating the last value over and over again!  Any help appreciated!
0
Comment
Question by:Jofnn
  • 8
  • 5
  • 4
  • +2
23 Comments
 
LVL 6

Expert Comment

by:LeeeRussell
Comment Utility
You need the set command in front of the bit you've written, i.e.

SET newvariable=%oldvariable:_= %
0
 
LVL 3

Author Comment

by:Jofnn
Comment Utility
LeeeRussell - Sorry, missed that part out, but I'm aware that it needs to be there.

Basically, I can't seem to get that sort of principal to work within the menu building section...
0
 
LVL 6

Expert Comment

by:LeeeRussell
Comment Utility
Sorry, I'm in a bit of a rush so I can't give this a lot of attention, but you might want to check out delayed expansion in the for loop.  Have google around for SETLOCAL ENABLEDELAYEDEXPANSION
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
==< Jofnn,

Can you share the code that isn't working, as well as the format of the INI file please.

~bp

0
 
LVL 3

Author Comment

by:Jofnn
Comment Utility
Thanks LeeeRussell,

SETLOCAL ENABLEDELAYEDEXPANSION is already on the script (the script has about 2000+ lines before this section!!) which was required as part of the code shown above, alongside some of the other code used throughout the script!

The for loop and everything is working fine at the moment and the menu's build as they should however, as you can imagine... seeing "OPTION_NAME_NUMBER_ONE" isn't quite as nice as seeing "OPTION NAME NUMBER ONE"... (Having said that, the underscores need to be there, as another script relies on them!)

*-*-*-*-*-*

BillPrew - the above code (you might want to put a PAUSE at the end of it so it doesn't just close on you) works absolutely fine... builds the menu's based on the data in the ini file

FORMATTED AS:      SDQ_MAINMENUOPT=OPTION_NAME_NUMBER_ONE,\\server\share\folder\file.ext,Y

The code above strips out the part between the "=" and the first comma and puts it in a menu... What I want to achieve is, before it builds the menu - it strips out the underscores from the name and shows the nice tidy name instead!

(Does that make sense?)
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
How about just adding:

set %NewMenuName%=%NewMenuName:_= %

after the first FOR loop?

~bp
0
 
LVL 3

Author Comment

by:Jofnn
Comment Utility
Do you mean before it starts the "i" counter?
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
Silly question, but are you are using  !! if it is in a loop? Have you tried echoing the value on the next line to see what it is/isn't doing?

SET newvariable=!oldvariable:_= !

Would be good to see the relevant lines!

Steve
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
Reading more I think Bill is right there (without the first two % of course), I had assumed it was a different bit of code within the loop originally.

echo Was %NewMenuName%
set NewMenuName=%NewMenuName:_= %
echo Now %NewMenuName%

Unless this is within another bigger for command or similar in which case you'll need to use !! still.

Steve
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
==> Do you mean before it starts the "i" counter?

Yes, see attached.

~bp
for /F "usebackq tokens=1-4* delims=,=" %%A in ("%MENU_INI_FILE%") do (
        set Name=%%~A
        if /I "!Name:~0,15!" EQU "SDQ_MAINMENUOPT" (
                if "!NewMenuName!" EQU "" (set NewMenuName=%%~B) else (set NewMenuName=!NewMenuName!,%%~B)
                if "!NewMenuLoca!" EQU "" (set NewMenuLoca=%%~C) else (set NewMenuLoca=!NewMenuLoca!,%%~C)
                if "!NewMenuAdm!" EQU "" (set NewMenuAdm=%%~D) else (set NewMenuAdm=!NewMenuAdm!,%%~D)
                REM CALL :check_filetype
        )
)
 
set %NewMenuName%=%NewMenuName:_= %
set /a i=0
for %%A in (%NewMenuName%) do (
        set /a i+=1
        echo.   [!i!] - %%A
)

Open in new window

0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
And as Steve mentioned, I cut and pasted a bit too much there, so:

set %NewMenuName%=%NewMenuName:_= %

should be:

set NewMenuName=%NewMenuName:_= %

~bp
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 11

Expert Comment

by:Ben Personick
Comment Utility
 If you are using Commas as your delimiter in your INI file you can leave the spaces in the items contained in the INI file.

  Therefore, instead of going through the Hoop of having the variables replace the spaces in the loop, you cold simply change your INI file to have the spaces.

  That would mean your script will be doing less work, and when you change the INI file you will be doing less work too.

  This seems the simplest solution to your troubles no?

You've already written the loop appropriately to ignore spaces in your terms, so then there would be nothing to change except the INI file itself to include the spaces.

  This is a particularly important thing to consider doing because perhaps in the future you may want to HAVE underscores in some part of the variable.
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
I was going to suggest that but the OP said he needed the underscores for another part of the process? http:#33735761

Steve
0
 
LVL 11

Accepted Solution

by:
Ben Personick earned 125 total points
Comment Utility
Ahh thanks Dragon, if that is the case then wouldn't this be the best option?  

1. As for the space replacement:  For-go setting any new variable and replace the spaces as necessary (See Attached Code)

2. I noticed another issue:  Since you can't parse by comma if there are spaces without /F, which would read your list as a single line, you must wrap the list items in quotes, so that it forms a proper loop (Also See Attached Code)

I tested my assumptions and the code is working as stands

Also I notice you are setting the first terms = to a variable so that you can ensure it equals "SDQ_MAINMENUOPT" however that is the ENTIRE first term, is there some reason you don't change this to:IF /I "%%A" EQU "SDQ_MAINMENUOPT"

 unless you are looking to match any line whcih starts with this value even if it has additional characters it's unessessary..  furthermore you could ensure it only reads lines which have those characters in the first term using a Find instead... but I digress, please look at the attached code.


@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "NAME=NOTHING"
SET "NewMenuName="
SET "MENU_INI_FILE=C:\Admin\InterestingMenu\Menu.INI"
for /F "UseBackq Tokens=1-4* Delims=,=" %%A in ("%MENU_INI_FILE%") do (
        SET "Name=%%~A"
        if /I "!Name:~0,15!" EQU "SDQ_MAINMENUOPT" (
               if "!NewMenuName!" EQU "" (set NewMenuName="%%~B") else (set NewMenuName=!NewMenuName!,"%%~B")
               if "!NewMenuLoca!" EQU "" (set NewMenuLoca="%%~C") else (set NewMenuLoca=!NewMenuLoca!,"%%~C")
               if "!NewMenuAdm!" EQU "" (set NewMenuAdm="%%~D") else (set NewMenuAdm=!NewMenuAdm!,"%%~D")
                REM CALL :check_filetype
        )
)

set /a i=0
for %%A in (%NewMenuName:_= %) do (
        set /a i+=1
        echo.   [!i!] - %%~A
)

ENDLOCAL

Open in new window

0
 
LVL 11

Expert Comment

by:Ben Personick
Comment Utility
Just a follow-up, attached is the text file I jimmied up to test this, and below is the output at the command prompt.

 [1] - OPTION NAME NUMBER ONE
  [2] - OPTION NAME NUMBER Two
  [3] - OPTION NAME NUMBER Three
  [4] - OPTION NAME NUMBER Four

~Q

SDQ_MAINMENUOPT=OPTION_NAME_NUMBER_ONE,\\server\share\folder\file.ext,Y
SDQ_MAINMENUOPT=OPTION_NAME_NUMBER_Two,\\server\share\folder\file.ext,Y
SDQ_MAINMENUOPT=OPTION_NAME_NUMBER_Three,\\server\share\folder\file.ext,Y
SDQ_MAINMENUOPT=OPTION_NAME_NUMBER_Four,\\server\share\folder\file.ext,Y

Open in new window

0
 
LVL 3

Author Comment

by:Jofnn
Comment Utility
@BillPrew - thank you, I applied that before the "i" counter but it didn't work... the menu returned like:

[1] - OPTION
[2] - NAME
[3] - NUMBER
[4] - ONE

...and so on!  That was the problem I was facing before... I could strip it, but it would split it over each line or repeat the very last value over and over and over again!

@QCubed - thank you, I added those changes and it works exactly as required for the stripping the _'s!  It's caused a problem with the selection part (with there being " marks around everything", but I should be able to work through that fine!!

Thanks again to all involved :)
0
 
LVL 3

Author Closing Comment

by:Jofnn
Comment Utility
This answered the question exactly as I needed!

Thanks :o)
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
Right... So your problem wasn't replacing the _ with  space then but your loop to display that afterwards splitting it up at each space...
0
 
LVL 3

Author Comment

by:Jofnn
Comment Utility
Basically... the script would run and read the ini file setting variables for each section between the comma's (in the INI file)... So the screen would show:

[1] - MENU_OPTION_NUMBER_ONE
[2] - MENU_OPTION_NUMBER_TWO
[3] - MENU_OPTION_NUMBER_THREE

Dependant on where I tried to split the menu to remove the underscores, it would have one of three scenarios:

Scenario 1 - the menu would build with one word per line (like above) so a 3 line menu with 4 words would turn into a 12 line menu with one word each

Scenario 2 - the variable would run through, and would repeat the final word again and again

Scenario 3 - it would bug out and die!

*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

The problem I'm facing now, is that after the menu is built, this prompts the user to select one of the options... At present, this goes to another screen and echo's the values of the variables that were defined on that specific line on the INI file... which in the future, will lead to another part of the script that determines certain data and acts accordingly...

The problem being - now all of the variables (NewMenuName; NewMenuLoca & NewMenuAdm) all have "" round them... making it a bit more difficult to code it to read these and extract certain info (i.e. the file ext of the NewMenuLoca)... Is there anyway that I can then remove the "" marks?
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
Call :stripQuotes %newname% newname
exit /b

:stripquotes
 set %2=%~1
exit /b

Steve
0
 
LVL 3

Author Comment

by:Jofnn
Comment Utility
Thanks dragon-IT... where is this slotting in though?
0
 
LVL 3

Author Comment

by:Jofnn
Comment Utility
Actually - don't worry about it... I added it into the Process choice section after it's worked out the variable (from stripping out the pre-defined menu).

Thanks again for your help!
0
 
LVL 11

Expert Comment

by:Ben Personick
Comment Utility
Joffn, thank you very much for the points as I head off to bed this AM, To strip the quotes off a term in the lists you can always use %%~A for a loop, and %variable:"=% if you assign the list item to a temp variable.  Or as dragon Suggests pass it through a call to a lable or other batch file.  Strilling the quotes is not hard, it all depends on how yu are using it all though.

If this is unclear I suggest you open a related ticket item so every member of this item will be notified, and post a larger section of your script, which includeds how you want to use list items.

~Q
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

It is only natural that we all want our PCs to be in good working order, improved system performance, so that is exactly how programs are advertised to entice. They say things like:            •      PC crashes? Get registry cleaner to repair it!    …
If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now