Need help to move, and rename files

on
Example:

I have files in the following directory:

c:\from\
abc.001
abc.002
abc.003

I want to move these files to the following directory and rename them using the following scheme:

c:\to\
zyx1.fil
zyx2.fil
zyx3.fil

Does anyone have example code showing how I can do this?

Thanks, ScarletRose
Comment
Watch Question

Do more with

EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
I use Visual Basic 6.0

Commented:
strFileName = dir$("C:\From") while strFileName<>vbNullstring strFileExt =mid$(strFileName, instrrev(strFileName, ".")+1)
strNewFilename = "xyz" & val(strFileExt) & ".fil"
filecopy strFileName , "C:\To\" & strNewFilename
wend
Commented:
Or if you want the numbers in sequence rather than the original extension:

strFileName = dir$("C:\From") intCntr = 1 while strFileName<>vbNullstring strNewFilename = "xyz" & val(intCntr) & ".fil" filecopy strFileName , "C:\To\" & strNewFilename intCntr =intCntr +1 wend Commented: c:\from\ abc.001 abc.002 abc.003 I want to move these files to the following directory and rename them using the following scheme: c:\to\ zyx1.fil zyx2.fil zyx3.fil sPath = "c:\from\" dPath = "c:\to\" fname = dir$(sPath & "*.*")
do while fname > ""

Commented:
Sorry ignore the last (finger trouble)

sPath = "c:\from\"
dPath = "c:\to\"

fname = dir$(sPath & "*.*") do while fname > "" dName = "xyz" & cstr(cdbl(right(fname,3))) & ".fil" Name sPath & fname as dPath & dName ' Name "c:\from\abc.001" as "c:\to\xyz1.fil" fname = dir loop Using the 'Name' function with a different path will move the file Commented: Hi ScarletRose If you don't mind using the Windows API, then there is a really simple way of copying and renaming files in one go. Declare Function CopyFile Lib "kernel32.dll" Alias "CopyFileA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal bFailIfExists As Long) As Long To use the API is very simple: ' Copy the file C:\source\abc.001 to C:\dest\zyx1.fil Dim retval As Long ' return value ' copy the file retval = CopyFile("C:\source\abc.001", "C:\dest\zyx1.fil", 0) The third parameter, 0, allows the file zyx1.fil to be overwritten if it exists. If you change this to "1", then the copy will fail if the file exists. Not fatally, retval will return 0 to indicate that the file could not be copied. Hope this helps EB Commented: Sorry ScarletRose, you did say "move" You could copy and delete but here is the move function: Declare Function MoveFile Lib "kernel32.dll" Alias "MoveFileA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String) As Long Platforms: Win 32s, Win 95/98, Win NT MoveFile moves or renames a file or directory -- it's really the same operation either way. If a directory is moved/renamed, all of the subdirectories and files contained in it will similarly be moved/renamed to reflect the path change. The function returns 1 if successful, or 0 if an error occured. lpExistingFileName The source file or directory; i.e., the file or directory to rename (move). lpNewFileName The target file or directory; i.e., the new file or directory name to give the source file (where to move the file or directory). Example: ' Move a file from C:\source\abc.001 to C:\dest\zyx1.fil ' The original file will no longer exist. This example changes the filename and moves the file into a different directory simultaneously. Dim retval As Long ' return value retval = MoveFile("C:\source\abc.001", "C:\dest\zyx1.fil") EB Commented: Thank you to everyone for your suggestions on how to solve my file move and rename problem. I am going through each suggestion and trying them in my program. I have a question for rsphaitz... for some reason I can not get the counter to increment past 1. I am currently trying your second suggestion. Do I need to put the code in a module or make a reference in the program?? Not sure what I may be doing wrong.. Thanks, ScarletRose Commented: My version had a few minor problems, corrected below with comments. I forgot to define the starting profile (*.*) when getting the original list, the copy needed the full path name of the "from" file, and the loop needed to get the next file. Now it's all corrected and tested: Dim strFileName As String Dim intCntr As Integer Dim strNewFilename As String ' Define starting location and fileset (*.*=all) strFileName = Dir$("C:\temp\From\*.*")

intCntr = 1

' Search until no more files are found
While strFileName <> vbNullString
' Define the name that the file will be copied to (based on the count)
strNewFilename = "xyz" & Val(intCntr) & ".fil"

' Copy the file
FileCopy "C:\temp\From\" & strFileName, "C:\temp\To\" & strNewFilename

' Delete the old if desired
'Kill "C:\temp\From\" & strFileName

' Get the next file in the original directory
strFileName = Dir\$

intCntr = intCntr + 1
Wend

Commented:
Hi ScarletRose

Just cut and paste this code into your form
It can be called by clicking a button or simply calling the sub outright. You may insert extra code to handle the result that retval <> 1

I am using your filename and path examples.
-EB

Declare Function MoveFile Lib "kernel32.dll" _
Alias "MoveFileA" (ByVal lpExistingFileName As String, _
ByVal lpNewFileName As String) As Long

sub MoveAll_Click()
Dim retval As Long

retval = MoveFile("C:\from\abc.001", "C:\to\zyx1.fil")
retval = MoveFile("C:\from\abc.002", "C:\to\zyx2.fil")
retval = MoveFile("C:\from\abc.003", "C:\to\zyx3.fil")

end sub

Commented:
Hi ScarletRose

If you get a compile error with that, make the declaration private:

Private Declare Function MoveFile Lib "kernel32.dll" _
Alias "MoveFileA" (ByVal lpExistingFileName As String, _
ByVal lpNewFileName As String) As Long

OR put it in a module

Commented:
Hi scarletRose,

You could use fileScripting Object with this one.

Sub Move()
Dim fso As Scripting.FileSystemObject
Dim tofld As Scripting.Folder
Dim fromfld As Scripting.Folder
Dim fil As Scripting.File
Dim strnewfilename As String

Set fso = New Scripting.FileSystemObject
Set tofld = fso.GetFolder("C:\to")
Set fromfld = fso.GetFolder("C:\from")

For Each fil In fromfld.Files
strnewfilename = "xyz" & CStr(CInt(Right(fil.Name, 3))) & ".fil"
fil.Move (tofld.Path & "/" & strnewfilename)

Next
Set fromfld = Nothing
Set tofld = Nothing
Set fso = Nothing

End Sub

this code works for all the file in the directory.  but if you have specification free feel to ask.

Commented:
Thanks to everyone for your suggestions and help...
I hope to close this asap....

I have a question for manthanein  or anyone who may know..

Is it possible to use the same code you have given me that adds a number to the end of the renamed file... to add todays date and a letter at the end of the file if needed?

Example names...

move c:\to\zyx1.fil  c:\last\20021210a.fil
c:\to\zyx2.fil  c:\last\20021210b.fil
c:\to\zyx3.fil  c:\last\20021210c.fil
c:\to\zyx4.fil  c:\last\20021210d.fil

Commented:
yes  it is posibble you should add a variable to hold the value for example

strDate = format(now,"yyyymmdd")
strnewfilename = "xyz" & CStr(CInt(Right(fil.Name, 3))) & strdate & ".fil"
Author of the Year 2009

Commented:
Hi ScarletRose,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

Accept rspahitz's comment(s) as an answer.

ScarletRose, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept THIS comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer

Do more with