1.

Solve : Trip off the last unknown char from a string?

Answer»

I can trip off the last 10 characters from a string without a problem, but when I tried to add a NEW string "\LIB" at the end of the tripped off string "C:\My Documents", there's always a special char at the end of the tripped off string, like "C:\My Documents ", it looks like a space, but I think it's a special char, does anybody knows how to get rid of that space or special char?

I have the following .bat file code :

SET STRING=C:\MyDocuments\ABCDE.bat
IF "%STRING:~-1%"=="t" SET STRING=%STRING:~0,-10%
ECHO String: %STRING%

>>> String: C:\MyDocuments

SET FINALSTR=%STRING%\lib
ECHO FINALSTR : %FINALSTR%

>>> FINALSTR : C:\MyDocuments \lib


- Tiffany
I tried this again, when I execute the the following batch file,

@echo off
SET STRING=C:\MyDocuments\kerah.bat
SET STRING=%STRING:~0,-10%
ECHO %STRING%

SET STRING2=%STRING%\lib
ECHO %STRING2% > tmp.txt
ECHO %STRING2%

I got this result : C:\MyDocuments \lib
with a space before \lib.

But when I tried to execute the above lines from the COMMAND prompt, I got the following result :
C:\MyDocuments\lib
without space before \lib.

Does anybody knows why? I NEED to execute the above lines in a batch file!!!

- TiffanyI believe that you have a white space at the end of the following line:

SET STRING=%STRING:~0,-10%

(you don't see it, but it is there).

If haven't, but you can to delete all spaces with:

@echo off
SET STRING=C:\MyDocuments\kerah.bat
SET STRING=%STRING:~0,-10%
ECHO %STRING%

SET STRING2=%STRING%\lib
REM replace the spaces with nothing
SET STRING2=%STRING2: =%
ECHO %STRING2% > tmp.txt
ECHO %STRING2%



>> More Infmation:

SET /?

Environment variable substitution has been enhanced as follows:

%PATH:str1=str2%

would expand the PATH environment variable, substituting each occurrence of "str1" in the expanded result with "str2". "str2" can be the empty string to effectively delete all occurrences of "str1" from the expanded OUTPUT



Discussion

No Comment Found