Solved

Rename Files..

Posted on 2001-07-31
11
227 Views
Last Modified: 2010-04-06
Hi,
i wanted to (rename&copy) all files in derectory named
c:\1\ which are all .gif files, to serial names in
c:\2\ .

i mean....
rename c:\1\abc.gif to c:\2\0.gif
rename c:\1\abc4efq.gif to c:\2\1.gif
rename c:\1\abctw4552.gif to c:\2\2.gif


Any fast solution ??
i need to rename 4981 files !!
help pleasd !!
0
Comment
Question by:neostudio
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 6

Expert Comment

by:edey
ID: 6337958
something like

var
 sr : TSearchRec;
 result : integer;
 fileNum : integer;
begin
 fileNum := 0;
 result := findFirst('c:\1\*.gif',faAnyFile,sr);
 while result = 0 do
 begin
  moveFile(pchar('c:\1\'+sr.name),pchar('c:\2\'+intToStr(fileNum)+'.gif');
  result := findNext(sr);
 end;
 findClose(sr);
end;

Note, this is untested "on-the-fly" code, but it _shold_ work

GL
Mike
0
 
LVL 1

Expert Comment

by:h_mohsenian
ID: 6338066
it is good comment just you forget to

inc(filenum);

also i think  :
copyFile(pchar('c:\1\'+sr.name),pchar('c:\2\'+intToStr(fileNum)+'.gif'),true);

is better becouse he wants to copy not move.

best regards
hamed
0
 
LVL 6

Expert Comment

by:edey
ID: 6338377
Ah, yes, good point(s) hamed.  I can't belive I forgot to increment the fileNumber!

GL
Mike
0
 
LVL 1

Expert Comment

by:h_mohsenian
ID: 6339276
hi edey, neostudio must be carefull for using movefile becouse if he use it instead of copyfile he will not find the original names of the main .gif files in c:\1\ folder. are you agree with me?

best regards
hamed
0
 
LVL 3

Author Comment

by:neostudio
ID: 6340181
Hi guys...

1- please decide who gets the points .
2- small little help is still nededed,
neither copyFile(... or MoveFile(... worker !!
any ideas ??


0
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.

 

Accepted Solution

by:
SMakarenko earned 60 total points
ID: 6340524
try uses next example:




uses ShellApi;

procedure TForm2.Button1Click(Sender: TObject);
var
  FileOp: TSHFileOpStruct;
  DstPos: Integer;
  SrcPos: Integer;
  err, i: Integer;
  s:string;
  ResultStr,ResultStr2: String;
  SearchRec: TSearchRec;
begin
  i:=0;
  if FindFirst('c:\1\*.gif',faAnyFile, SearchRec) = 0 then
  begin
    inc(i);
    ResultStr := 'c:\1\'+SearchRec.Name;
    ResultStr2:= 'c:\2\'+IntToStr(i)+'.gif';
    while FindNext(SearchRec) = 0 do
    begin
      inc(i);
      s := 'c:\1\'+SearchRec.Name;
      ResultStr := Concat(ResultStr, #0, s);
      s := 'c:\2\'+IntToStr(i)+'.gif';
      ResultStr2 := Concat(ResultStr2, #0, s);
    end;
    FindClose(SearchRec);
  end;


  ResultStr := Concat(ResultStr, #0);
  ResultStr2 := Concat(ResultStr2,#0);
  FileOp.Wnd := Application.MainForm.Handle;
  FileOp.wFunc := FO_COPY;
  FileOp.pFrom := PChar(ResultStr);
  FileOp.pTo := PChar(ResultStr2);
  FileOp.fFlags := FOF_NOCONFIRMATION + FOF_NOCONFIRMMKDIR  + FOF_MULTIDESTFILES;
  FileOp.lpszProgressTitle := 'Copy files';
  if SHFileOperation(FileOp) <> 0 then RaiseLastWin32Error;
end;
0
 
LVL 6

Expert Comment

by:edey
ID: 6340530
What happens when you call copy/moveFile?  

GL
Mike
0
 
LVL 1

Expert Comment

by:h_mohsenian
ID: 6341128
hi edey and neostudio,

there is a grate diference between moveFile and copyFile :

if you call CopyFile all of the file were copied to another directory and give their new name but the original files are stil in the c:\1\ directory without eny change it mean you have numbered file in c:\2\ and stile original files in c:\1\

now if you use movefile all of original files are copied to c:\2\ and there was not eny gif file in c:\1\ folder and you never could understand for example what is the original name of 354.gif ( for example it was hamed.gif at start )

now if original files ( in c:\1\ ) folder is importand for you u must use copyfile but if not you can use moveFile

more clear : copyfile is like <Copy and paste then rename> but movefile is like <Cut and Paste then rename>

best regards
hamed
0
 
LVL 3

Author Comment

by:neostudio
ID: 6342613
Hi...

edey,
The code you posted said copying but did nothing at all, after the copy procedure i made the caption reflect the sr.name to see the proicess if it was going, and it was going from 1 to 503 with no problems, but nothing copied in realtime .
Sorry.

the code posted by SMakarenko worked 100%
your points .



h_mohsenian
O??Co
Thank You
0
 
LVL 1

Expert Comment

by:h_mohsenian
ID: 6345012
sorry , what is the meaning of 'O??Co' ;-)
0
 
LVL 3

Author Comment

by:neostudio
ID: 6345062
;-)
it was SHUKRAN in arabic
but seems like the string was encoded.....

bye
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
Windows Drag & Drop Location 2 94
Delphi 2010 Export to pdf 2 287
FMX StringGrid1->Canvas->FillRect Problem 3 127
Tembedded WB animatid gifs not animated on some pcs 2 72
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

911 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

21 Experts available now in Live!

Get 1:1 Help Now