tags:

views:

41

answers:

3

Hi, if I have a .txt that has the following:

2005050 "2/19/2005"
2005060 "3/1/2005"
2005070 "3/11/2005"
2005080 "3/21/2005"
2005090 "3/31/2005"

Is there a way for the batch file to read and always add .png in the end of a character of 7.

For example.

2005050.png "2/19/2005"
2005060.png "3/1/2005"
2005070.png "3/11/2005"
2005080.png "3/21/2005"
2005090.png "3/31/2005"

+3  A: 

This batch file will split each line at the first space, and append .png to the string before the split. The script reads lines from infile.txt and outputs to outfile.txt.

@echo off
echo. > outfile.txt
for /f "tokens=1*" %%i in (infile.txt) do echo %%i.png %%j >> outfile.txt

Update

Or to delete the outfile.txt first....

@echo off
del /q /f outfile.txt
for /f "tokens=1*" %%i in (infile.txt) do echo %%i.png %%j >> outfile.txt

Another Update

To just add new records to outfile.txt do something like....

@echo off
for /f "tokens=1*" %%i in (infile.txt) do (
    find "%%i.png %%j" outfile.txt > nul
    if errorlevel 1 then (
        echo %%i.png %%j >> outfile.txt
    )
)
Andrew Cooper
echo. > outfile.txt is not needed. It creates a first empty line in outfile.txt
DmitryK
Thanks again!!!
steven
I put it in to make sure that outfile.txt was blank before the for loop started appending to it. I could probably just use del to make sure it's not there instead.
Andrew Cooper
That's what I get for walking away from the computer for a few minutes :) Good answer
LittleBobbyTables
just reran the batch and it repeats the name over and over . Is there a way to only have those new lines to show up once and not keep repeating. I get this:2005050.png "2/19/2005"2005060.png "3/1/2005"2005070.png "3/11/2005"2005080.png "3/21/2005"2005090.png "3/31/2005"2005050.png "2/19/2005"2005060.png "3/1/2005"2005070.png "3/11/2005"2005080.png "3/21/2005"2005090.png "3/31/2005"2005050.png "2/19/2005"2005060.png "3/1/2005"2005070.png "3/11/2005"just want to have those line there only once rather than multiple times.
steven
The problem is the for loop appends to the output file. To avoid repeating the records you need to delete the output file or blank it out first.
Andrew Cooper
the problem is that I will need to keep updating the list with new txt from the original. So right now If I added let say 2005091.png "4/1/2005" to the original list and ran that batch file. I would get the duplicate names going down the line and the new one. Where I need is only the of them.
steven
The simplest way to do it is just to recreate outfile.txt completely each time you update the source file. Will this not work for you?
Andrew Cooper
See new update. And please accept the answer.
Andrew Cooper
A: 

Sed for windows

sed -r "s/^(.......)(.*)/\1.png\2/" file
A: 

My Answer failed: I did try: but I couldn't get the SET to WORK within the FOR

@echo off

set str1=ooo
set str2=ppp

for /f "tokens=*" %%a in ('type testprog.txt') do (

    set str=!%%a!
    echo %%str:~0,7%%.png %%str:~-5,14%% >> tempprog.txt

)

move tempprog.txt testprog.txt
start testprog.txt

Maybe someone can edit a working version as I would like to see what I did wrong...

Edoctoor