Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search for file extension

Status
Not open for further replies.

GavinRoss

MIS
Aug 31, 2011
8
US
Hello,

Hope someone can help. I am using this script to search for files older than a certain date and then move them to another folder. The script is working fine, however I need to only move a certain file extension.. e.g. .xls. Anyone know how I can modify this script to only move these file extensions?

Thanks

Dim objFSO, ofolder, objStream

Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("scripting.filesystemobject")
Set objNet = CreateObject("WScript.NetWork")
Set FSO = CreateObject("Scripting.FileSystemObject")
set outfile = fso.createtextfile("Move-Result.txt",true)
SPath = "D:\Strata\Strata_Billing\"

CheckFolder(objFSO.getFolder(SPath))

Sub CheckFolder(objCurrentFolder)
Dim strTempL, strTempR, strSearchL, strSearchR, objNewFolder, objFile, strSize
Const OverwriteExisting = True
currDate = Date
dtmDate = Date - 60
strTargetDate = ConvDate(dtmDate)
For Each objFile In objCurrentFolder.Files
FileName = objFile
'WScript.Echo FileName & " " & strSize
strDate = ConvDate(objFile.DateCreated)
strDate = ConvDate(objFile.DateLastModified)
strSize = ConvertSize (objFile.Size)
If strDate < strTargetDate Then
objFSO.MoveFile FileName, "D:\Strata\Strata_Billing\Strata_Archive\"
outfile.writeline filename & ", ," & strSize
End If
Next
End Sub

Function ConvDate (sDate) 'Converts MM/DD/YYYY HH:MM:SS to string YYYYMMDD
strModifyDay = day(sDate)
If len(strModifyDay) < 2 Then
strModifyDay = "0" & strModifyDay
End If
strModifyMonth = Month(sDate)
If len(strModifyMonth) < 2 Then
strModifyMonth = "0" & strModifyMonth
End If
strModifyYear = Year(sDate)
ConvDate = strModifyYear & strModifyMonth & strModifyDay
End Function

Function ConvertSize(Size) 'Converts file size into KB,MB,GB,TB
Size = CSng(Replace(Size,",",""))

If Not VarType(Size) = vbSingle Then
ConvertSize = "SIZE INPUT ERROR"
Exit Function
End If

Suffix = " Bytes"
If Size >= 1024 Then suffix = " KB"
If Size >= 1048576 Then suffix = " MB"
If Size >= 1073741824 Then suffix = " GB"
If Size >= 1099511627776 Then suffix = " TB"

Select Case Suffix
Case " KB" Size = Round(Size / 1024, 1)
Case " MB" Size = Round(Size / 1048576, 1)
Case " GB" Size = Round(Size / 1073741824, 1)
Case " TB" Size = Round(Size / 1099511627776, 1)
End Select

ConvertSize = Size & Suffix
End Function
 
when you loop through the files, check the extention (.xls)

Code:
For Each objFile In objCurrentFolder.Files
   FileName = objFile
   [red]if (right(FileName, 4) = ".xls") then
       move the file
   end if [/red]
next

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Thanks for the reply... when I add that to the script I get "Expected end of statement"
any ideas?

Thanks
 
It means your missing some syntax. Post only the checkFolder() sub inside [ignore]
Code:
...
[/ignore] tags

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Here is the code;

Sub CheckFolder(objCurrentFolder)
Dim strTempL, strTempR, strSearchL, strSearchR, objNewFolder, objFile, strSize
Const OverwriteExisting = True
currDate = Date
dtmDate = Date - 1
strTargetDate = ConvDate(dtmDate)
For Each objFile In objCurrentFolder.Files
FileName = objFile
If (right(FileName, 4) = ".xls") Then
move the file
Next
End If
'WScript.Echo FileName & " " & strSize
strDate = ConvDate(objFile.DateCreated)
strDate = ConvDate(objFile.DateLastModified)
strSize = ConvertSize (objFile.Size)
If strDate < strTargetDate Then
objFSO.MoveFile FileName, "C:\Test\archive\"
outfile.writeline filename & ", ," & strSize
End If
Next
End Sub
 
Seriously...

Code:
If (right(FileName, 4) = ".xls") Then
   [red]move the file[/red]
Next

Put your code in there. And please use [ignore]
Code:
...
[/ignore] tags

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
>if (right(FileName, 4) = ".xls")

FileSystemObject has a method to return the file extension

In this example:

if objFSO.GetExtensionName(objFile.Path)="XLS" Then

or, if you like default methods:

if objFSO.GetExtensionName(objFile)="XLS" Then

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top