Solved

rename all files extensions in one shot

Posted on 2008-06-24
22
563 Views
Last Modified: 2012-05-05
I want to write a VB code .. where I can rename all the files in 4 different folder...

like I have 4 folders

c:\folder\folder1\(contain bunch of files)
c:\folder\folder2\(contain bunch of files)
c:\folder\folder3\(contain bunch of files)
c:\folder\folder\folder4(contain bunch of files)

How can I write a program to rename all the files to extension *.backup?
0
Comment
Question by:CalmSoul
  • 8
  • 7
  • 3
  • +2
22 Comments
 
LVL 3

Expert Comment

by:DarrenMcCall
ID: 21860195
Make a shell call with these commands.
REN c:\folder\folder1\* *.backup
REN c:\folder\folder2\* *.backup
REN c:\folder\folder3\* *.backup
REN c:\folder\folder\folder4\* *.backup

Open in new window

0
 
LVL 5

Author Comment

by:CalmSoul
ID: 21860207
what is shell?
0
 
LVL 3

Expert Comment

by:DarrenMcCall
ID: 21860241
It's like you run a "cmd" prompt.
Are you trying to write VBScript or what exactly?
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 5

Author Comment

by:CalmSoul
ID: 21860269
I am trying to write an exe file?

I have VB6.0 installed on my machine!
0
 
LVL 7

Expert Comment

by:__Vortex__
ID: 21860279
Here is a VB6 example that will point you in the right direction:
http://www.vb-helper.com/howto_rename_files.html

If you need any more help please let me know

V
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 21860311
Or like this:
Renamefiles "c:\folder\folder1" 
Renamefiles "c:\folder\folder2" 
Renamefiles "c:\folder\folder3" 
Renamefiles "c:\folder\folder4" 
 
Sub RenameFiles(strFolder As String)
    Dim strFile As String
    Dim strFiles() As String
    Dim f As Integer
    Dim i As Integer
    Dim strFileParts() As String
    
    strFile = Dir$(strFolder & "\*.*")
    Do Until strFile = ""
    ReDim Preserve strFiles(f)
    strFiles(f) = strFile
    f = f + 1
    strFile = Dir$()
    Loop
    For i = 0 To f - 1
        strFileParts = Split(strFiles(i), ".")
        strFileParts(UBound(strFileParts)) = "backup"
        Name strFolder & "\" & strFiles(i) As Join(strFileParts)
    Next i
End Sub

Open in new window

0
 
LVL 3

Accepted Solution

by:
DarrenMcCall earned 250 total points
ID: 21860318
I have no way of testing this, but see if it works.
Private cmd1 As Double
cmd1 = Shell("cmd /c REN c:\test\* *.backup", vbNormalFocus)
 
Private cmd2 As Double
cmd2 = Shell("cmd /c REN c:\folder\folder2\* *.backup", vbNormalFocus)
 
Private cmd3 As Double
cmd3 = Shell("cmd /c REN c:\folder\folder3\* *.backup", vbNormalFocus)
 
Private cmd4 As Double
cmd4 = Shell("cmd /c REN c:\folder\folder\folder4\* *.backup", vbNormalFocus)

Open in new window

0
 
LVL 5

Author Comment

by:CalmSoul
ID: 21860381
DarrenMcCall:

its works but the problem is I want to hide I don't want user to click any thing...

at the moment it is working with "button click" function... any other way?

I just want exe file to run silently... how is that possible?
0
 
LVL 7

Expert Comment

by:__Vortex__
ID: 21860470
You can use the form_load event (Double click the form):

Private Sub Form_Load()
  'Insert your code here



  'Closes the form when done.
  Unload Me
End Sub
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 21860494

If you don't want the user to click anything, how do you want it the code to start?

If you get round to trying my code, there was an error in line 23.

Sub RenameFiles(strFolder As String)
    Dim strFile As String
    Dim strFiles() As String
    Dim f As Integer
    Dim i As Integer
    Dim strFileParts() As String
    
    strFile = Dir$(strFolder & "\*.*")
    Do Until strFile = ""
        ReDim Preserve strFiles(f)
        strFiles(f) = strFile
        f = f + 1
        strFile = Dir$()
    Loop
    For i = 0 To f - 1
        strFileParts = Split(strFiles(i), ".")
        strFileParts(UBound(strFileParts)) = "backup"
        Name strFolder & "\" & strFiles(i) As strFolder & "\" & Join(strFileParts, ".")
    Next i
End Sub

Open in new window

0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 21860517
Add a MODULE, create a Public "Sub Main", and place your code in there.  Afterwards go to Project --> Properties and change the "Startup object" to "Sub Main".
0
 
LVL 5

Author Comment

by:CalmSoul
ID: 21860563
thanks __Vortex__:

Now, how can I add validations like if folder don't exist insert folder?


0
 
LVL 7

Expert Comment

by:__Vortex__
ID: 21860640
Try This

strDirectory = "c:\test"
If Dir$(strDirectiry, vbDirectory) <> vbNullString Then
  Private cmd1 As Double
  cmd1 = Shell("cmd /c REN c:\test\* *.backup", vbNormalFocus)
Else
  msgbox("Folder test does not exist")
End If
0
 
LVL 7

Expert Comment

by:__Vortex__
ID: 21860664
Sorry i didn't notice the spelling mistake:

Dim strDirectory as String
strDirectory = "c:\test"
If Dir$(strDirectory, vbDirectory) <> vbNullString Then
  Private cmd1 As Double
  cmd1 = Shell("cmd /c REN c:\test\* *.backup", vbNormalFocus)
Else
  msgbox("Folder test does not exist")
End If
0
 
LVL 5

Author Comment

by:CalmSoul
ID: 21860691
Can we do else create a folder?

Else
  msgbox("Folder test does not exist")
0
 
LVL 7

Expert Comment

by:__Vortex__
ID: 21860706
of course:

Else
   MkDir "c:\test"
0
 
LVL 5

Author Comment

by:CalmSoul
ID: 21865831
Guys:

What if I want to pass the path as variable like this ..

Dim cmd1 As Double
Dim cmd2 as string
cmd2 = location.text
cmd1 = Shell("cmd /c REN c:\* *.backup", vbNormalFocus)

what will change in CMD1

cmd1 = Shell("cmd /c REN 'cmd2'* *.backup", vbNormalFocus)????

I am getting errors!
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 21866142
Something like:

    cmd1 = Shell("cmd /c REN " & cmd2 & "* *.backup", vbNormalFocus)
0
 
LVL 5

Author Comment

by:CalmSoul
ID: 21866918

Else
   MkDir "c:\test"

is not working

is it like

shell (cmd ... something??)
0
 
LVL 5

Author Comment

by:CalmSoul
ID: 21868373
How about if I only want to rename a file with specific extension in the folder? say only text files?


cmd3 = Shell("cmd /c REN c:\folder\folder3\* txt.backup", vbNormalFocus)


???
0
 
LVL 7

Expert Comment

by:__Vortex__
ID: 21868445
This will create a folder:

  Dim cmd4 As Double

  cmd4 = Shell("cmd /c MD c:\folder\folder\folder4", vbNormalFocus)

or

  Dim cmd4 As Double
  Dim strPath as string

  strPath = "c:\folder\folder\folder4"

  cmd4 = Shell("cmd /c MD " & strPath , vbNormalFocus)
0
 
LVL 7

Assisted Solution

by:__Vortex__
__Vortex__ earned 250 total points
ID: 21868465
to rename .txt it would be this:

Shell("cmd /c REN c:\folder\folder3\*.txt *.backup", vbNormalFocus)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question