Petri.com forums Home Forums Start Page Forums Frequently Asked Questions FAQ Member List Members List
Go Back   Petri IT Knowledgebase Forums > Windows Scripting > General Scripting
Petri.com is happy to award RicklesP the title of Most Valuable Member !!!
Register Calendar Calendar Search Petri IT Knowledgebase Forums Search Todays Posts Today's Posts Mark Forums Read
Notices

using batch script to delete or modify data in a text file

using batch script to delete or modify data in a text file

this thread has 7 replies and has been viewed 68952 times

Closed Thread
 
Thread Tools Search this Thread Display Modes
  #1  
Old 16th May 2008, 18:42
ttt123 ttt123 is offline
Casual
Casual
 
 Join Date: May 2008
  6 month star 12 month star
 Posts: 5
 Reputation: ttt123 is on a distinguished road (10)
Default using batch script to delete or modify data in a text file

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  
Old 16th May 2008, 20:52
Rems's Avatar
Rems Rems is offline
Moderator
 
 Join Date: Mar 2005
  6 month star 12 month star
 Location: NL
 Posts: 2,434
 Reputation: Rems is a splendid one to beholdRems is a splendid one to beholdRems is a splendid one to beholdRems is a splendid one to beholdRems is a splendid one to beholdRems is a splendid one to beholdRems is a splendid one to beholdRems is a splendid one to behold (965)
Default Re: using batch script to delete or modify data in a text file

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  
Old 16th May 2008, 21:43
ttt123 ttt123 is offline
Casual
Casual
 
 Join Date: May 2008
  6 month star 12 month star
 Posts: 5
 Reputation: ttt123 is on a distinguished road (10)
Default Re: using batch script to delete or modify data in a text file

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  
Old 16th May 2008, 23:30
Rems's Avatar
Rems Rems is offline
Moderator
 
 Join Date: Mar 2005
  6 month star 12 month star
 Location: NL
 Posts: 2,434
 Reputation: Rems is a splendid one to beholdRems is a splendid one to beholdRems is a splendid one to beholdRems is a splendid one to beholdRems is a splendid one to beholdRems is a splendid one to beholdRems is a splendid one to beholdRems is a splendid one to behold (965)
Default Re: using batch script to delete or modify data in a text file

Quote:
Originally Posted by ttt123
FOR /F "usebackq tokens=* delims=" " %G IN (textfile.txt) DO ECHO %G
You showed the syntax for a command-line, in a batch one should use %%G of course.


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"
If you like to set enabledelayedexpansion as default so it is also enabled when using a command-line from a command-prompt, you'll have to add a dword entry DelayedExpansion = 1 to the registry
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  
Old 18th May 2008, 06:07
ttt123 ttt123 is offline
Casual
Casual
 
 Join Date: May 2008
  6 month star 12 month star
 Posts: 5
 Reputation: ttt123 is on a distinguished road (10)
Default Re: using batch script to delete or modify data in a text file

Thank you so much! Your script works like a charm!

JT
  #6  
Old 24th July 2008, 11:59
mrt2005 mrt2005 is offline
Casual
Casual
 
 Join Date: Jul 2008
  6 month star 12 month star
 Posts: 9
 Reputation: mrt2005 is on a distinguished road (10)
Default Re: using batch script to delete or modify data in a text file

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  
Old 24th July 2008, 13:40
Rems's Avatar
Rems Rems is offline
Moderator
 
 Join Date: Mar 2005
  6 month star 12 month star
 Location: NL
 Posts: 2,434
 Reputation: Rems is a splendid one to beholdRems is a splendid one to beholdRems is a splendid one to beholdRems is a splendid one to beholdRems is a splendid one to beholdRems is a splendid one to beholdRems is a splendid one to beholdRems is a splendid one to behold (965)
Default Re: using batch script to delete or modify data in a text file

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 ------------
note:
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  
Old 24th July 2008, 14:21
mrt2005 mrt2005 is offline
Casual
Casual
 
 Join Date: Jul 2008
  6 month star 12 month star
 Posts: 9
 Reputation: mrt2005 is on a distinguished road (10)
Default Re: using batch script to delete or modify data in a text file

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
Closed Thread


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

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


All times are GMT +3. The time now is 23:35.

Steel Blue 3.5.4 vBulletin Style ©2006 vBEnhanced
Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
 

Valid XHTML 1.0!   Valid CSS!

Copyright 2005 Daniel Petri