Solved

Rename multiple folders

Posted on 2006-06-11
4
287 Views
Last Modified: 2010-04-05
hi experts,
OK I have another problem. I have for example a combobox with 3 items. For each item I have created a different folder and several files. Folders are renamed as itemindex+1 (ex folder name: 1,2 ..3). In my project I load every time  the combobox items from a file and the data from the folders are informations about every combobox item. But if I want to delete for example the second item I delete it's files too and it's folder and when I'm trying to start my project again.. and error display the warning that the file I'm attempting to load from is unreacheble. That's true. Now how can I rename the folder 3 when the second is deleled??
It's not just renaming a folder... I need to rename al folder found after the deleted folder.

Hope you understood something. Thanks
0
Comment
Question by:A. Cristian Csiki
  • 2
4 Comments
 
LVL 9

Expert Comment

by:sun4sunday
ID: 16881751
To rename a folder use RenameFile

RenameFile('C:\Old', 'C:\New');


sun4sunday
0
 
LVL 11

Author Comment

by:A. Cristian Csiki
ID: 16894625
well let's see.
I've got 50 folders. In each got 7 different files. Lets suppose my user want to delete an combobox item (exemple: 25). The directory 25 is deleted. Now, how can I read all the folders and when i detect the mising of 25 rename the rest ..like this (26 becomes 25, 27 becomes 26 and so on)?
Can I do that?
Thanks
0
 
LVL 14

Accepted Solution

by:
Pierre Cornelius earned 500 total points
ID: 16898644
Here's a demo.

PAS File:
====================================================
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    inpBaseDir: TEdit;
    Button1: TButton;
    Label2: TLabel;
    inpSubDirs: TEdit;
    GroupBox1: TGroupBox;
    lstDirs: TListBox;
    btnRefresh: TButton;
    btnRename: TButton;
    btnDelete: TButton;
    procedure inpSubDirsKeyPress(Sender: TObject; var Key: Char);
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure btnRefreshClick(Sender: TObject);
    procedure btnDeleteClick(Sender: TObject);
    procedure btnRenameClick(Sender: TObject);
  private
    procedure SortDirs;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.inpSubDirsKeyPress(Sender: TObject; var Key: Char);
begin
  if NOT (Key in ['0'..'9',#8]) then
  begin
    key:= #0;
    beep;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var i: integer;
    s: string;
begin
  for i:= 0 to StrToInt(inpSubDirs.text)-1 do
  begin
    s:= inpBaseDir.Text;
    if s[Length(s)] <> '\' then s:= s + '\';
    s:= s + IntToStr(i+1);
    if not DirectoryExists(s)
      then CreateDir(s);
  end;
  ShowMessage('Done');
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  inpBaseDir.Text:= ExtractFilePath(ParamStr(0));
end;

procedure TForm1.btnRefreshClick(Sender: TObject);
var sr: TSearchRec;
    s: string;
begin
  lstDirs.Clear;
  s:= inpBaseDir.Text;
  if (s[Length(s)] <> '\') then s:= s+'\';
  if FindFirst(s+'*.', faDirectory, sr) = 0 then
  try
    repeat
      if ((sr.Attr AND faDirectory) = sr.Attr)
        AND (sr.Name <> '.') AND (sr.Name <> '..')
        then lstDirs.Items.Add(s+sr.FindData.cFileName);
    until FindNext(sr) <> 0;
  finally
    FindClose(sr);
  end;
  SortDirs;
end;

procedure TForm1.btnDeleteClick(Sender: TObject);
var sr: TSearchRec;
    path: string;
begin
  if lstDirs.ItemIndex < 0 then
   raise Exception.Create('Select a directory to delete first');

  path:= lstDirs.Items[lstDirs.ItemIndex];
  if MessageDlg('Are you sure you want to delete '+
                'the following directory and its files?'#13#13+
                path,
                mtWarning, [mbOK, mbCancel],0)
    <> mrOK
    then Abort;

  if FindFirst(path+'\*.*', 0, sr) = 0 then
  try
    repeat
      if not DeleteFile(path+'\'+sr.Name)
        then RaiseLastWin32Error;
    until FindNext(sr) <> 0;
  finally
    FindClose(sr);
  end;

  if not RemoveDir(lstDirs.Items[lstDirs.ItemIndex])
    then RaiseLastWin32Error;

  btnRefreshClick(self);
end;

function ExtractLastDir(path: string): string;
var i, j: integer;
    s: string;
begin
  i:= -1;
  s:= path;
  for i:= 0 to length(s) do //find last "\"
    if s[i] = '\' then j:= i;
  if i = -1 then raise exception.Create('Error: dir must contain numbers only');
  result:= copy(s, j+1, length(s)-j);
end;

function SortByNum(List: TStringList; Index1, Index2: Integer): Integer;
var s: string;
    i1,i2: integer;
begin
  s:= ExtractLastDir(List[Index1]);
  i1:= StrToInt(s);

  s:= ExtractLastDir(List[Index2]);
  i2:= StrToInt(s);

  result:= i1 - i2;
end;

procedure TForm1.SortDirs;
var sl: TStringList;
begin
  sl:= TStringList.Create;
  try
    sl.Assign(lstDirs.Items);
    sl.CustomSort(SortByNum);
    lstDirs.Items.Assign(sl);
  finally
    sl.free;
  end;
end;

procedure TForm1.btnRenameClick(Sender: TObject);
var i,j: integer;
    s, s2: string;
begin
  SortDirs;
  for i:= 0 to lstDirs.Count-1 do
  begin
    s:= ExtractLastDir(lstDirs.Items[i]);
    j:= StrToInt(s);
    s2:= lstDirs.Items[i];
    SetLength(s2, Length(lstDirs.Items[i])-Length(s));
    s2:= s2+ IntToStr(i+1);
    if j <> (i+1)
      then if not RenameFile(lstDirs.Items[i], s2)
        then RaiseLastWin32Error;
  end;
  btnRefreshClick(self);
end;

end.


DFM File:
===========================================================
object Form1: TForm1
  Left = 192
  Top = 114
  Width = 696
  Height = 480
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 8
    Top = 16
    Width = 40
    Height = 13
    Caption = 'Base Dir'
  end
  object Label2: TLabel
    Left = 56
    Top = 48
    Width = 90
    Height = 13
    Caption = 'Number of sub Dirs'
  end
  object inpBaseDir: TEdit
    Left = 56
    Top = 16
    Width = 617
    Height = 21
    TabOrder = 0
  end
  object Button1: TButton
    Left = 240
    Top = 48
    Width = 89
    Height = 25
    Caption = 'Create Sub Dirs'
    TabOrder = 1
    OnClick = Button1Click
  end
  object inpSubDirs: TEdit
    Left = 160
    Top = 48
    Width = 65
    Height = 21
    TabOrder = 2
    Text = '10'
    OnKeyPress = inpSubDirsKeyPress
  end
  object GroupBox1: TGroupBox
    Left = 16
    Top = 88
    Width = 321
    Height = 201
    Caption = 'Sub Dirs'
    TabOrder = 3
    DesignSize = (
      321
      201)
    object lstDirs: TListBox
      Left = 8
      Top = 24
      Width = 297
      Height = 137
      Anchors = [akLeft, akTop, akRight, akBottom]
      ItemHeight = 13
      TabOrder = 0
    end
    object btnRefresh: TButton
      Left = 8
      Top = 168
      Width = 75
      Height = 25
      Caption = 'Refresh'
      TabOrder = 1
      OnClick = btnRefreshClick
    end
    object btnRename: TButton
      Left = 88
      Top = 168
      Width = 75
      Height = 25
      Caption = 'Rename'
      TabOrder = 2
      OnClick = btnRenameClick
    end
    object btnDelete: TButton
      Left = 168
      Top = 168
      Width = 75
      Height = 25
      Caption = 'Delete'
      TabOrder = 3
      OnClick = btnDeleteClick
    end
  end
end


Kind Regards
Pierre
0
 
LVL 11

Author Comment

by:A. Cristian Csiki
ID: 16929439
I don't know why I didn't got a reply mail when you posted the comment. Only today I saw your add. I'll give it a try in few days. Thanks a lot.
Nodramas.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

772 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