Solved

Creating a new folder

Posted on 2003-11-17
10
281 Views
Last Modified: 2010-04-05
I Have an application that creates a file with some information in it. Then it imports the information to it. I want that it Creates a new Folder, and the folder´s name will be the current date. Then cuts the files and copy it to the folder created.
0
Comment
Question by:Fredy992
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 3

Expert Comment

by:JDuncan
ID: 9763269
Look up ForceDirectories(DateToStr(date));

0
 

Author Comment

by:Fredy992
ID: 9763300
I want the routine to: Create a new Folder, and the folder´s name will be the current date. Then cut all of the files from a specific folder and copy it to the folder created.

0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 9763357
as alternative to JDuncon:
  createDirectory('Path\NameHere',nil);


>cut all of the files from a specific folder and copy it to the folder created.

from my paq:

uses shellapi;

procedure TForm1.Button1Click(Sender: TObject);
var I : Integer;
    opInfo : _SHFILEOPSTRUCT;
begin
  opInfo.pFrom := 'd:\TmpFolder';  //SourceFolder
  opInfo.pTo   := 'Path\NameHere';  //DestinationFolder
  opInfo.wFunc := FO_MOVE;  //Move it
  opInfo.fFlags := FOF_NOCONFIRMATION  or //no confirmation
                   FOF_NOCONFIRMMKDIR or
 //no confirmation                  FOF_RENAMEONCOLLISION or //rename if dest exist
                   FOF_SIMPLEPROGRESS;  //show a progress
  opInfo.hNameMappings := nil;  // no mappings
  opInfo.fAnyOperationsAborted := False; //obsolete
  opInfo.lpszProgressTitle := 'Moving . . .';  //foo simpleprogressdisplay
  opInfo.Wnd := self.handle;  //the parent of the progressdisplay
  SHFileOperation(opInfo);  //do it
end;

meikl ;-)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 27

Expert Comment

by:kretzschmar
ID: 9763361
typo
JDuncon -> JDuncan (sorry)
0
 
LVL 8

Assisted Solution

by:gmayo
gmayo earned 62 total points
ID: 9763688
I do a similar thing already. Every time it saves, it stores the old files in a dated directory:
{--------------------------------------------------------------------------------------------------}
function SaveData(basename : string; progressproc : TProgressProcedure) : boolean;
var
      f : TextFile;
      i : integer;
      t : TFile;
      s : string;
begin
      //if not FileExists(basename) then MkDir(basename);
      Result := true;
      i := 0;
      if Assigned(progressproc) then progressproc(0.0);

      s := basename + '\' + FormatDateTime('yyyy-mm-dd hh-nn-ss', Now);
      MkDir(s);

      while (Result = true) and (i < FileList.Count) do begin
            t := TFile(FileList.Objects[i]);

            if t.Save then begin
                  if t.ReadOnly then begin
        CopyFile(PChar(MakeFilename(basename, t.Name)), PChar(s + '\' + basename + '.' + t.Name), false)
                  end
                  else
                  begin
        RenameFile(MakeFilename(basename, t.Name), s + '\' + basename + '.' + t.Name);
        try
          AssignFile(f, MakeFilename(basename, t.Name));
          Rewrite(f);
          Result := t.SaveToFile(f);
        except
          Result := false;
        end;

        CloseFile(f);
                  end;
      if Assigned(progressproc) then progressproc((i + 1) / FileList.Count);

            end;
            Inc(i);
      end;
      if Assigned(progressproc) then progressproc(1.0);

end;
{--------------------------------------------------------------------------------------------------}

If there is too much in there, let me know and I will cut it down to what you need.

Geoff M.
0
 
LVL 11

Accepted Solution

by:
calinutz earned 63 total points
ID: 9763902
To create de folder is a very simple task:

var
 d,m,y:word;
 newdir:string;
begin
decodedate(now,y,m,d);
newdir:=IntToStr(d)+'_'+IntToStr(m)+'_'+IntToStr(y);
CreateDir('C:\'+newdir);
...

then to move the files from your temporary dir you should make a procedure. There is no direct way... like

Copy c:\temp\*.* to c:\NewDir\

But you can make a preocedure that does that very easily. If you want I can make you one and send it to your email...
Cheers
0
 

Author Comment

by:Fredy992
ID: 9765430
I made the following: And it worked. Does anyone see something wrong in it ?

procedure TfrmForm1.Button1Click(Sender: TObject);
var
  d,m,y:word;
  newdir:string;
  SR: TSearchRec;
  I: integer;
  Source, Desti: string;
begin
  decodedate(now,y,m,d);
  newdir:=IntToStr(d)+'_'+IntToStr(m)+'_'+IntToStr(y);
  CreateDir('C:\'+newdir);

 I := FindFirst('c:\*.*', faAnyFile, SR);
  while I = 0 do begin
    if (SR.Attr and faDirectory) <> faDirectory then
    begin
      Origem := 'c:\' + SR.Name;
      Destino := 'c:\'+newdir+'\' + SR.Name;
      try
      CopyFile(PChar(Source), PChar(Desti), False); // ---< Copia >---
      DeleteFile('c:\' + SR.Name); // ---< Exclui >---
      except
      end;
    end;
    I := FindNext(SR);
  end;
end;

It copys then deletes right after
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 9765494
in this case
calinutz should get the points
(because the beginning is identical)

meikl ;-)
0
 
LVL 8

Expert Comment

by:gmayo
ID: 9765957
Should he? Or should it really be up to the author who gets the points? I would suggest the latter myself, bearing in mind my solution was first and did what he wanted! ;-)

Geoff M.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 9766192
oh yes, partial, maybe splitting points between you both
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question