Solved

rename all files extensions in one shot

Posted on 2008-06-24
22
551 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now