This code breaks the text into parts and saves it in one folder.

How to make each file be saved in a separate folder when broken?

procedure TForm2.Button1Click(Sender: TObject); var n,k,j,p,q:integer; t,t1:TStringList; F1, F2 : File; i, SizePart, SizePartAdd : Cardinal; Buff : array of Byte; begin n:=strtoint(Edit1.Text);//количество разбиений t:=TStringList.Create; if OpenDialog1.InitialDir = '' then begin OpenDialog1.InitialDir := ExtractFilePath(Application.ExeName); end; if not OpenDialog1.Execute then Exit; if not FileExists(OpenDialog1.FileName) then begin ShowMessage('Указанный файл не найден. Действие отменено.'); Exit; end; AssignFile(F1, OpenDialog1.FileName); Reset(F1, 1); if FileSize(F1) < N then begin ShowMessage('Указанный файл слишком мал. Разбиение отменено.'); CloseFile(F1); Exit; end; SizePart := FileSize(F1) div N; SizePartAdd := FileSize(F1) mod N; SetLength(Buff, SizePart); for i := 1 to N do begin AssignFile(F2, OpenDialog1.FileName + '.part' + IntToStr(i)); Rewrite(F2, 1); BlockRead(F1, Pointer(Buff)^, SizePart); BlockWrite(F2, Pointer(Buff)^, SizePart); if (i = N) and (SizePartAdd > 0) then begin BlockRead(F1, Pointer(Buff)^, SizePartAdd); BlockWrite(F2, Pointer(Buff)^, SizePartAdd); end; CloseFile(F2); end; CloseFile(F1); end; 

    1 answer 1

    How to create a path for a file depends on your requirements. You can, for example, like this:

     somePath := ExtractFilePath(OpenDialog1.FileName) + 'part' + IntToStr(i) + PathDelim + ExtractFileName(OpenDialog1.FileName); 

    and further:

      ForceDirectories(ExtractFilePath(somePath)); // Убедиться, что папка создана AssignFile(F2, somePath); // или другой способ сохранения файла 
    • Little did not understand how to do it. Can you give an example of my code? You are welcome ? - Tatiana
    • @ Tatyana Edit the question, write what path you have and what should be, with examples. - Kromster 1:01 pm
    • The path I open through opendialog. That is, in the folder where the file is opened there and the file folders should be created. - Tatiana