Solved

Rename Files..

Posted on 2001-07-31
11
225 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 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

10 Experts available now in Live!

Get 1:1 Help Now