Rename a file based on a string VB SCRIPT

davidpayten

Board Regular
Joined
May 24, 2004
Messages
225
Hi All,

I am trying to rename a file that I have in a folder using "VB script". The the name of the file is currently:

>J:\Upload Data\connect\postbillcca_12052008.txt

I want to look for postbillcca_????????.txt
or
I want to look for postbillcca_???????.txt ( the day if single ie 6 will show as 6 instead of 06

Cant seem to find anything in the vb library that will help me, kind of new to this sort of thing.

Any help would be much appreciated.

Cheers

Dave
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Just for a start.
Code:
Sub test()
Dim myDir As String, fn As String, msgAs String
myDir = "J:\Upload Data\connect\"
fn = Dir(myDir & "postbillcca_*.txt")
msg = "Found"
If fn = "" Then msg = "No such file"
Do While fn <> ""
    If (LCase(fn) Like "postbillcca_########.txt") + _
       (LCase(fn) Like "postbillcca_#######.txt") Then
       msg = msg & vbLf & fn
    End If
    fn = Dir
Loop
MsgBox msg
End Sub
 
Upvote 0
Hey There,

Having trouble trying to execute the following bit of code. It doesn't seem to like / execute:
line 9 for > & right(f1.name,4) = ".txt"

In essence im trying to find a file based on name and whether it ends in txt and then if it exists shorten the name = postbill.txt

Again trying to do this in VB script:



Dim fs, f, f1, fc, s,d, strName
Set fs = CreateObject("Scripting.FileSystemObject")
folderspec = "J:\Upload Data\connect\"
Set f = fs.GetFolder(folderspec)
Set fc = f.Files
For Each f1 in fc

If Instr(1,f1.name,"postbillcca") > 0 & right(f1.name,4) = ".txt" Then

fs.GetFile(f1).copy folderspec
Filename = folderspec & f1.Name
NewFileName = folderspec & "postbillcca.txt"
fs.MoveFile Filename, NewFileName

End if


Next
 
Upvote 0
If Instr(1,f1.name,"postbillcca") > 0 & right(f1.name,4) = ".txt" Then

You can't use this operator...
What you want is:

If Instr(1,f1.name,"postbillcca") > 0 AND right(f1.name,4) = ".txt" Then

your code has a few more syntax errors of this same kind...make all those corrections and try it again.

AB


EDIT:
Note that this is okay:
NewFileName = folderspec & "postbillcca.txt"
(& is a concatenation operator for strings.)
 
Upvote 0

Forum statistics

Threads
1,214,392
Messages
6,119,255
Members
448,879
Latest member
oksanana

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top