VBS-如何获取文件名的今天?

时间:2019-02-19 17:24:07

标签: excel vbscript

我有一个程序可以将.csv转换为.xls文件,但是文件中具有今天的日期,例如“ C:\ Users \ user \ Downloads \ arquivo_2019-02-19.csv”,然后在需要时要进行转换,有必要为每个文件更改vbs代码(在此消息的末尾)。

我有一个函数可以在另一个示例中使用,我在这里只是为了描述它的功能。

    %let todaysDate = %sysfunc(today(), yymmdd10.); 
filename  test1 ftp "C:\Users\user\Downloads\arquivo_&todaysdate..csv"

如何自动执行此过程以获取每个新文件的今天日期的代码?

Option Explicit

Dim fldr, f, file,strOrigFile, strFile, fso, strDirectory
Dim objExcel

strOrigFile = "C:\Users\user\Downloads\arquivo_2019-02-19.csv"  
'******************************************************
'CONVERTS THE FILE TO AN EXCEL FILE
'******************************************************
strFile = replace(strOrigFile,".csv",".xls")

Set objExcel = CreateObject("Excel.Application")

objExcel.Workbooks.Open strOrigFile

objExcel.DisplayAlerts = False
objExcel.ActiveWorkbook.SaveAs strFile, -4143

objExcel.DisplayAlerts = False
objExcel.ActiveWorkbook.Close
objExcel.DisplayAlerts = False
objExcel.Application.Quit

'******************************************************
'FORMAT THE EXCEL FILE AND SAVE IT
'******************************************************

objExcel.Workbooks.Open strFile

objExcel.columns("A:A").numberformat="0"
objExcel.columns("C:C").numberformat="0"


objExcel.DisplayAlerts = False
objExcel.ActiveWorkbook.SaveAs strFile, -4143

objExcel.DisplayAlerts = False
objExcel.ActiveWorkbook.Close
objExcel.DisplayAlerts = False
objExcel.Application.Quit
Set objExcel = Nothing

msgbox "FINISHED!!!"

1 个答案:

答案 0 :(得分:-1)

尝试一下:

Option Explicit

Dim fldr, f, file,strOrigFile, strFile, fso, strDirectory
Dim objExcel
Dim dd, mm, yy
Dim datevalue, timevalue, dtsnow, dtsvalue

dtsnow = Now()
dd = Right("00" & Day(dtsnow), 2)
mm = Right("00" & Month(dtsnow), 2)
yy = Year(dtsnow)

datevalue = yy & "-" & mm & "-" & dd

strOrigFile = "C:\Users\user\Downloads\arquivo_" & datevalue & ".csv"

'******************************************************
'CONVERTS THE FILE TO AN EXCEL FILE
'******************************************************
strFile = replace(strOrigFile,".csv",".xls")

Set objExcel = CreateObject("Excel.Application")

objExcel.Workbooks.Open strOrigFile

objExcel.DisplayAlerts = False
objExcel.ActiveWorkbook.SaveAs strFile, -4143

objExcel.DisplayAlerts = False
objExcel.ActiveWorkbook.Close
objExcel.DisplayAlerts = False
objExcel.Application.Quit

'******************************************************
'FORMAT THE EXCEL FILE AND SAVE IT
'******************************************************

objExcel.Workbooks.Open strFile

objExcel.columns("A:A").numberformat="0"
objExcel.columns("C:C").numberformat="0"

objExcel.DisplayAlerts = False
objExcel.ActiveWorkbook.SaveAs strFile, -4143

objExcel.DisplayAlerts = False
objExcel.ActiveWorkbook.Close
objExcel.DisplayAlerts = False
objExcel.Application.Quit
Set objExcel = Nothing

msgbox "FINISHED!!!"