Solved

Rename multiple folders

Posted on 2006-06-11
4
286 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Run video youtube webbrowse 10 57
Magic Software info 18 124
LAN or WAN ? 11 81
Multiple image collision 13 69
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
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 …

932 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

8 Experts available now in Live!

Get 1:1 Help Now