1.

Solve : Batch File to get Strings?

Answer»

Hello All
I am new here and to dos too.

I have a big text file like this:

A1111-001,C:\text\A1111-001.jpg,Y,,,,
A1111-002,C:\text\A1111-002.jpg,,,,
A1111-003,C:\text\A1111-003.jpg,,,,,
A1111-004,C:\text\A1111-004.jpg,Y,,,
A1111-005,C:\text\A1111-005.jpg,,,,

I need a batch file to do get string of the FIRST line with "Y,,," and the last line without that to a new text file.

Exsample like this:
"A1111-001","A1111-003"
"A1111-004","A1111-005"

Thank you for the help! Is this a CSV file? If so, why not use a spreadsheet to parse it?
Using a BAT file to parse a CSV is so retro.
Like a horse and carriage rental.
Quote from: mtank on April 18, 2014, 07:47:12 PM


A1111-001,C:\text\A1111-001.jpg,Y,,,,
A1111-002,C:\text\A1111-002.jpg,,,,
A1111-003,C:\text\A1111-003.jpg,,,,,
A1111-004,C:\text\A1111-004.jpg,Y,,,
A1111-005,C:\text\A1111-005.jpg,,,,

I need a batch file to do get string of the first line with "Y,,," and the last line without that to a new text file.

Exsample like this:
"A1111-001","A1111-003"
"A1111-004","A1111-005"

Thank you for the help!
Your example contradicts your description.It's a text file not csv... Quote from: Squashman on April 18, 2014, 08:40:31 PM
Your example contradicts your description.
He wants the one before the Y and the Y for each Y.


EDIT:
Try this, it will write to new.txt
Code: [Select]echo off
if exist new.txt del new.txt
setlocal EnableDelayedExpansion
for /f "tokens=1-3 delims=," %%A in (test.txt) do (
if "%%C"=="Y" (
if not "!working!"=="" echo "!working!","!a!" >>new.txt
set working=%%A
)
set a=%%A
)
echo "!working!","!a!" >>new.txt
pause >nul

EDIT 2: Just saw you wanted it in a file, so I edited my code to accommodate.That works fine, Lemonilla Quote from: mtank on April 18, 2014, 08:42:53 PM
It's a text file not csv...

A CSV file is a text file with Comma SEPARATED Values, with lines like this: v1, v2, v3, v4

This is what you posted as an example:

Quote from: mtank
I have a big text file like this:

A1111-001,C:\text\A1111-001.jpg,Y,,,,
A1111-002,C:\text\A1111-002.jpg,,,,
A1111-003,C:\text\A1111-003.jpg,,,,,
A1111-004,C:\text\A1111-004.jpg,Y,,,
A1111-005,C:\text\A1111-005.jpg,,,,

Those are comma separated values. That is a csv file, although it may not have a ".csv" extension.
Quote from: Geek-9pm on April 18, 2014, 08:04:03 PM
Is this a CSV file? If so, why not use a spreadsheet to parse it?

Because you would have to click around in the spreadsheet program to get anything done (unless you were so good at scripting you wouldn't be asking here) Powershell has very good csv support, and that is not quite "retro" yet.

The last echo doesn't need delayed expansion. You are already out of the code block. Quote from: Squashman on April 19, 2014, 06:35:51 AM
The last echo doesn't need delayed expansion. You are already out of the code block.
True, ' echo "%working%","%a%" ' would work too, though I don't think there is a difference in performance between the two.Lemonilla you save my life. This code did exactly what i need, THANK YOU! Thanks all the help guys! Quote from: mtank on April 19, 2014, 09:27:56 AM
Lemonilla you save my life. This code did exactly what i need, THANK YOU! Thanks all the help guys!
Thank you for giving me something to do.  I've exhausted all my ideas for scripts awhile AGO.  Its good to have a new challenge.


Discussion

No Comment Found