如何获取字符串的结尾部分并连接到另一个字符串?

时间:2017-10-27 19:07:58

标签: string vbscript split

更新脚本以提高效率。 JAL脚本在循环中调用VBScript来检查txt文件中的每个文件。

我需要帮助的是获取文件名,将FormattedDT(日期/时间)添加到它(原始文件名是FullFileName),然后将其复制到存档文件夹:

\\a70tsgnaeasa001\eas\server\log\archive

例如,如果正在读取的txt文件中的文件名是:

 \\A70TSGNAEASA001\d$\EAS\Server\FIPS\BCBSFIPS.TXT

它最后一次修改时间是10/27/2017 3:00,然后我希望目的地是:

\\a70tsgnaeasa001\eas\server\log\archive\BCBSFIPS_10_27_2017_03_00_00.TXT

我无法弄清楚如何删除.extension,附加文件的日期/时间,然后将其复制到存档文件夹。我把目前为止的代码放在下面。

TXT文件格式:

REM there is a comment here on the first line
\\filepath1
\\filepath2
...
\\filepathN

的VBScript:

option explicit

Dim SFSO, f, WSSL, FullFileName, txtFile, exitCode, txtLine, ArchiveFolder,         
DateFolder, CopyFileName
Dim ArchDT, FormattedDT, mon, day, yr, hr, min, sec, FileNameArray
ArchiveFolder = "\\\\A70TSGNAEASA001\\eas\\Server\\log\\Archive\\"

Set SFSO = CreateObject("Scripting.FileSystemObject")
Set WSSL = Wscript.CreateObject("WScript.Shell")

exitCode = 0

'tests for no args
If WScript.Arguments.Count = 0 then
    WScript.quit (9999)
End If

txtFile = WScript.Arguments(0)

DateFolder = ArchiveFolder&Year(Date)&"-"&Month(Date)&"-"&Day(Date)&"\\"

SFSO.CreateFolder(DateFolder)

Set f = SFSO.OpenTextFile(txtFile)
On Error Resume Next
Do Until f.AtEndOfStream

txtLine = f.ReadLine
If(Left(txtLine,2) = "\\") Then
    FullFileName = txtLine

    ' Check for file exists  
    If SFSO.fileexists(FullFileName) Then 
        ' create archive folder path

        'get modified date time, format to _mm_dd_yyyy_hh_mm_ss
        ArchDT = CDate(SFSO.DateLastModified(FullFileName))
        mon = Month(ArchDT)
        day = Day(ArchDT)
        yr = Year(ArchDT)
        hr = Hour(ArchDT)
        min = Minute(ArchDT)
        sec = Second(ArchDT)
        FormattedDT = "_"&mon&"_"&day&"_"&yr&"_"&hr&"_"&min&"_"&sec

        ' File name with date/time appended to it
        FileNameArray = Split(FullFileName, "\")
        ShortFileName = FileNameArray(UBound(FileNameArray))
        FileNameArray2 = Split(ShortFileName, ".")
        CopyFileName =     
DateFolder&FileNameArray2(0)&FormattedDT&FileNameArray2(1)      

        'copy file to archive folder
        SFSO.CopyFile FullFileName, CopyFileName 
    Else
    'exitCode = 9999
    End If
End If
Loop

f.Close
SFSO.Close
WScript.quit(exitCode)

1 个答案:

答案 0 :(得分:1)

我对代码进行了一些更改,只是为了让它更好地阅读,因为你在那里有一些拼写错误等。

我删除了一些变量,因为您使用了几个保留字(DayEmpty)并将ArchiveFolder重新调整为常量。

代码的其余部分依据是else步骤。那时我得到了相关文件的最后修改日期(你试图使用SFSO对象,但是对象没有那个方法可用,所以我使用了GetFile确实

我将日期分为日期和时间元素,并根据您的要求重新格式化,然后将数组重新加入到一起,以提供要附加到存档文件夹中文件名的格式化日期值。

然后我将包含文件路径的FullFileName变量拆分为“\”字符,并选出最后一个元素,该元素将包含文件名。

然后从ArchiveFolder构建CopyFileName,并使用格式化日期和“.txt”后缀替换现有文件名后缀来存档文件名。

如果您不理解我所做的任何事情,请告诉我,我会尝试进一步解释。

option explicit

Dim SFSO, WSSL, FullFileName, txtFile, exitCode, txtLine, CopyFileName
Dim ArchDT, FormattedDT, fileName, tempArray
Const ArchiveFolder = "\\A70TSGNAEASA001\eas\Server\log\Archive\"

Set SFSO = CreateObject("Scripting.FileSystemObject")
Set WSSL = Wscript.CreateObject("WScript.Shell")

exitCode = 0

'tests for no args
If WScript.Arguments.Count = 0 then
    WScript.quit (9999)
End If

txtFile = WScript.Arguments(0)

Set f = SFSO.OpenTextFile(txtFile)
On Error Resume Next
Do Until f.AtEndOfStream
    txtLine = f.ReadLine
    If(Left(txtLine,2) == "\\")
        FullFileName = txtLine
        ' Check for file exists  
        If SFSO.fileexists(FullFileName) then 
            ' Check file status
            If VarType(SFSO.OpenTextFile(FullFileName,8,False)) = vbError Then      
                exitCode = 9999
            Else
                ArchDT = oFso.GetFile(FullFileName).DateLastModified        
                'get modified date time, format to _mm_dd_yyyy_hh_mm_ss
                tempArray = Split(ArchDT, " ")  ' gives us an array with date and time in two elements
                tempArray(0) = Right("0" & Month(ArchDT),2) & "_" & Right("0" & Day(ArchDT), 2) & "_" & Year(ArchDT)
                tempArray(1) = Replace(tempArray(1), ":", "_")
                FormattedDT = Join(tempArray, "_")  ' now have the formatted date value

                ' Get the filename
                tempArray = Split(FullFileName, "\")

                fileName = tempArray(UBound(tempArray)) ' filename is the last element in this array
                ' File name with date/time appended to it
                    'Part I'm having trouble with
                CopyFileName = ArchiveFolder & Replace(fileName, ".txt", FormattedDT & ".txt")

                'copy file to archive folder (how I would copy the file)
                SFSO.CopyFile(FullFileName, CopyFileName)
            End If
        Else
            exitCode = 9999
        End If
    End If
Loop

f.Close
Err.Clear
SFSO.Close
WScript.quit(exitCode)
相关问题