Home All Groups Group Topic Archive Search About

Looking for Easy Way to Acomplish this Task.

Author
27 Nov 2007 10:07 PM
Kardon Coupé
Dear All,

I'm looking for a easy way to....

{Start at Root Directory}
-{Check Amount of Directories}
--{Go into first Directory}
---{Locate file based on file extension}
----{Rename Found file to same name as Directory its within}
---{Return to Root Directory}
--{Go to Next Directory}
-{Repeat Process until all Directory's are processed}
{End}

I've never been one to master directories and recurrsive searching, so I
don't want to approach this the wrong way, and dig a big hole for myself....

Thanks in Advance.
Regards
Paul.

Author
27 Nov 2007 10:25 PM
Mike Williams
"Kardon Coupé" <prefer.to@readon.newsgroups> wrote in message
news:u0cH1IUMIHA.5860@TK2MSFTNGP04.phx.gbl...

> I'm looking for a easy way to....
> {Start at Root Directory}
> -{Check Amount of Directories}
> --{Go into first Directory}
> ---{Locate file based on file extension}
> ----{Rename Found file to same name as Directory its within}

So if there are a hundred files with that extension in a specific directory
you want to give them all the same name!

Mike
Author
27 Nov 2007 10:32 PM
Kardon Coupé
> So if there are a hundred files with that extension in a specific
> directory you want to give them all the same name!

Nope, there will be one file in there with one extension..... I have already
done a lot of work on the directories on my hard drive, but the final thing
I want to do, is save me having to go into each folder, and press "f2" copy,
go to file, "f2" paste.....

I just wanted to automate things using VB, but like I have to admit, when it
comes to working with directories and files within VB, I always manage to
muck somat up....hence turning here for advice...

Regards
Paul.
Author
28 Nov 2007 4:56 AM
Mike Williams
"Kardon Coupé" <prefer.to@readon.newsgroups> wrote in message
news:elgceWUMIHA.5300@TK2MSFTNGP04.phx.gbl...

> Nope, there will be one file in there with one extension.....
> I have already done a lot of work on the directories on
> my hard drive, but the final thing I want to do, is save
> me having to go into each folder, and press "f2" copy, go to file, "f2"
> paste.....

Right. Well I still don't understand why you've got only one file with a
specific extension in any one directory, or how you can be sure of that in
advance without getting your code to check, but here is a general  routine
for recursively trawling directories looking for and counting files. At the
moment it just dumps the name of files that match your specified criteria
into a ListBox, and I'll leave it to you to add the renaming stuff yourself.
Be careful of course to check that you have permission and that the file is
"free for renaming" before renaming a file, or trap the error that would
otherwise occur. Paste this example into a VB Form containing a fairly wide
ListBox and a Command Button. The code will take some time if you point it
at your main root folder of course, especially on the first run, but then so
would all the alternative "native Windows" methods.

Mike

Option Explicit
Option Compare Text
Private Declare Function FindFirstFile Lib "kernel32" _
Alias "FindFirstFileA" (ByVal lpFileName As String, _
lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindNextFile Lib "kernel32" _
Alias "FindNextFileA" (ByVal hFindFile As Long, _
lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindClose Lib "kernel32" _
(ByVal hFindFile As Long) As Long
Private Type FILETIME
   dwLowDateTime As Long
   dwHighDateTime As Long
End Type
Private Type WIN32_FIND_DATA
    lngFileAttributes As Long
    ftCreationTime As FILETIME
    ftLastAccessTime As FILETIME
    ftLastWriteTime As FILETIME
    lngFileSizeHigh As Long
    lngFileSizeLow As Long
    lngReserved0 As Long
    lngReserved1 As Long
    FilenameName As String * 260
    strAlternate As String * 14
End Type
Private TotalFolders As Long
Private TotalFiles As Long
Private Matches As Long
Private Const FILE_ATTRIBUTE_DIRECTORY = &H10

Private Sub CountFiles(Folder As String)
Dim mydata As WIN32_FIND_DATA
Dim hFind As Long, Filename As String, Search As String
If Right$(Folder, 1) <> "\" Then Folder = Folder & "\"
Search = Folder & "*"
hFind = FindFirstFile(Search, mydata)
If hFind > 0 Then
Do
Filename = Left$(mydata.FilenameName, _
    InStr(mydata.FilenameName, Chr$(0)) - 1)
If (mydata.lngFileAttributes And _
    FILE_ATTRIBUTE_DIRECTORY) = _
    FILE_ATTRIBUTE_DIRECTORY Then
    If Filename = "." Or Filename = ".." Then
      '
      Else
      ' It is a folder
    TotalFolders = TotalFolders + 1
    ' recurse this folder
    CountFiles Folder & Filename ' recursive call to check sub folders
    End If
Else
    ' It is a file
    TotalFiles = TotalFiles + 1
    ' Just display files beginning with "M"
    If Right$(Filename, 4) = ".txt" Then
      Matches = Matches + 1
      List1.AddItem Folder & Filename
      'List1.AddItem Filename
    End If
End If
Loop While CBool(FindNextFile(hFind, mydata))
Call FindClose(hFind)
End If
End Sub

Private Sub Command1_Click()
Caption = "Please wait . . ."
CountFiles "c:\temp"
Me.Caption = "No. of Folders = " & Format(TotalFolders) _
  & " : No. of files = " & Format(TotalFiles) _
  & " : No. of matches = " & Format(Matches)
End Sub
Author
28 Nov 2007 12:51 AM
dpb
Kardon Coupé wrote:
Show quote
> Dear All,
>
> I'm looking for a easy way to....
>
> {Start at Root Directory}
> -{Check Amount of Directories}
> --{Go into first Directory}
> ---{Locate file based on file extension}
> ----{Rename Found file to same name as Directory its within}
> ---{Return to Root Directory}
> --{Go to Next Directory}
> -{Repeat Process until all Directory's are processed}
> {End}
>
....

C:\> for /r %f in (*.jnk) do ren %f %_cwds%@name[%_cwd].%@ext[%f]

Substitute your desired extension for the 'jnk' extension in the above.

--

AddThis Social Bookmark Button