CalmSoul
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(c ontain bunch of files)
How can I write a program to rename all the files to extension *.backup?
like I have 4 folders
c:\folder\folder1\(contain
c:\folder\folder2\(contain
c:\folder\folder3\(contain
c:\folder\folder\folder4(c
How can I write a program to rename all the files to extension *.backup?
ASKER
what is shell?
It's like you run a "cmd" prompt.
Are you trying to write VBScript or what exactly?
Are you trying to write VBScript or what exactly?
ASKER
I am trying to write an exe file?
I have VB6.0 installed on my machine!
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
http://www.vb-helper.com/howto_rename_files.html
If you need any more help please let me know
V
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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?
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
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
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".
ASKER
thanks __Vortex__:
Now, how can I add validations like if folder don't exist insert folder?
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
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
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
ASKER
Can we do else create a folder?
Else
msgbox("Folder test does not exist")
Else
msgbox("Folder test does not exist")
of course:
Else
MkDir "c:\test"
Else
MkDir "c:\test"
ASKER
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!
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)
cmd1 = Shell("cmd /c REN " & cmd2 & "* *.backup", vbNormalFocus)
ASKER
Else
MkDir "c:\test"
is not working
is it like
shell (cmd ... something??)
ASKER
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)
???
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)
Dim cmd4 As Double
cmd4 = Shell("cmd /c MD c:\folder\folder\folder4",
or
Dim cmd4 As Double
Dim strPath as string
strPath = "c:\folder\folder\folder4"
cmd4 = Shell("cmd /c MD " & strPath , vbNormalFocus)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window