I’m currently struggling with a batch script. I’m not sure where the bug is, but here is my code. I want to create two text files whenever the word “VALUES” appears. However, every time I try to make some changes, it still doesn’t work properly.
Below, I am including the code, a few lines of the text file that I want to split, and the expected results.
MY CODE
@echo off
setlocal enabledelayedexpansion
set "input_file=SQL-FILESdbo.Dict.txt"
set "part1_file=SQL-FILESpart1.txt"
set "part2_file=SQL-FILESpart2.txt"
:: Ensure the output files are empty before starting
> %part1_file% echo(
> %part2_file% echo(
for /f "tokens=*" %%A in (%input_file%) do (
set "line=%%A"
for /f "tokens=1,* delims=VALUES" %%B in ("%%A") do (
echo %%B >> %part1_file%
if "%%C" neq "" (
echo VALUES%%C >> %part2_file%
)
)
)
echo File split complete.
endlocal
pause
I tried multiple ways, even asked GPT to help me solve this problem but it still dosen’t work.
MY TXT FILE INCLUDES:
INSERT INTO dbo.Dict (id,DictCatId,Value,TextCode,ShortDesc,LongDesc,Sys,Hide,Color) VALUES (3,2,2,NULL,Data ważności (FEFO),NULL,0,0,NULL);
INSERT INTO dbo.Dict (id,DictCatId,Value,TextCode,ShortDesc,LongDesc,Sys,Hide,Color) VALUES (4,2,3,NULL,Data dostawy (FIFO),NULL,0,0,NULL);
INSERT INTO dbo.Dict (id,DictCatId,Value,TextCode,ShortDesc,LongDesc,Sys,Hide,Color) VALUES (7,6,1,NULL,Возгараемые,NULL,0,0,NULL);
INSERT INTO dbo.Dict (id,DictCatId,Value,TextCode,ShortDesc,LongDesc,Sys,Hide,Color) VALUES (8,4,1,NULL,Kartony TYP A,NULL,0,0,NULL);
INSERT INTO dbo.Dict (id,DictCatId,Value,TextCode,ShortDesc,LongDesc,Sys,Hide,Color) VALUES (9,4,2,NULL,Pojemniki,NULL,0,0,NULL);
INSERT INTO dbo.Dict (id,DictCatId,Value,TextCode,ShortDesc,LongDesc,Sys,Hide,Color) VALUES (11,5,1,NULL,Kartony,NULL,0,0,NULL);
INSERT INTO dbo.Dict (id,DictCatId,Value,TextCode,ShortDesc,LongDesc,Sys,Hide,Color) VALUES (12,5,2,NULL,Pojemniki,NULL,0,0,NULL);
As you can see its just SQL inserts and I want to make 2 txt files as described higher.
part1.txt must inclue only
INSERT INTO dbo.Dict (id,DictCatId,Value,TextCode,ShortDesc,LongDesc,Sys,Hide,Color) VALUES
part2.txt must inclue
(3,2,2,NULL,Data ważności (FEFO),NULL,0,0,NULL);
Paweł Muszyński is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.