Batch File to check second character of each string in .txt file : 50% complete - Need Help

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Batch File to check second character of each string in .txt file : 50% complete - Need Help

#1 Post by SIMMS7400 » 07 Jan 2016 07:56

Team -

Before everyone says use Perl or POWERSHELL - I cant. :)

I have a business need that requires me to develop a batch script to remove instances of the ' symbol from a text file as it's preventing a process from loading into our target systems. The catch is, however, the ' only needs to be removed if it's the second character in a string.

For example

Import File

DOS'
DO'S
D'OS

New File

DOS'
DO'S
D OS

I've written a peice of code that removes ALL instances of the ' symbol so I just need help amending it to only remove it if it's the second character in a string. Here is my code:

Code: Select all

@echo off

setlocal EnableDelayedExpansion
REM This is to allow for variable editing within the for loop

for /f "delims=" %%A in (test1.txt) do (
REM This loops through each line in test.txt
REM and performs everything within () to the line
REM which is held in %%A

    set a=%%A
    echo !a:'= ! >>newfile.txt
    REM this adds the line to newFile.txt replacing every instance of " with no character
)


Any help is greatly appreciated! Thanks!

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Batch File to check second character of each string in .txt file : 50% complete - Need Help

#2 Post by Squashman » 07 Jan 2016 08:12

I will give you a hint.

Use the IF command with a substring to check if the second character is a single quote.
http://www.robvanderwoude.com/ntset.php#SubStr

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: Batch File to check second character of each string in .txt file : 50% complete - Need Help

#3 Post by SIMMS7400 » 07 Jan 2016 10:51

Thank you for the tips! Unfortunately, I cant seem to get it fire. If it wouldn't be too much trouble, would you be able to show me how to use it?

Thank you.


Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Batch File to check second character of each string in .txt file : 50% complete - Need Help

#5 Post by Compo » 07 Jan 2016 11:56

ctakacs wrote:

Code: Select all

@echo off

setlocal EnableDelayedExpansion
REM This is to allow for variable editing within the for loop

for /f "delims=" %%A in (test1.txt) do (
REM This loops through each line in test.txt
REM and performs everything within () to the line
REM which is held in %%A

    set a=%%A
    echo !a:'= ! >>newfile.txt
    REM this adds the line to newFile.txt replacing every instance of " with no character
)

Just a quick note ATM to show you that because of the mistakes in the code and explanation of the opening post, I was compelled to perform a search of this forum and found this

The use of a double quote gave it away and also the fact the OP suggested they were removing a single quote when in fact they are replacing it with a space.

Please explain your task properly without pretending you have written code which doesn't do what your telling us it does!

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: Batch File to check second character of each string in .txt file : 50% complete - Need Help

#6 Post by SIMMS7400 » 07 Jan 2016 12:18

Code: Select all

@echo off

setlocal EnableDelayedExpansion
REM This is to allow for variable editing within the for loop

for /f "delims=" %%A in (test1.txt) do (
REM This loops through each line in test.txt
REM and performs everything within () to the line
REM which is held in %%A

    set a=%%A
    IF "%var:~1,1%"==' echo !a:'= ! >>newfile.txt
    REM this adds the line to newFile.txt replacing every instance of " with no character
)


So what I added was IF "%var:~1,1%"==' which in peusdo for all strings, if the 2nd character is ' then do.....

Am I on the right track?
Last edited by Squashman on 07 Jan 2016 13:23, edited 1 time in total.
Reason: Please use code tags.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Batch File to check second character of each string in .txt file : 50% complete - Need Help

#7 Post by Squashman » 07 Jan 2016 13:11

You have double quotes on the left side of the comparison so you need them on the right side as well.

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: Batch File to check second character of each string in .txt file : 50% complete - Need Help

#8 Post by SIMMS7400 » 07 Jan 2016 13:29

As far as the double quote - I'm not quit sure where you referring to?

Can I call out the ' symbol as I did or will this fail? Perhaps my single quote is whats throwing you off?
Last edited by SIMMS7400 on 07 Jan 2016 13:32, edited 1 time in total.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Batch File to check second character of each string in .txt file : 50% complete - Need Help

#9 Post by Squashman » 07 Jan 2016 13:32

ctakacs wrote:Can I call out the ' symbol as I did or will this fail?

Not understanding your question. Could you rephrase that or provide an example.

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: Batch File to check second character of each string in .txt file : 50% complete - Need Help

#10 Post by SIMMS7400 » 07 Jan 2016 13:34

Code: Select all

@echo off

setlocal EnableDelayedExpansion
REM This is to allow for variable editing within the for loop

for /f "delims=" %%A in (test1.txt) do (
REM This loops through each line in test.txt
REM and performs everything within () to the line
REM which is held in %%A

    set a=%%A
    IF "%var:~1,1%"==' echo !a:'= ! >>newfile.txt
    REM this adds the line to newFile.txt replacing every instance of " with no character
)


So the portion I added says if the 2nd character is a ' symbol then do....

However you said I'm missing a set up double quotes - but I don't see where? I thought perhaps you were thinking this was incorrect (not double quotes) "==' but this is what I'm evaluating for

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Batch File to check second character of each string in .txt file : 50% complete - Need Help

#11 Post by Squashman » 07 Jan 2016 13:58

The IF command is doing a LITERAL string comparison. You are using "double quotes" on the left side of your comparison but not the right side. The quotes are part of the comparison!

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: Batch File to check second character of each string in .txt file : 50% complete - Need Help

#12 Post by SIMMS7400 » 07 Jan 2016 14:00

Code: Select all

@echo off

setlocal EnableDelayedExpansion
REM This is to allow for variable editing within the for loop

for /f "delims=" %%A in (test1.txt) do (
REM This loops through each line in test.txt
REM and performs everything within () to the line
REM which is held in %%A

    set a=%%A
    IF "%var:~1,1%"==' echo "!a:'= !" >>newfile.txt
    REM this adds the line to newFile.txt replacing every instance of " with no character
)


Quotes on the other side of the comparison does not work (see above). Unless I'm missing something? I'm sorry for my lack of knowledge on this subject matter. I'm still in the learning process.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Batch File to check second character of each string in .txt file : 50% complete - Need Help

#13 Post by Squashman » 07 Jan 2016 14:05

Code: Select all

@echo off

setlocal EnableDelayedExpansion
REM This is to allow for variable editing within the for loop

for /f "delims=" %%A in (test1.txt) do (
REM This loops through each line in test.txt
REM and performs everything within () to the line
REM which is held in %%A

    set a=%%A
    IF "!a:~1,1!"=="'" (
       >>newfile.txt echo !a:~0,1! !a:~2!
    ) else (
        >>newfile.txt echo %%A
    )
)
Last edited by Squashman on 07 Jan 2016 14:19, edited 2 times in total.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Batch File to check second character of each string in .txt file : 50% complete - Need Help

#14 Post by Compo » 07 Jan 2016 14:14

Squashman wrote:

Code: Select all

@echo off

setlocal EnableDelayedExpansion
REM This is to allow for variable editing within the for loop

for /f "delims=" %%A in (test1.txt) do (
REM This loops through each line in test.txt
REM and performs everything within () to the line
REM which is held in %%A

    set a=%%A
    IF "!a:~1,1!"=="'" (
       echo !a:'= ! >>newfile.txt
    ) else (
        echo %%A >>newfile.txt
    )
)
Whilst you've fixed the problem you tried explaining earlier, this still isn't doing what it's supposed to be doing.

At The Moment this is replacing every single quote with a space on any line where the second character is a single quote.

What was asked for, (though unconfirmed), was to remove the second character on any line if that character is a double quote.

Maybe something more like this will do

Code: Select all

@Echo Off
SetLocal
Type Nul>NewFile.txt
For /F "Delims=" %%A In (test1.txt) Do Call :Chk %%A
Exit/B

:Chk
Echo(%*|Findstr/b .'>Nul&&Call :Fix %*||Echo(%*>>NewFile.txt
GoTo :EOF

:Fix
Set _=%*
Echo(%_:~,1% %_:~2%>>NewFile.txt
Remove the space on the last line if a space isn't really required.

Please also note that the existence of some poison characters in your input file will potentially ruin the expected results.
Last edited by Compo on 07 Jan 2016 14:18, edited 1 time in total.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Batch File to check second character of each string in .txt file : 50% complete - Need Help

#15 Post by Squashman » 07 Jan 2016 14:17

Compo wrote:
At The Moment this is replacing every single quote with a space on any line where the second character is a single quote.

What was asked for, (though unconfirmed), was to remove the second character on any line if that character is a double quote.

Agreed. But none of the examples showed two single quotes in a line.
Easily fixed though.

Post Reply