Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Strip underscores from variable

Posted on 2010-09-22
23
Medium Priority
?
793 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
ID: 33735413
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
ID: 33735569
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
ID: 33735617
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 59

Expert Comment

by:Bill Prew
ID: 33735709
==< 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
ID: 33735761
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 59

Expert Comment

by:Bill Prew
ID: 33735799
How about just adding:

set %NewMenuName%=%NewMenuName:_= %

after the first FOR loop?

~bp
0
 
LVL 3

Author Comment

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

Expert Comment

by:Steve Knight
ID: 33735830
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
ID: 33735900
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 59

Expert Comment

by:Bill Prew
ID: 33737256
==> 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 59

Expert Comment

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

set %NewMenuName%=%NewMenuName:_= %

should be:

set NewMenuName=%NewMenuName:_= %

~bp
0
 
LVL 14
ID: 33738965
 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
ID: 33739343
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 14

Accepted Solution

by:
Ben Personick (Previously QCubed) earned 500 total points
ID: 33739783
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 14
ID: 33739818
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
ID: 33741677
@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
ID: 33741681
This answered the question exactly as I needed!

Thanks :o)
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 33741820
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
ID: 33741873
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
ID: 33741897
Call :stripQuotes %newname% newname
exit /b

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

Steve
0
 
LVL 3

Author Comment

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

Author Comment

by:Jofnn
ID: 33741984
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 14
ID: 33742000
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

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

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 …
This article provides a convenient collection of links to Microsoft provided Security Patches for operating systems that have reached their End of Life support cycle. Included operating systems covered by this article are Windows XP,  Windows Server…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Screencast - Getting to Know the Pipeline
Suggested Courses

916 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