Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 596
  • Last Modified:

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?
0
CalmSoul
Asked:
CalmSoul
  • 8
  • 7
  • 3
  • +2
2 Solutions
 
DarrenMcCallCommented:
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
 
CalmSoulAuthor Commented:
what is shell?
0
 
DarrenMcCallCommented:
It's like you run a "cmd" prompt.
Are you trying to write VBScript or what exactly?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
CalmSoulAuthor Commented:
I am trying to write an exe file?

I have VB6.0 installed on my machine!
0
 
__Vortex__Commented:
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
 
GrahamSkanRetiredCommented:
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
 
DarrenMcCallCommented:
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
 
CalmSoulAuthor Commented:
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
 
__Vortex__Commented:
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
 
GrahamSkanRetiredCommented:

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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
CalmSoulAuthor Commented:
thanks __Vortex__:

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


0
 
__Vortex__Commented:
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
 
__Vortex__Commented:
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
 
CalmSoulAuthor Commented:
Can we do else create a folder?

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

Else
   MkDir "c:\test"
0
 
CalmSoulAuthor Commented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Something like:

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

Else
   MkDir "c:\test"

is not working

is it like

shell (cmd ... something??)
0
 
CalmSoulAuthor Commented:
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
 
__Vortex__Commented:
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
 
__Vortex__Commented:
to rename .txt it would be this:

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

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 8
  • 7
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now