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!CaliforniaNewFile
013903 Sally Johnson 445 Sanders Lane Arizona
039034 John Smith 3930 Orleans St California
The ! is used as a delimiter between fields. I would think twice about blindly replacing them.
ALTER.EXE can do it. See [Solved] Anybody Have a Link to ALTER.ZIP? ALTER MyFile.txt 33 32If you're doing this in Win32, see the other apps on the pages describing ALTER.
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 /BIf 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 !%%GWhen your only tool is a hammer, every problem looks like a nail.
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.txt7777DD 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.93939393NewFile.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.
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 :EOFBy 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.
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
| « non-printable display in ... | exception error in main » |