• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 797
  • Last Modified:

Strip underscores from variable

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
Jofnn
Asked:
Jofnn
  • 8
  • 5
  • 4
  • +2
1 Solution
 
LeeeRussellCommented:
You need the set command in front of the bit you've written, i.e.

SET newvariable=%oldvariable:_= %
0
 
JofnnAuthor Commented:
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
 
LeeeRussellCommented:
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
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
Bill PrewCommented:
==< Jofnn,

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

~bp

0
 
JofnnAuthor Commented:
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
 
Bill PrewCommented:
How about just adding:

set %NewMenuName%=%NewMenuName:_= %

after the first FOR loop?

~bp
0
 
JofnnAuthor Commented:
Do you mean before it starts the "i" counter?
0
 
Steve KnightIT ConsultancyCommented:
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
 
Steve KnightIT ConsultancyCommented:
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
 
Bill PrewCommented:
==> 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
 
Bill PrewCommented:
And as Steve mentioned, I cut and pasted a bit too much there, so:

set %NewMenuName%=%NewMenuName:_= %

should be:

set NewMenuName=%NewMenuName:_= %

~bp
0
 
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
 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
 
Steve KnightIT ConsultancyCommented:
I was going to suggest that but the OP said he needed the underscores for another part of the process? http:#33735761

Steve
0
 
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
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
 
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
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
 
JofnnAuthor Commented:
@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
 
JofnnAuthor Commented:
This answered the question exactly as I needed!

Thanks :o)
0
 
Steve KnightIT ConsultancyCommented:
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
 
JofnnAuthor Commented:
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
 
Steve KnightIT ConsultancyCommented:
Call :stripQuotes %newname% newname
exit /b

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

Steve
0
 
JofnnAuthor Commented:
Thanks dragon-IT... where is this slotting in though?
0
 
JofnnAuthor Commented:
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
 
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

  • 8
  • 5
  • 4
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now