Solved

Rename Files..

Posted on 2001-07-31
11
230 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
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…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

752 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