jdoe Posted August 10, 2007 Posted August 10, 2007 Hi,I have problems writing a batch file that could extract the last character or strip a string.TEST.CMDFOR /F "tokens=1*" %%F IN (TEST.TXT) DO ( ECHO %%G <-- should take Test_ and return _ ECHO %%G <-- should take Test_ and return Test)PAUSEEXIT1) I can't find a way to read %%G and echo at the last character2) I can't find a way to read %%G and echo %%G minus one characterI know about %PATH:~-1% but I can get it to work in my case.Thanks
Yzöwl Posted August 10, 2007 Posted August 10, 2007 Does this help you?@ECHO OFFFOR /F "DELIMS=" %%? IN ("Test_") DO SET VAR=%%?ECHO/Should take Test_ and return _ECHO/%VAR:~-1%ECHO/ECHO/Should take Test_ and return TestECHO/%VAR:~0,-1%ECHO/PAUSEEXIT
jdoe Posted August 10, 2007 Author Posted August 10, 2007 (edited) Hi Yzöwl,This is exactly what made me post my problem here.I don't know how to make it work inside a FOR /F loop, like this...Let say the file TEST.TXT contains two line...I_AM_A_LINE_I_AM_A_LINE_TOO_batch.cmd@ECHO OFFFOR /F "DELIMS=" %%? IN (TEST.TXT) DO ( SET VAR=%%? ECHO/Should take Test_ and return _ ECHO/%VAR:~-1% ECHO/ ECHO/Should take Test_ and return Test ECHO/%VAR:~0,-1% ECHO/)PAUSEEXIT Edited August 10, 2007 by jdoe
Yzöwl Posted August 10, 2007 Posted August 10, 2007 You could enable and use delayed expansion, however if any of your lines contained exclamation marks, you'd have problems.My suggestion is therefore to use call:@ECHO OFF&SETLOCALFOR /F "DELIMS=" %%? IN (TEST.TXT) DO (SET VAR=%%? ECHO/Should take `String_` and return `_`&CALL ECHO/%%VAR:~-1%%&ECHO/ ECHO/Should take `String_` and return `String`&CALL ECHO/%%VAR:~0,-1%%&ECHO/)PAUSE
gunsmokingman Posted August 10, 2007 Posted August 10, 2007 Here is another way to get the last character using VBS script.This collect all the files where this script is locatedThen it uses the InStr to filter out text filesThen it returns the last character of the file.Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")Dim F1, F2, F3 Set F1 = Fso.GetFolder(Fso.GetParentFolderName(WScript.ScriptFullName)) For Each F2 In F1.Files If InStr(F2.Name,".txt") Then F3 = F3 & F2.Name & vbTab & Right(F2.Name,1) & vbCrLf End If Next MsgBox F3,0 + 32, "Last Character"
jdoe Posted August 10, 2007 Author Posted August 10, 2007 Yzöwl,I know you have the knowledge and you're very close. The way I thought it would be easy was to read the last character and strip it if an underscrore is found. In other words I want to strip the underscore if there is one, from a list of strings, in a set of files. I should have explain this before but read last char and strip it was my main problem. For your information there is no exclamation marks in my files and I'm not used to ENABLEDELAYEDEXPANSION and I didn't found good examples on how to use it.The use of CALL make it work but I can't read the output to be sure it is an "_". I tried to put the CALL in a ('CALL ECHO/%%VAR:~-1%%') and read the output but it's not working.---------------------gunsmokingman,Hi VBScript man. Thanks but the target is a batch file and I will switch to vbs only if I REALLY can't get it to work.
IcemanND Posted August 10, 2007 Posted August 10, 2007 Basically does the same thing that Yzowl does, but calls a "function" within the batch file instead of concatenating all those commands together and gives you an IF statement to check the last character.@ECHO OFFFOR /F "delims=" %%A IN (TEST.TXT) DO ( call :get_char %%A)pausegoto quit:get_charset var=%1if "%var:~-1%"=="_" ( echo IT'S AN UNDERSCORE) else ( Echo not an underscore)goto quit:quit
Yzöwl Posted August 10, 2007 Posted August 10, 2007 Just to ensure that my idea of what you are trying to achieve is correct, here's the TEST.TXT for my examples:I_AM_A_LINE_WITH_A_TRAILING_UNDERSCORE_I_AM_A_LINE_WITH_TWO_TRAILING_UNDERSCORES__I_AM_A_LINEI_AM_A_LINE_TOOI AM ANOTHER LINE WITH A TRAILING UNDERSCORE_I AM ANOTHER LINE WITH TWO TRAILING UNDERSCORES TOO__I AM ANOTHER LINEI AM ANOTHER LINE TOOAs shown by IcemanND, the best way is to use a subroutine, I include this mainly due to an error in Ice's example:@ECHO OFF&SETLOCALFOR /F "DELIMS=" %%? IN (TEST.TXT) DO CALL :SUB "%%?"PAUSE&ENDLOCAL&GOTO :EOF:SUBSET "VAR=%~1"IF %VAR:~-1%==_ ECHO/ %VAR%&ECHO/ Changes To&ECHO/ %VAR:~0,-1%&ECHO/In order to grab only lines ending with an underscore, I suggest using findstr.@ECHO OFF&SETLOCALFOR /F "DELIMS=" %%? IN ('FINDSTR/E "_" TEST.TXT') DO CALL :SUB "%%?"PAUSE&ENDLOCAL&GOTO :EOF:SUBSET "VAR=%~1"ECHO/ %VAR%&ECHO/ Changes To&ECHO/ %VAR:~0,-1%&ECHO/Here is an example using delayed expansion@ECHO OFF&SETLOCAL ENABLEDELAYEDEXPANSIONFOR /F "DELIMS=" %%? IN ('FINDSTR/E "_" TEST.TXT') DO ( SET "VAR=%%?"&&ECHO/ !VAR!&ECHO/ Changes To&ECHO/ !VAR:~0,-1!&ECHO/)PAUSE&ENDLOCAL&GOTO :EOFI hope this helps you a little better!
IcemanND Posted August 10, 2007 Posted August 10, 2007 Just didn't account for spaces.@ECHO OFFFOR /F "delims=" %%A IN (TEST.TXT) DO ( call :get_char %%A)pausegoto :EOF:get_charset var=%*echo %var%if "%var:~-1%"=="_" ( echo IT'S AN UNDERSCORE) else ( Echo not an underscore)goto :EOF
jdoe Posted August 11, 2007 Author Posted August 11, 2007 (edited) Thanks to both of you Yzöwl and IcemanND.I finally get it to work the way I want with a CALL.I qualify myself as a good Google "searcher" but this time I just couldn't find the rigth syntax and using a CALL wasn't, IMHO, obvious.Thanks again, you saved me from using a VBScript. Edited August 11, 2007 by jdoe
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now