Solved

Creating a new folder

Posted on 2003-11-17
10
277 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
Comment Utility
Look up ForceDirectories(DateToStr(date));

0
 

Author Comment

by:Fredy992
Comment Utility
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
Comment Utility
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
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
typo
JDuncon -> JDuncan (sorry)
0
 
LVL 8

Assisted Solution

by:gmayo
gmayo earned 62 total points
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 11

Accepted Solution

by:
calinutz earned 63 total points
Comment Utility
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
Comment Utility
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
Comment Utility
in this case
calinutz should get the points
(because the beginning is identical)

meikl ;-)
0
 
LVL 8

Expert Comment

by:gmayo
Comment Utility
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
Comment Utility
oh yes, partial, maybe splitting points between you both
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now