# Solved: pulling out hair over batch code... HELP PLEASE!!



## panicy (Aug 19, 2010)

hello everyone

so I'm working on a batch file that will take out extra whitespace and a trailing backslash at the end of a line from a text file.

I found the site http://jonkruger.com/blog/2008/01/07/minor-batch-file-tricks/ 
and this works perfect when I'm hardcoding in specifics paths that I want it to remove the backslash and whitespace. (I've also added in lines for removing whitespace in the same fashion by instead of using "\" I use " ")

However, the problem is that I need it in a for loop to pull multiple lines from a text file, which doesn't sound that it should involve that much.

So for example, say that you have a text file called "mytext.txt" and the files looks like:
c:\program files\my folder\myfile.exe\ (3 spaces at the end also)
c:\program files\my other folder\myotherfiles.exe\ (No spaces at the end)

With that in mind I am setting up the for loop as follows:
for /f "tokens=*" %%i in (mytext.txt) do (
set MyVar=%%i
IF "%MyVar:~-1%"==" " SET MyVar=%MyVar:~0,-1%
IF "%MyVar:~-1%"==" " SET MyVar=%MyVar:~0,-1%
IF "%MyVar:~-1%"==" " SET MyVar=%MyVar:~0,-1%
IF "%MyVar:~-1%"==" " SET MyVar=%MyVar:~0,-1%
IF "%MyVar:~-1%"==" " SET MyVar=%MyVar:~0,-1%
IF "%MyVar:~-1%"=="\" SET MyVar=%MyVar:~0,-1%"
echo %MyVar% >> myoutput.txt
)

the purpose is it first looks for trailing whitespace at the end of the line - 5 times. Then after making sure that there isn't anymore whitespace then it checks for a trailing slash and if it finds that then it removes that also.

For some reason this is failing on me...

If I remove the for loop and hardcode in a path it works fine
ie;
set MyVar=c:\program files\my folder\myfile.exe\ (3 spaces used as an example here)
IF "%MyVar:~-1%"==" " SET MyVar=%MyVar:~0,-1%
IF "%MyVar:~-1%"==" " SET MyVar=%MyVar:~0,-1%
IF "%MyVar:~-1%"==" " SET MyVar=%MyVar:~0,-1%
IF "%MyVar:~-1%"==" " SET MyVar=%MyVar:~0,-1%
IF "%MyVar:~-1%"==" " SET MyVar=%MyVar:~0,-1%
IF "%MyVar:~-1%"=="\" SET MyVar=%MyVar:~0,-1%
echo %MyVar% >> output.txt
)

I'm getting really frustrated. I just dont get it. I'm doing the same thing with the for loop just substituting the hardcoded path with the variable path from the txt file.

Someone smarter then me please help. Thanks for looking


----------



## ghostdog74 (Dec 7, 2005)

You are using the wrong tool for the job. Use a proper file processing tool instead of batch. There are many, Perl/Python, sed/gawk, even vbscript. Start to learn how to use them for your own benefit. 
I will show you how to do it with  sed for windows 


```
c:\test> sed -i.bak "s/[ \t\/]*$//" file
```
that's it. Just one line.


----------



## TheOutcaste (Aug 8, 2007)

Since the For statement is actually all on one line, you have to enable Delayed Expansion, and use the *!* symbol for the variables instead of *%*.
See *Set /?* for more info on Delayed Expansion.

You need to remove the space before the *>>* when you output the line, or you will be adding a trailing space.
The line that removes the backslash will actually replace it with a *"*, not sure that's what you intended, so I removed the trailing double quote in the code below.

Give this a try:

```
SetLocal EnableDelayedExpansion
for /f "tokens=*" %%i in (mytext.txt) do (
set MyVar=%%i
IF "!MyVar:~-1!"==" " SET MyVar=!MyVar:~0,-1!
IF "!MyVar:~-1!"==" " SET MyVar=!MyVar:~0,-1!
IF "!MyVar:~-1!"==" " SET MyVar=!MyVar:~0,-1!
IF "!MyVar:~-1!"==" " SET MyVar=!MyVar:~0,-1!
IF "!MyVar:~-1!"==" " SET MyVar=!MyVar:~0,-1!
IF "!MyVar:~-1!"=="\" SET MyVar=!MyVar:~0,-1!
echo !MyVar!>> myoutput.txt
)
```


----------



## Squashman (Apr 4, 2003)

http://cypressor.twoday.net/stories/4496343/


----------



## panicy (Aug 19, 2010)

thanks for the help everyone!!

ghostdog74- I know batch isn't very good for parsingSetLocal EnableDelayedExpansion text. It isn't my first choice either. I originally wanted to do this in python, but the boss wanted batch. I dont know why, I've just learned when to complain and when not to, and this was a "when not to" situation. So batch it is...  But thank you

TheOutcaste- This was good insight for me. I actually did call "SetLocal EnableDelayedExpansion" within the code I just forgot to copy and paste that into my post. I was in a hurry to get it uploaded and this is the reason for the other " at the end of the last line also. What you said about not including a space before the ">>" was huge. I looked right over it (sigh... I can't believe I missed that). The reason that there was whitespace is because I parsed through it a few times before and DID include a space before >> which is what gave the extra whitespace(s). So after redoing some of the code before it and taking out the spaces before >> it then parsed down to no extra whitespace and then I was able to just use the one if statement to remove the trailing backslash within the for loop. I did change the variables to ! instead of %. That was great, thank you so much

Squashman- I searched google with very possible query related to whitespace and batch and never came across this site. This was good info too. Thanks!


----------



## TheOutcaste (Aug 8, 2007)

There's a small bug in the routine on the page Squashman linked. You must have enabled Delayed Expansion before calling the routine, or RESULT gets set to !RESULT:~1,-1! (the actual text, not the expanded variable).
This is because the Endlocal statement reverts Delayed Expansion to the state is was in _before_ the SetLocal statement that enabled it in the TRIM routine. If it was disabled before calling TRIM, the delayed expansion of *!RESULT:~1,-1!* doesn't happen, and that is seen as a text string, not a variable.


----------



## ghostdog74 (Dec 7, 2005)

panicy said:


> thanks for the help everyone!!
> ghostdog74- I know batch isn't very good for parsingSetLocal EnableDelayedExpansion text. It isn't my first choice either. I originally wanted to do this in python, but the boss wanted batch. I dont know why, I've just learned when to complain and when not to, and this was a "when not to" situation. So batch it is...  But thank you


that's the kind of boss i don't like. Obviously, he doesn't know anything, except the only he knows is sitting there taking his fat salary. I am also not sure if he is talking about PURE cmd.exe commands. If not, write your python script (or using nix tools like sed i mentioned), put your python execution/sed command in a .bat file, and show him your batch file.

another alternative is to use vbscript, which is native to windows. I am sure your boss will have to agree if his concern is you are using "downloaded" stuff.

PS: Note, if you want to use the solution that is posted by Squashman's link, you should also note that whitespaces are not just spaces and tabs. It consists of newlines "\n", "\r" , null (\x0b) , form feed (\x0c) etc...as well. So that bunch of code will still break.
Its a lot better to use tools that support regex, eg [[:blank:]]


----------

