|
Answer» I need to automate a text file to split on a certain line into two new files multiple times a day.
Example:
exampletext.txt line 1 line 2 line 3 line 4 line 5
exampletext1.txt line 1 line 2 line 3
exampletext2.txt line 4 line 5
I'm not sure what the best method to do this is, whether to use a batch file or if there is a program already out there.
Off topic: I APOLOGIZE if this is in the wrong location, or has been posted before. I searched and couldn't find anything.Welcome to the CH forums.
I'm not sure if the following code is the 'best' method, no doubt one of the scripting gurus will do better..
Code: [Select]@echo off setlocal enabledelayedexpansion cls set a=1
if exist exampletext1.txt del exampletext1.txt if exist exampletext2.txt del exampletext2.txt
for /f "delims=*" %%A in (exampletext.txt) do ( echo %%A >> exampletext1.txt set /a a=!a!+1 if !a! equ 4 GOTO second )
:second for /f "delims=* skip=3" %%A in (exampletext.txt) do ( echo %%A >> exampletext2.txt )
echo Exampletext1.txt = type exampletext1.txt echo. echo Exampletest2.txt = type exampletext2.txt
Good luck Thanks for the reply, is that the code i should use for a batch file?Yes, but you have to change the paths\filenames to the actual ones you use and, if you wish, remove the final few lines which display the output file contents.
Ok thanks, i don't really need it to display anything, I just need the files converted so another program can read them.Also where in there can i specify the line break.I thought you wanted to always split the file after line 3. Below is another version which will allow you to specify how MANY lines are to be copied into the first file.
Save the script somewhere in your path as filename.bat and when you invoke it specify how many lines are to be copied to the first file. e.g. entering "filename 6" (without the "") will split the INPUT file after line 6. Note that if the entered value is not a valid number greater than 0 (zero) then all lines will be copied to the first output file.
Code: [Select]@echo off setlocal enabledelayedexpansion cls
set a=0 set skip=%1
if exist exampletext1.txt del exampletext1.txt if exist exampletext2.txt del exampletext2.txt
for /f "delims=*" %%A in (exampletext.txt) do ( echo %%A >> exampletext1.txt set /a a+=1 if !a! equ !skip! goto second )
:second for /f "delims=* skip=%skip%" %%A in (exampletext.txt) do ( echo %%A >> exampletext2.txt
) set skip=
echo Exampletext1.txt = type exampletext1.txt echo. echo Exampletest2.txt = type exampletext2.txt
I gave it a shot and it isn't working for me. More than likely I'm doing something wrong. I have all these saved in the directory C:\batchtest
programschedule.bat Code: [Select]@echo off setlocal enabledelayedexpansion cls
set a=0 set skip=%1
if exist C:\batchtest\ps1.txt if exist C:\batchtest\ps2.txt
for /f "delims=*" %%A in (C:\batchtest\ps.txt) do ( echo %%A >> C:\batchtest\ps1.txt set /a a+=1 if !a! equ !skip! goto second )
:second for /f "delims=* skip=%skip%" %%A in (C:\batchtest\ps.txt) do ( echo %%A >> C:\batchtest\ps2.txt
) set skip=
echo C:\batchtest\ps1.txt = type C:\batchtest\ps1.txt echo. echo C:\batchtest\ps2.txt = type C:\batchtest\ps2.txt initiate.bat Code: [Select]call C:\batchtest\programschedule 14This is the file that i will use to start programschedule.bat and specify the line to cut. Ideally I will have this set to run every 30 minutes.
ps.txt Quote Thursday
12:00 AM Test 8:00 AM Test 8:30 AM Test 9:00 AM Test 9:30 AM Test 10:00 AM Test 10:30 AM Test 11:00 AM Test 11:30 AM Test 12:00 AM Test 12:30 PM Test 1:00 PM Test 2:00 PM Test 2:30 PM Test 3:00 PM Test 3:30 PM Test 4:00 PM Test 5:00 PM Test 6:00 PM Test 6:30 PM Test 7:00 PM Test 10:00 PM Test 11:00 PM Test This is the initial file to be split and the next two should be the end result.
ps1.txt QuoteThursday
12:00 AM Test 8:00 AM Test 8:30 AM Test 9:00 AM Test 9:30 AM Test 10:00 AM Test 10:30 AM Test 11:00 AM Test 11:30 AM Test 12:00 AM Test 12:30 PM Test 1:00 PM Test ps2.txt QuoteThursday
2:00 PM Test 2:30 PM Test 3:00 PM Test 3:30 PM Test 4:00 PM Test 5:00 PM Test 6:00 PM Test 6:30 PM Test 7:00 PM Test 10:00 PM Test 11:00 PM Test I found my problem, i somehow managed to lose the del ps1.txt and ps2.txt after the if commands. Although things still aren't doing exactly what i want them to do. My current end results are
ps1.txt QuoteThursday 12:00 AM Test 8:00 AM Test 8:30 AM Test 9:00 AM Test 9:30 AM Test 10:00 AM Test 10:30 AM Test 11:00 AM Test 11:30 AM Test 12:00 AM Test 12:30 PM Test 1:00 PM Test 2:00 PM Test ps2.txt Quote 2:00 PM Test 2:30 PM Test 3:00 PM Test 3:30 PM Test 4:00 PM Test 5:00 PM Test 6:00 PM Test 6:30 PM Test 7:00 PM Test 10:00 PM Test 11:00 PM Test
and i need them to be
ps1.txt QuoteThursday
12:00 AM Test 8:00 AM Test 8:30 AM Test 9:00 AM Test 9:30 AM Test 10:00 AM Test 10:30 AM Test 11:00 AM Test 11:30 AM Test 12:00 AM Test 12:30 PM Test 1:00 PM Test 2:00 PM Test ps2.txt QuoteThursday
2:00 PM Test 2:30 PM Test 3:00 PM Test 3:30 PM Test 4:00 PM Test 5:00 PM Test 6:00 PM Test 6:30 PM Test 7:00 PM Test 10:00 PM Test 11:00 PM Test
Basically I need the Thursday and line break at the top of both files.
I have taken some c++ classes so I understand this a little bit but not to the point to where I can make this happen like you guys. Again i appreciate all the help.Maybe we've got the specs right this time, but note that your required output shows the 2.00 PM Test in both output files, is this intentional?
See the attachment...
Code: [Select]@echo off setlocal enabledelayedexpansion cls
set a=0 set skip=%1
if exist ps1.txt del ps1.txt if exist ps2.txt del ps2.txt
for /f "delims=*" %%A in (ps.txt) do ( set day=%%A & goto start )
:start
echo %day%>ps1.txt echo.>>ps1.txt for /f "skip=1 delims=*" %%B in (ps.txt) do ( echo %%B >> ps1.txt set /a a+=1 if !a! equ !skip! goto second )
:second set/a skip +=2 echo %day%>ps2.txt echo.>>ps2.txt for /f "delims=* skip=%skip%" %%C in (ps.txt) do ( echo %%C >> ps2.txt
) set/a skip -=2 echo. echo Lines output to PS1.TXT = %skip% echo.&echo.
echo Output file PS1.TXT == echo. type ps1.txt echo.&echo. echo Output file PS2.TXT == echo. type ps2.txt
[attachment deleted by admin]No it was not intentional but it doesn't hurt to be there.
|