Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

name

Status
Not open for further replies.

patweb

Programmer
Apr 17, 2003
174
BE
I can't see why, but renaming files with name is not working when using variables ?

1. He recognise the files, because he does an import.
2. While testing and hard coding the names of the directory he's perform the renaming. See below in remarks.

Dim targetfilename As String
Dim lengte As Integer
strFileName = CStr(x)
MsgBox "strFilename :" & strFileName

lengte = Len(strFileName) - 3
MsgBox "lengte :" & lengte
targetfilename = Left(strFileName, lengte) & "txt"
MsgBox "targetfilename : " & targetfilename

'tested and ok : strFileName = "C:\Import\00017169.cmd"
'tested and ok : targetfilename = "C:\Import\00017169.txt"

Name strFileName As targetfilename 'renaming the files
strFileName = ""
targetfilename = ""

next i
 
Try using Filecopy and then delete the old file, something like this:

FileCopy strFilename, TargetFileName
Kill strFilename

Let me know if this helps.

Regards,
gkprogrammer
 
GKProgrammer's sudjestion is the best idea, but the reason you are having trouble is the line bellow

targetfilename = Left(strFileName, lengte) & "txt"


lengte is an int and you are trying to trow it into a string without coverting it. you need to use something like this

targetfilename = Left(strFileName, CStr(lengte)) & "txt"
 
This is the correct way to use the 'Left' function.
targetfilename = Left(strFileName, lengte) & "txt"


'The last suggestion has an incorrect use of the 'Left' function.
targetfilename = Left(strFileName, CStr(lengte)) & "txt"

Left(string, length)

The Left function syntax has these named arguments:

Part Description
string Required. String expression from which the leftmost characters are returned. If string contains Null, Null is returned.
length Required; Variant (Long). Numeric expression indicating how many characters to return. If 0, a zero-length string ("") is returned. If greater than or equal to the number of characters in string, the entire string is returned.
 
He still has problem to recognise the file. Thanks for the suggestions. The name of the Sourcefile is coming from an Application.FileSearch. Perhaps this could be a reason.

With Application.FileSearch
.FileName = strExtension

The solution (perhaps not the best) I found for this question is the following. The code insert the Sourcefile (Path & Filename) in a table. Afterwards I get these names to "name Sourcefile as TargetFile". This works.

Pat.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top