![]() |
|
|
|||||||
| Petri.co.il is happy to award auglan the title of Most Valuable Member !!! |
| Register | Calendar |
Search |
Today's Posts |
Mark Forums Read |
| Notices |
|
|
using batch script to delete or modify data in a text filethis thread has 7 replies and has been viewed 53105 times
|
![]() |
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
||||||||
|
||||||||
|
Hi All -
Please show me how to modify or delete data in the text file using a batch script. I am having trouble when trying to use the "for" statement to get rid of the quotation (") in the text file. I was able to delims everything else except quotation ("). Wonder whether anyone has any idea??? Thanks, JT |
|
#2
|
||||||||||
|
||||||||||
|
Can you post your batch file so we know what and how you are trying to do it exactly.
\Rems
__________________
This posting is provided "AS IS" with no warranties, and confers no rights. __________________ ** Remember to give credit where credit's due ** and leave Reputation Points for meaningful posts |
|
#3
|
||||||||
|
||||||||
|
Here is a sample of the text file that I was trying to parse through (contains 3 lines):
528864&ctu=%2Fmailinglist%2Findex%2Ecfm%3Fstory%3D multilist%5EListID%3D7%5Eaction%3Dleave%5Eemail%3D gec460^ui=528864" 1263225&ctu=%2Fmailinglist%2Findex%2Ecfm%3Fstory%3 Dmultilist%5EListID%3D7%5Eaction%3Dleave%5Eemail%3 Dmtrmouthb^ui=1263225" 198266&ctu=%2Fmailinglist%2Findex%2Ecfm%3Fstory%3D multilist%5EListID%3D7%5Eaction%3Dleave%5Eemail%3D yy8740^ui=198266" I am trying to delims the quotation (") at the end of each line by using the for statement, but it never works: FOR /F "usebackq tokens=* delims=" " %G IN (textfile.txt) DO ECHO %G JT |
|
#4
|
||||||||||
|
||||||||||
|
Quote:
Here is an alternative you can use (batch): Code:
@echo off
setlocal enabledelayedexpansion
FOR /F "usebackq delims=" %%G IN ("textfile1.txt") DO (
Set "line=%%G" & echo !line:"=!
)>>"textfile2.txt"
in: HKEY_CURRENT_USER\Software\Microsoft\Command Processor or in: HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor \Rems
__________________
This posting is provided "AS IS" with no warranties, and confers no rights. __________________ ** Remember to give credit where credit's due ** and leave Reputation Points for meaningful posts |
|
#5
|
||||||||
|
||||||||
|
Thank you so much! Your script works like a charm!
JT |
|
#6
|
||||||||
|
||||||||
|
Hi all!
I'm new to batch scripts and petri! Rems, I was using your code to delete text from a .txt file, however the string that I need to delete contains "|" (pipe), the full string I need to delete is 11391|. I can either remove the 11391 with: @echo off setlocal enabledelayedexpansion FOR /F "usebackq delims=" %%G IN ("RBE.txt") DO ( Set "line=%%G" & echo !line:11391=! )>>"RBE1.txt" or remove the entire line with: find /v "11391|" < RBE.txt > RBE1.txt I am so close but not quite there, I've even tried it in quotes or removing "|" seperately. Any help would be great! mrt2005 |
|
#7
|
||||||||||
|
||||||||||
|
You could make use of a subroutine that is called for each line from within the loop - so the string substitution can be performed outside the loop.
example: Code:
@echo off
setlocal enabledelayedexpansion
FOR /F "usebackq delims=" %%G IN ("c:\RBE.txt") DO (
Set Line=%%G
Set Line=!Line:"='!
Call:replace "!Line!"
)
pause
goto:eof ------------
:replace subroutine
(Set Line=%*&Set Line=!Line:~1,-1!)
Set Line=!Line:'="!
Set "Line=!Line:11391|=!"
echo.!Line! >>"c:\RBE.2.txt"
goto:eof ------------
The lines: Set Line=%%G Set Line=!Line:"='! Replaces quote signs (if exist in the file) with a single-quote sign And in the subroutine singe-quotes will we reversed to quote signs again. This way you can avoid that quotes existing in the line will be seen as separate parameter on the call:replace line \Rems
__________________
This posting is provided "AS IS" with no warranties, and confers no rights. __________________ ** Remember to give credit where credit's due ** and leave Reputation Points for meaningful posts |
|
#8
|
||||||||
|
||||||||
|
Rems,
Thank you so much for the quick response, it works a treat. I've been working on this for about a week now! Thanks again, mrt2005 |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Dos batch file for export of syslog data | jdeguerre | General Scripting | 2 | 24th March 2008 17:30 |
| Want to ping default gateway & send results to text file [was: batch file] | ssckrp | General Scripting | 5 | 26th May 2007 00:18 |
| Low Toner Script Writing to text file | ekrengel | General Scripting | 8 | 17th July 2006 16:34 |
| Exporting data to text file using template | Solid | Misc | 2 | 3rd July 2006 22:38 |
| batch file trouble - modify registry using reg add | serus716 | General Scripting | 0 | 26th January 2005 02:56 |