need help with batch file to edit two text files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
jjlyon
Posts: 18
Joined: 11 Oct 2012 20:43

need help with batch file to edit two text files

#1 Post by jjlyon » 18 Feb 2013 22:18

hi, a while back i got help here with a problem ( a batch program that looked into a text file with a list of items and then made a copy of all the items from one folder into another (renaming the file with a number so as not to overwrite duplicates) this has been extremely useful with my business and i am eternally grateful. now however i need to expand on this:

i have recently started selling another range of items, signs. i use the same batch file to help. the problem is a lot of my online customers buy multiples of the same sign however my old batch program only copys 1 items over :( (for example the list will have "warning sign.bmp", no matter how many i have sold two the same customer it only copys the 1 sign over thats in the list.

what i need is a batch program that looks into two text files. one text file that has a list of numbers (amounts customers have purchased) and the other that has the list of items i have sold. both will have the same number of lines.
i need the batch file to multiply (line by line) the items in the item text file by the numbers in the amounts file. for example

my items.txt file will have the list:

Item two.bmp
Item one.bmp
item two.bmp
Item three.bmp
item one.bmp

the amounts.txt text file will have (each number being the amount 'person A' has purchased of the same item)
2
1
3
3
1

i need the batch program to change the items file to show:

Item two.bmp
Item two.bmp
Item one.bmp
Item two.bmp
Item two.bmp
Item two.bmp
Item three.bmp
Item three.bmp
Item three.bmp
item one.bmp

as you can see the new list is the same as the 1st list however the items in the list (lines) have been multiplied depending on what the amounts.txt file says. if the first line of the amounts.txt file is the number '2' and the first line of the Items.txt file is 'Item One.bmp' then the batch program will duplicate the line to show another 'Item One.bmp' underneath the first :)

if its easier i am happy if the batch file copy's the new list into a different text file, for example a total.txt file

any help would be amazing! i will be using any batch code suggested that helps in my main batch file that contains other batch code for a few other different tasks

jjlyon
Posts: 18
Joined: 11 Oct 2012 20:43

Re: need help with batch file to edit two text files

#2 Post by jjlyon » 18 Feb 2013 22:44

if it helps in any way the codes i use in my batch file (which are without the batch code i need help with) are:

Code: Select all

@echo off
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:set folder="C:\Users\DiskRetail\Desktop\New folder (3)" IS THE LOCATION
:TO DELEAT ALL FILES
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
set folder="C:\Documents and Settings\jjlyon\Desktop\Sign Files\Signs to Print"
cd /d %folder%
for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:CODE THAT ADDS '.bmp' AT THE END OF EACH LINE IN THE TEXT FILE
:Set _t0=C:\Users\DiskRetail\Desktop\New folder (TXT FILE DESTINATION)
:Set _t1=0names.txt (TXT FILE NAME)
:Set _t3=.bmp (WORD TO ADD AT THE END OF EACH LINE)
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Set _t0=C:\Documents and Settings\jjlyon\Desktop\Sign Files
Set _t1=0names.txt
Set _t3=.bmp
PUSHD %_t0%
If EXIST tmp.txt del tmp.txt
For /F "usebackq delims=" %%A in ("%_t1%") do echo %%A%_t3% >>tmp.txt
del "%_t1%"
rename tmp.txt "%_t1%"
For %%A in (0 1 2) do Set _t%%A=
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:CODE THAT COPYS AND RENAMES FILES FROM ONE FOLDER INTO ANOTHER FROM A TEXT LIST
:set "target=C:\Users\DiskRetail\Desktop\New folder\names" (DESTINATION FOLDER)
:for /f "delims=" %%a in (names.txt) do (  (NAME OF TXT FILE)
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
setlocal EnableDelayedExpansion
set "target=C:\Documents and Settings\jjlyon\Desktop\Sign Files\Signs to Print"
set num=0
for /f "delims=" %%a in (0names.txt) do (
set /a num=num+1
set c=00!num!
set c=!c:~-2!
for /f "delims=" %%b in ('dir "%%a" /a:-d /o:n /b /s') do call :next "%%~fa"
)

pause
GOTO:EOF

:next
echo processing %1
pushd "%target%"
set fnum=
:loop
if exist "??%~n1!fnum!%~x1" (
set /a fnum=fnum+1
goto :loop
)
copy /b /y "%~1" "!c!%~n1!fnum!%~x1" >nul
popd




sorry if its a bit messy, my programming skills are very very low, there's snippets of code that i use to help me i have found online in the past or as mentioned above i have had help with on this forum in the past.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: need help with batch file to edit two text files

#3 Post by abc0502 » 19 Feb 2013 00:10

This Should work, Just Provide:
> Item file: That Contain Item names.
> Amount file: That Contain Items Number.
> Total file location: The file where you will get the final result. (it doesn't have to exist, the batch will create it.)
SO Set the First three variable in the batch and run it.

Code: Select all

@Echo OFF

REM ====[ settings ]==================
SET "Items=%userprofile%\desktop\1.txt"
SET "Amount=%userprofile%\desktop\2.txt"
SET "Total=%userprofile%\desktop\Total.txt"

REM ====[ Read Two Files and Multiply ]============
(
   For /F "delims=" %%A in ('Type "%Items%"') Do (
      Setlocal EnableDelayedExpansion
         Set /p line=
         rem !line! is the numbers from %Amount%
         CALL :Multiply "%%A" "!line!"
      Endlocal
   )
)<"%Amount%">>"%Total%"

Pause
Exit /B

:Multiply
For /L %%A In ( 1 1 %~2 ) Do (
   Echo %~1
   )
Goto :EOF


jjlyon
Posts: 18
Joined: 11 Oct 2012 20:43

Re: need help with batch file to edit two text files

#4 Post by jjlyon » 19 Feb 2013 12:52

hi, that's excellent! i almost didn't think it was possible.

it works great, but sometimes when the same customer buys two different items the items.txt file can show: (with an empty line)

Item.txt

Item A
Item B

Amount.txt
2
1
1

with the empty line. i noticed that the code does not multiply the empty line instead showing this:

Item A
Item A
Item B

this doesn't seem to be a problem if there is text wear the empty line should be such as

Empty
Item A
Item B

so would it be possible to replace all empty lines in the items.txt file with a word such as 'Empty' if its easer?

again thank you so much for your help, i should of mentioned the possibility of empty lines in my original message.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: need help with batch file to edit two text files

#5 Post by abc0502 » 19 Feb 2013 13:04

does this empty line a space or just like when you press enter without any input in the line ?

jjlyon
Posts: 18
Joined: 11 Oct 2012 20:43

Re: need help with batch file to edit two text files

#6 Post by jjlyon » 19 Feb 2013 13:15

like when you press enter with no input :)

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: need help with batch file to edit two text files

#7 Post by abc0502 » 19 Feb 2013 13:30

yes, but never mind, i found another way.

Just added extra step, this take the item file and from it create a temprary file with "Empty" in all empty lines then process that file and the total file will have "Empty" word repeated.

I hope that what you need

Code: Select all

@Echo OFF

REM ====[ settings ]==================
SET "Items=%userprofile%\desktop\1.txt"
SET "Amount=%userprofile%\desktop\2.txt"
SET "Total=%userprofile%\desktop\Total.txt"

REM ====[ Replace Empty Lines with Empty Word]=====
For /F %%A In ('Type "%Items%"^|find /v /c ""') Do Set "TotalLines=%%A"
Setlocal EnableDelayedExpansion
<"%Items%" (
   For /L %%A IN ( 1 1 %TotalLines% ) Do (
      SET "Line="
      SET /P "Line="
      IF "!line!" == "" ( Echo Empty
         ) Else ( Echo !Line! )
      )
   )>"Temp.tmp"
Endlocal

REM ====[ Read Two Files and Multiply ]============
(
   For /F "delims=" %%A in ('Type "Temp.tmp"') Do (
      Setlocal EnableDelayedExpansion
         Set /p line=
         rem !line! is the numbers from %Amount%
       CALL :Multiply "%%A" "!line!"
      Endlocal
   )
)<"%Amount%">>"%Total%"

REM ====[ Clean Temporary File ]===================
DEL /F /Q "Temp.tmp" >NUL
Pause
Exit /B

:Multiply
For /L %%A In ( 1 1 %~2 ) Do (
   Echo %~1
   )
Goto :EOF

jjlyon
Posts: 18
Joined: 11 Oct 2012 20:43

Re: need help with batch file to edit two text files

#8 Post by jjlyon » 19 Feb 2013 15:54

hi, that is fantastic! this will helps so much! you have just made my job a whole world easer. thank you so much! :D

jjlyon
Posts: 18
Joined: 11 Oct 2012 20:43

Re: need help with batch file to edit two text files

#9 Post by jjlyon » 04 Mar 2013 17:20

hi, is there anyway to modify the batch file so it does not add spaces at the end of each item in the total.txt list? for example ( _ = space)

Item1_
Item2_
Item2_

to

Item1
Item2
Item2

:) its not a big problem as i have just renamed my files but it would help clean things up a little

kind regards
Josh

Queue
Posts: 31
Joined: 16 Feb 2013 14:31

Re: need help with batch file to edit two text files

#10 Post by Queue » 04 Mar 2013 17:26

See the line:

Code: Select all

         ) Else ( Echo !Line! )

Try changing it to:

Code: Select all

         ) Else ( Echo !Line!)

Notice I removed the space between ! and )

Queue

jjlyon
Posts: 18
Joined: 11 Oct 2012 20:43

Re: need help with batch file to edit two text files

#11 Post by jjlyon » 04 Mar 2013 18:47

ahhh thats great thanks! i rely should learn how to program batch files for myself one day, do you know any good instructional websites i can learn it from step by step? :)


jjlyon
Posts: 18
Joined: 11 Oct 2012 20:43

Re: need help with batch file to edit two text files

#13 Post by jjlyon » 07 Mar 2013 11:36

thanks Squashman this will come in handy, because iv never noticed it before i feel silly now :oops: lol

jjlyon
Posts: 18
Joined: 11 Oct 2012 20:43

Re: need help with batch file to edit two text files

#14 Post by jjlyon » 07 Mar 2013 11:44

hi again, there is a slight problem with the batch file, i hope some one can help. for some reason the batch file doesn't show lines of text exactly as they should appear for example anything coming after the symbols "-" and "&" is removed in the total.txt along with them symbols themselves I.E:

(input text in the Items.txt file

LEARN CAR BODY WORK & REPAIR - DVD
LEARN CAR METAL PREPARATION & RUST REPAIR STEP BY STEP GUIDE - DVD
LEARN HOW TO TIG, GAS & ARC WELD TRAINING ON DVD - 2.5 Hours
30,000+ VECTOR CLIPART IMAGES IN EPS FORMAT FOR BUSINESS & PERSONAL PROJECTS

results in the total.txt file :(

LEARN CAR BODY WORK
LEARN CAR METAL PREPARATION
LEARN HOW TO TIG, GAS
30,000+ VECTOR CLIPART IMAGES IN EPS FORMAT FOR BUSINESS

is there any way to change the batch file so it doesn't delete anything after the symbols - and & and doesn't delete the symbols themselves. so the total.txt file show

LEARN CAR BODY WORK & REPAIR - DVD
LEARN CAR METAL PREPARATION & RUST REPAIR STEP BY STEP GUIDE - DVD
LEARN HOW TO TIG, GAS & ARC WELD TRAINING ON DVD - 2.5 Hours
30,000+ VECTOR CLIPART IMAGES IN EPS FORMAT FOR BUSINESS & PERSONAL PROJECTS


everything else in the batch file is perfect with its ability to multiply the lines of text and empty lines depending on the numbers in the Amount.txt file :)

kind regards
Josh

foxidrive
Expert
Posts: 6033
Joined: 10 Feb 2012 02:20

Re: need help with batch file to edit two text files

#15 Post by foxidrive » 07 Mar 2013 17:50

Instead of this subroutine, use the one at the bottom:

Code: Select all

:Multiply
For /L %%A In ( 1 1 %~2 ) Do (
   Echo %~1
   )


Code: Select all

:Multiply
For /L %%A In ( 1 1 %~2 ) Do (
   for /f "delims=" %%z in ("%~1") do echo %%~z
   )

Post Reply