Jump to content

Recommended Posts

Posted

Hi,

I have problems writing a batch file that could extract the last character or strip a string.

TEST.CMD

FOR /F "tokens=1*" %%F IN (TEST.TXT) DO (

ECHO %%G <-- should take Test_ and return _
ECHO %%G <-- should take Test_ and return Test

)

PAUSE
EXIT

1) I can't find a way to read %%G and echo at the last character

2) I can't find a way to read %%G and echo %%G minus one character

I know about %PATH:~-1% but I can get it to work in my case.

Thanks

:wacko:


Posted

Does this help you?

@ECHO OFF
FOR /F "DELIMS=" %%? IN ("Test_") 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/
PAUSE
EXIT

Posted (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 OFF

FOR /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/

)

PAUSE
EXIT

:}

Edited by jdoe
Posted

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&SETLOCAL
FOR /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

Posted

Here is another way to get the last character using VBS script.

This collect all the files where this script is located

Then it uses the InStr to filter out text files

Then 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"

Posted

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.

Posted

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 OFF
FOR /F "delims=" %%A IN (TEST.TXT) DO (
call :get_char %%A
)
pause
goto quit

:get_char
set var=%1
if "%var:~-1%"=="_" (
echo IT'S AN UNDERSCORE
) else (
Echo not an underscore
)
goto quit




:quit

Posted

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_LINE
I_AM_A_LINE_TOO
I AM ANOTHER LINE WITH A TRAILING UNDERSCORE_
I AM ANOTHER LINE WITH TWO TRAILING UNDERSCORES TOO__
I AM ANOTHER LINE
I AM ANOTHER LINE TOO

As 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&SETLOCAL
FOR /F "DELIMS=" %%? IN (TEST.TXT) DO CALL :SUB "%%?"
PAUSE&ENDLOCAL&GOTO :EOF
:SUB
SET "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&SETLOCAL
FOR /F "DELIMS=" %%? IN ('FINDSTR/E "_" TEST.TXT') DO CALL :SUB "%%?"
PAUSE&ENDLOCAL&GOTO :EOF
:SUB
SET "VAR=%~1"
ECHO/ %VAR%&ECHO/ Changes To&ECHO/ %VAR:~0,-1%&ECHO/

Here is an example using delayed expansion

@ECHO OFF&SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "DELIMS=" %%? IN ('FINDSTR/E "_" TEST.TXT') DO (
SET "VAR=%%?"&&ECHO/ !VAR!&ECHO/ Changes To&ECHO/ !VAR:~0,-1!&ECHO/)
PAUSE&ENDLOCAL&GOTO :EOF

I hope this helps you a little better!

Posted

Just didn't account for spaces.

@ECHO OFF
FOR /F "delims=" %%A IN (TEST.TXT) DO (
call :get_char %%A
)
pause
goto :EOF

:get_char
set var=%*
echo %var%
if "%var:~-1%"=="_" (
echo IT'S AN UNDERSCORE
) else (
Echo not an underscore
)
goto :EOF

Posted (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 by jdoe

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...