Solved Create a new file with name based on content of other file

April 8, 2014 at 11:03:18
Specs: Windows 7
Hello.

First of all thank you for the help!

I have a txt file that is comma separated structured like this: store,supplier,barcode,qty

My goal is to create new files named with store+supplier containing all the matching lines.

can anyone help me?

Thank you!!


See More: Create a new file with name based on content of other file

Report •


#1
April 8, 2014 at 19:23:35
Please give a realistic sample of the CSV file. Some have quotes, some don't.
I assume strings are quoted in this prototype:
@echo off & setlocal
for /f "tokens=*" %%a in (pscouto) do call :xx %%a
goto :eof

:xx
echo %*
set store=%~1
set supplier=%~2
:: optional foll line to remove commas from filenames
set supplier=%supplier:,=_%
set store=%store:,=_%
echo FILE:%store%-%supplier%
>>"%store%-%supplier%" echo "%store%","%supplier%",%3,%4


Report •

#2
April 9, 2014 at 04:58:59
Many thanks for the fast answer!!
My file is like this, with no quotes on strings:

Loja 4,Saner,17:08.33 07-Apr-2014 ,5601163060518,55,,
Loja 4,Makro,17:08.33 07-Apr-2014 ,5601163100313,65,,
Loja 4,Recheio,17:08.33 07-Apr-2014 ,5601024105099,45,,
Loja 4,Recheio,17:08.33 07-Apr-2014 ,8410108141222,78,,
Loja 2,Makro,17:08.33 07-Apr-2014 ,5000281003337,789,,
Loja 2,Makro,17:08.33 07-Apr-2014 ,5000281003337,789,,
Loja 2,Recheio,17:08.33 07-Apr-2014 ,5000281003337,789,,

My goal is to create files based on this one. So for this example it will create:
three files for "loja 4" named loja4_saner ; loja4_makro ; loja4_recheio (with the two lines) And two files for "loja 2" named loja2_makro (with two lines) and loja2_recheio.

Thank you very much for your effort!!


Report •

#3
April 9, 2014 at 19:05:55
✔ Best Answer
Ah! So there's a date/time field you neglected to mention. What do you want done with that item? Other than dealing with that question, this minor alteration to resp#2 should at least approximate your intentions:
@echo off & setlocal
for /f "tokens=*" %%a in (pscouto) do call :xx %%a
goto :eof
:xx
set store=%1
set supplier=%2
echo FILE:%store%-%supplier%
>>"%store%-%supplier%" echo "%store%","%supplier%",%4,%5
::==== end batch-script

Report •

Related Solutions

#4
April 10, 2014 at 02:31:02
Hi! Perfect! Spot on!

Thank you for the time and effort spent!!

Best regards!!

Pedro


Report •

Ask Question