Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / April 2006

Tip: Looking for answers? Try searching our database.

dir command vba strange behaviour

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
canvas - 05 Apr 2006 07:23 GMT
Hello,
A question.
I have many directories and files in them. I wrote a vba script to rename
the files in the directories.
The files must have the name of the directory where it's in:
If the file: test.ape is in the directory 12 then the files must be:
12_test.ape.
The script works but the strange thing is that if the filenames are changed
the dir command sees the new
name and renames it again like 12_test12_test_12_test.ape.
Is there a way to avoid this???/

Sub autoopen()
Dim strNewName As String
Dim strOldName As String
Dim pad As String, map As String
Dim test As Integer
Dim x As Integer
pad = ActiveDocument.Path
test = MsgBox("doorgaan?", vbYesNo)
If test = vbNo Then
   Exit Sub
End If
For x = 1 To 300
   strOldName = Dir(pad & "\" & x & "\*.ape")
   Do While Len(strOldName) > 0
       strNewName = x & "_" & strOldName
       Name pad & "\" & x & "\" & strOldName As pad & "\" & x & "\" &
strNewName
       strOldName = Dir()
   Loop
Next x
MsgBox "ready"
Application.Quit
End Sub

Thanks a lot
Jezebel - 05 Apr 2006 08:23 GMT
Dir() retrieves filenames, not files. If you rename the file, that's a new
name, and Dir() retrieves it.

What you need to do is to collect all the filenames in the folder (eg into a
collection), then rename them all, maybe something like this --

Dim pCollection as collection
Dim pName as variant            'Has to be a variant or object to iterate a
collection

For x = 1 To 300

  'Get all the names in the folder
  set pCollection = new collection
  strOldName = Dir(pad & "\" & x & "\*.ape")
  Do While Len(strOldName) > 0
       pCollection.Add strOldName
      strOldName = Dir()
  Loop

   'Rename the files
   For each pName in pCollection
         strOldName = pad & "\" & x & "\" & pName
         strNewName = pad & "\" & x & "\" & x & "_" & strOldName
         Name strOldName As  strNewName
   Next

Next

> Hello,
> A question.
[quoted text clipped - 34 lines]
>
> Thanks a lot
Access101 - 05 Apr 2006 18:42 GMT
Is the filename prefix consistent enough to do this:

If mid(fname, 3, 1) = "_" then
   'parse before renaming file
else
  'rename file normally
end if

> Dir() retrieves filenames, not files. If you rename the file, that's a new
> name, and Dir() retrieves it.
[quoted text clipped - 63 lines]
> >
> > Thanks a lot
canvas - 06 Apr 2006 07:29 GMT
Thank you very much this works great...superb
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.