Help! - trying to use Name to rename files

C

chantal.lyn

I need to import an unspecified number of text files (converted from
Word to trap the formfield contents) into Access, however I need to
rename a number of the filenames to strip "."

I'm using code I found elsewhere on here, however whenever the loop
tries to rename a file, I get error message 53 'File not found'. The
file is very much there, and checking the variable contents in the
immediate window produces the right results.

Can anyone suggest why this **** code isn't working? Thanks!

-----------------------------------------------
Sub RemovePunctuation()
'On Error Resume Next

Dim strFolder As String
Dim strFileName As String
Dim strNewName As String


strFolder = "C:\Documents and Settings\CGUEVARA\My
Documents\doc\3ps\converted"


strFileName = Dir(strFolder & "\*.txt")


Do Until Len(strFileName) = 0


' Only rename the ones with embedded periods.
If strFileName Like "*.*.*" Then


' strip off the ".txt" to get new name
strNewName = Left(strFileName, Len(strFileName) - 4)


' remove any embedded periods (.)
strNewName = Replace(strNewName, ".", "")
strNewName = Replace(strNewName, ",", "")
strNewName = Replace(strNewName, " ", "")



' Rename the file.
Name strFileName As strNewName & ".txt"


'End If


' Get next file name.
strFileName = Dir()


Loop


MsgBox "Filenames fixed!", vbInformation, "Done now"
End Sub
 
D

Dirk Goldgar

I need to import an unspecified number of text files (converted from
Word to trap the formfield contents) into Access, however I need to
rename a number of the filenames to strip "."

I'm using code I found elsewhere on here, however whenever the loop
tries to rename a file, I get error message 53 'File not found'. The
file is very much there, and checking the variable contents in the
immediate window produces the right results.

Can anyone suggest why this **** code isn't working? Thanks!

-----------------------------------------------
Sub RemovePunctuation()
'On Error Resume Next

Dim strFolder As String
Dim strFileName As String
Dim strNewName As String


strFolder = "C:\Documents and Settings\CGUEVARA\My
Documents\doc\3ps\converted"


strFileName = Dir(strFolder & "\*.txt")


Do Until Len(strFileName) = 0


' Only rename the ones with embedded periods.
If strFileName Like "*.*.*" Then


' strip off the ".txt" to get new name
strNewName = Left(strFileName, Len(strFileName) - 4)


' remove any embedded periods (.)
strNewName = Replace(strNewName, ".", "")
strNewName = Replace(strNewName, ",", "")
strNewName = Replace(strNewName, " ", "")



' Rename the file.
Name strFileName As strNewName & ".txt"


'End If


' Get next file name.
strFileName = Dir()


Loop


MsgBox "Filenames fixed!", vbInformation, "Done now"
End Sub

I think you're going to have to prepend the folder path to both the old
and new names:

Name strFolder & "\" & strFileName _
As strFolder & "\" & strNewName & ".txt"
 
C

chantal.lyn

excellent, thanks - oh I feel stupid for not realising that!! It all
works beautifully now :D
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top