Dos Batch file to replace ! point with a space in text file

February 8, 2012 at 19:37:00
Specs: Windows XP
I currently have a text file (Myfile.txt) containing multiple records. Some of the records contain an ! point within the text. I just want to replace the ! point with a space. Can someone help me with creating a DOS batch file which could do this? I searched this site and googled but can't find any good samples. Any help is greatly appreciated

MyFile
013903 Sally Johnson!445 Sanders Lane!Arizona
039034 John Smith!3930 Orleans St!California

NewFile
013903 Sally Johnson 445 Sanders Lane Arizona
039034 John Smith 3930 Orleans St California


See More: Dos Batch file to replace ! point with a space in text file

Report •

#1
February 8, 2012 at 20:51:16
The ! is used as a delimiter between fields. I would think twice about blindly replacing them.

Report •

#2
February 8, 2012 at 23:46:40
ALTER.EXE can do it. See [Solved] Anybody Have a Link to ALTER.ZIP?

ALTER MyFile.txt 33 32

If you're doing this in Win32, see the other apps on the pages describing ALTER.


Report •

#3
February 9, 2012 at 04:15:01
Assumptions:
o Batch file will reside in same directory as "MyFile.txt"
o "MyFile.txt" strictly adheres to the example given, relating to number of fields

@ECHO OFF

FOR /F "usebackq tokens=1-3 delims=!" %%A IN ("%~dp0\Myfile.txt") DO (
    >>"%~dp0\NewFile.txt" ECHO.%%A %%B %%C
)

EXIT /B

If more delimiters and fields exist, increase the number of tokens and variables to be ECHO'ed into NewFile.txt. For example:

tokens=1-3
013903 Sally Johnson!445 Sanders Lane!Arizona
%%A                 !%%B             !%%C

tokens=1-7
013903 Sally Johnson!445 Sanders Lane!Arizona!USA!Earth!Milky Way!Universe
%%A                 !%%B             !%%C    !%%D!%%E  !%%F      !%%G 

When your only tool is a hammer, every problem looks like a nail.


Report •

Related Solutions

#4
February 9, 2012 at 07:41:38
Thanks OrangeBoy. I think I might have made my example to simplistic and should have provided a better sample which actually represents the data I am working with. I actually have 17 fields, but each field is not parsed by a space. Thus, I am not sure how to incorporate your logic when it comes to the token section. As per my example below, the ! point can only occur starting in position 32 through 62 representing the client name. I have provided a better example of what the data really looks like below.

Based on this, can you help me modify the code you provided?

I greatly appreciate your help.


MyFile.txt

7777DD   9993933.00000 00000000ABC! BRANDS INC               20100617201210012 A0.000   99999.75            220110100XXEWEUR1.93494348
3007DD   1343432.50000 00000000SA!A LEE oRGANIZATION         20090710201203012 X0.000   83943.45            220120100DDWUAUD6.93939393
3007xx   1343432.50000 00000000John! Larry! Johnson Smith!   20090710201203012 X0.000   83943.45            220120100CDWUAUD6.93939393

NewFile.txt - should look like this:

7777DD   9993933.00000 00000000ABC  BRANDS INC               20100617201210012 A0.000   99999.75            220110100XXEWEUR1.93494348
3007DD   1343432.50000 00000000SA A LEE oRGANIZATION         20090710201203012 X0.000   83943.45            220120100CDWUAUD6.93939393
3007xx   1343432.50000 00000000John  Larry  Johnson Smith    20090710201203012 X0.000   83943.45            220120100CDWUAUD6.93939393

Based on this, can you help me modify the code you provided?

I greatly appreciate your help.


Report •

#5
February 11, 2012 at 08:35:48
A little modification will be needed. Given an absolute worse case scenario:

------   -------.----- --------1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 ----------------- --.---   -----.--            -----------------.--------
------   -------.----- --------A!B!C!D!E!F!G!H!I!J!K!L!M!N!O!----------------- --.---   -----.--            -----------------.--------

We can see that a maximum of 15 tokens can be expected, represented by variables
A - O. The following should account for this worse case scenario:

@ECHO OFF
FOR /F "usebackq tokens=1-15 delims=!" %%A IN ("%~dp0\Myfile.txt") DO (
    CALL :Write_NewFile %%A %%B %%C %%D %%E %%F %%G %%H %%I %%J %%K %%L %%M %%N %%O
)
EXIT /B

:Write_NewFile
>>"%~dp0\NewFile.txt" ECHO.%*
GOTO :EOF

By introducing a call to the :Write_NewFile "subroutine", the original record
length will be preserved by dropping the trailing blanks introduced by the
spaces between any unused variables.

When your only tool is a hammer, every problem looks like a nail.


Report •

#6
February 23, 2012 at 08:16:14
Hi OrangeBoy,
I tried your method, but still have a some problem. I added some additinal logic in front of your logic. My logic basically prompts the user for the month (mm), day (dd), and year (ccyy) in order to select the filename to open and then performs the replace logic you provided me. Everything seems to be working except when it tries to find the filename. For some reason it addes a period to the end of the file name. For example, if the file name is 10120120214.txt the program adds an extra period at the end of the file. Thus, it would look like this 10120120214.txt. Can you tell me why that may be occuring or how to get rid of it. Below is the latest logic:

 
@echo OFF & setlocal enabledelayedexpansion

 

      set /p mo=month (mm):
        if %mo% lss 01 goto :err
        if %mo% gtr 12 goto :err
      set /p day=day (dd):
        if %day% lss 01 goto :err
        if %day% gtr 31 goto :err
      set /p yr=year (ccyy):



del \\chiabcx04\DATA04\tempA.txt


@ECHO OFF
FOR /F "usebackq tokens=1-15 delims=!" %%A IN ("%~dp0\\chiabcx04\DATA04\101%yr%%mo%%day%.txt") DO (
    CALL :Write_NewFile %%A %%B %%C %%D %%E %%F %%G %%H %%I %%J %%K %%L %%M %%N %%O
)
EXIT /B

:Write_NewFile
>>"%~dp0\tempA.txt" ECHO%*
GOTO :EOF

:ERR
echo ERROR


Report •

Ask Question