Computing.Net > Forums > Programming > Copy file, changing contents

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Copy file, changing contents

Reply to Message Icon

Name: phaworth
Date: June 29, 2009 at 11:07:47 Pacific
OS: Windows XP
Subcategory: Batch
Comment:

I need a script to read records from a file, strip
everything up to and including the first "," character from
the record, then write the record to another file.

Thanks,
Pete



Sponsored Link
Ads by Google

Response Number 1
Name: IVO
Date: June 29, 2009 at 12:42:35 Pacific
Reply:

@echo off > output.txt
for /F "tokens=1,* delims=," %%j in ('type "input.txt"') do echo.%%k>> output.txt


0

Response Number 2
Name: phaworth
Date: June 29, 2009 at 13:04:42 Pacific
Reply:

Thanks for the quick response, that helps me out a lot!
Pete


0

Response Number 3
Name: ghostdog
Date: June 29, 2009 at 17:56:36 Pacific
Reply:

if you have gawk for windows (see my sig)

c:\test> gawk -F"," "{$1=\"\";}{sub(/^,/,\"\")}1" OFS="," file.txt

GNU win32 packages | Gawk


0

Response Number 4
Name: phaworth
Date: June 29, 2009 at 18:04:13 Pacific
Reply:

Thanks for both replies. One follow up question - can the
changed data be written back to the same file rather than
output to a different file?
Thanks,
Pete


0

Response Number 5
Name: phaworth
Date: June 29, 2009 at 23:19:38 Pacific
Reply:

Sorry, one more thing on this. Is there a way to check if the
input file exists before executing the commands? I'm finding
that if the input file does not exist, an empty output file is
created which causes problems.
Thanks,
Pete


0

Related Posts

See More



Response Number 6
Name: IVO
Date: June 30, 2009 at 04:11:46 Pacific
Reply:

:: MYCOPY.BAT Usage: mycopy "Pathname_Filename"
@echo off
if not exist "%~1" (echo.File "%~1" not found & goto :EOF) 
type nul > "%~dpn1.tmp"
for /F "tokens=1,* delims=," %%j in ('type "%~1"') do echo.%%k>> "%~dpn1.tmp"
del "%~1"
ren "%~dpn1.tmp" "%~1"
echo.File "%~1" was copied and edited.
:: End_Of_Batch

example: mycopy "C:\Documents and Settings\My data\FileIn.txt"

Don't miss the double quotes embracing filename.


0

Response Number 7
Name: phaworth
Date: June 30, 2009 at 10:23:17 Pacific
Reply:

Thanks a lot, that all works perfectly!


0

Response Number 8
Name: phaworth
Date: August 18, 2009 at 13:13:56 Pacific
Reply:

This code has been working fine for me for several months.
Just started to use it on a file that contains some html code
and it's not handling it very well.

The file looks something like this:

2,words words words!

<form action="https://www.paypal.com/cgi-bin/webscr" 
method="post">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" 
value="7449166">
<input type="image" 
src="https://www.paypal.com/en_US/i/btn/btn_buynow_LG.gi
f" border="0" name="submit" alt="PayPal - The safer easier 
way to pay online!">
<img alt="" border="0" 
src="https://www.paypal.com/en_US/i/scr/pixel.gif" 
width="1" height="1">
</form>

There's actually an html line break tag after "words" but it's getting interpreted as a real line break in this message so you don;t see it.

AFter processing the file looks like this:

words words words

Everything after the line break tag is replaced with white space.

Any ideas?

Thanks,
Pete


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Copy file, changing contents

copy file where date is prev month www.computing.net/answers/programming/copy-file-where-date-is-prev-month/7477.html

Batch - Copy Files using .txt List www.computing.net/answers/programming/batch-copy-files-using-txt-list/18010.html

copy files www.computing.net/answers/programming/copy-files/13221.html