Link to home
Start Free TrialLog in
Avatar of CalmSoul
CalmSoulFlag for United States of America

asked on

rename all files extensions in one shot

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?
Avatar of DarrenMcCall
DarrenMcCall
Flag of United States of America image

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

Avatar of CalmSoul

ASKER

what is shell?
It's like you run a "cmd" prompt.
Are you trying to write VBScript or what exactly?
I am trying to write an exe file?

I have VB6.0 installed on my machine!
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
Avatar of GrahamSkan
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

ASKER CERTIFIED SOLUTION
Avatar of DarrenMcCall
DarrenMcCall
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
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

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

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".
thanks __Vortex__:

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


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
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
Can we do else create a folder?

Else
  msgbox("Folder test does not exist")
of course:

Else
   MkDir "c:\test"
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!
Something like:

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

Else
   MkDir "c:\test"

is not working

is it like

shell (cmd ... something??)
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)


???
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)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial