How to add timestamp to filename in UFT when using writetofile property - hp-uft

I trying to generate files with time stamp when I'm running test with multiple inputs(parameterisation).
Please suggest me how to add timestamp to filename
ex: C:\xxx\xxxx\xxx\Responses\xxx.xlsx to this path I need to append timestamp to make file unique .

Easy option:
Dim sTimeStamp, sPath
sPath = "C:\Path\To\My\Folder\myFile.xlsx"
' Get current time and date value and strip out the separators
sTimeStamp = Replace(Replace(Replace(Now, " ", "_"), "/", ""), ":", "")
'insert timestamp into file name
sPath = Replace(sPath, ".xlsx", "_" & sTimeStamp & ".xlsx")
Debug.Write sPath ' output is C:\Path\To\My\Folder\myFile_20112017_015827.xlsx

Related

VBS Script to Replace a file if its modified date does not match stated date

I have the following script that will copy a file from one location to another, but what I require is for the script to only run if the modified date does not match the date that is stated in the vb script.
I have attempted to use DateDiff but can't get it work. This is the simple script I have so far, which will overwrite regardless:
dim filesys
set filesys=CreateObject("Scripting.FileSystemObject")
If filesys.FileExists("C:\test1\test.txt") Then
filesys.CopyFile "C:\test1\test.txt", "C:\test2\"
End If
How do I check for the file modified-date and compare it with another date before starting the copying process?
Assuming that you want to get the date modified and one possible way to compare as strings since you want to see if the dates are not equal.
dim filesys
set filesys=CreateObject("Scripting.FileSystemObject")
Dim sFilespec
sFilespec = "C:\test1\test.txt"
If filesys.FileExists(sFilespec) Then
' Get DateCreated
dateModified = FormatDateTime(ShowFileInfo(sFilespec),2)
dateStated = FormatDateTime(Now(),2)
Debug.WriteLine("Modified: " & FormatDateTime(dateModified,2))
Debug.WriteLine(" Stated: " & FormatDateTime(dateStated,2))
Debug.WriteLine("")
'
If dateModified <> dateStated Then
Debug.WriteLine dateModified & " is not equal to " & dateStated
filesys.CopyFile sFilespec, "C:\test2\"
Else
Debug.WriteLine dateModified & " is equal to " & dateStated
End If
End If
' Get the date last modified
Function ShowFileInfo(filespec)
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFile(filespec)
ShowFileInfo = f.DateLastModified
End Function

Renaming multiple files in a loop

I have a folder with 8 Excel files with the following naming convention:
date_All_Groups
date_HRFull_Status_All
date_RME_Groups_Excluded
These files are used for monthly reports, therefore the date will obviously always be different.
I will be using a macro to manipulate the data in each worksheet, however I cannot create the macro due the changing file name (the date) - the only guarantee I have is that each of these files will DEFINITELY contain a partial string match.
I have a script that finds the files in the location and will rename the file, but it only renames 1 file and its not the first file in the folder.
My issue is using the For Each loop effectively.
Here's the code I have:
Dim fso, folder, file
Dim folderName, searchFileName, renameFile1, renameFile2, renameFile3, renameFile4, renameFile5, renameFile6, renameFile7, renameFile8
'Path
folderName = "C:\test\"
'Future FileName
renameFile1 = "All Groups.csv"
renameFile2 = "Groups Excluded.csv"
renameFile3 = "No Exclusions.csv"
renameFile4 = "HR.csv"
renameFile5 = "AD Users.csv"
renameFile6 = "Encryption Status.csv"
renameFile7 = "ePO4 Not Encrypted.csv"
renameFile8 = "ePO5 Not Encrypted.csv"
' Create filesystem object and the folder object
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder(folderName)
' Loop over all files in the folder until the searchFileName is found
For Each file In folder.Files
' See If the file starts with the name we search
If InStr(file.Name, "All_Groups") then
file.Name = renameFile1
End If
If InStr(file.Name, "Groups_Excluded") Then
file.Name = renameFile2
End If
If InStr(file.Name, "No_Exclusions") Then
file.Name = renameFile3
End If
If InStr(file.Name, "HR") Then
file.Name = renameFile4
End If
If InStr(file.Name, "AD_Users") then
file.Name = renameFile5
End If
If InStr(file.Name, "Encryption_Status") then
file.Name = renameFile6
End If
If InStr(file.Name, "ePO4") then
file.Name = renameFile7
End If
If InStr(file.Name, "ePO5") then
file.Name = renameFile8
End If
Exit For
' echo the job is completed
WScript.Echo "Completed!"
Next
The original code I found was exactly as above, but with only one If statement inside the For Each loop and the Exit For was inside the If statement.
Currently when I execute the script, the code renames only one file and its always the HR file first.
If I execute the script again, it then starts with All Groups, then Groups Excluded, and so on.
And the "Echo Completed" does not do anything either.
If you just want to rename your files to "canonical" names you could do something like this, assuming that you just want the date from the beginning of the filename removed and the underscores replaced with spaces:
Set re = New RegExp
re.Pattern = "\d{4}-\d{2}-\d{2}_(.*\.csv)"
For Each f In folder.Files
For Each m In re.Execute(f.Name)
f.Name = Replace(m.Submatches(0), "_", " ")
Next
Next
If the files have the same "date" you only need Find for that, for excample (if the date is a iso date "YYYYMMDD") (Date Returns "today" date)
IsoDate=CStr(Year(Date)) & Right("0" & CStr(Month(Date)),2) & Right("0" & CStr(Day(Date)),2)
And the for each:
For Each file In folder.Files
If InStr(file.Name, IsoDate) = 1 then 'if is in the start of the string
file.Name = Mid(file.Name, Len(IsoDate)+1) 'The same name with out date
End IF
Next

Remove Number and period in filename vbsript

I have already found a script that will replace underscore (_) and other text that I write in script. I need to modify this script so that it will also remove all the numbers in the filename too. I tried [0-9] and /d but that didn't remove the numbers in the filename. Also I tried to remove a period in the file name, but that also removed the file extension. So it took away the .csv too. Can someone help?
'========================================================
' VBScript to replace underscore in file name with space
' for each files in a folder
' Written by ApOgEE of http://coderstalk.blogspot.com
'========================================================
Dim sName
Dim fso
Dim fol
' create the filesystem object
Set fso = WScript.CreateObject("Scripting.FileSystemObject")
' get current folder
Set fol = fso.GetFolder(".")
' go thru each files in the folder
For Each fil In fol.Files
' check if the file name contains underscore
If InStr(1, fil.Name, "_") <> 0 Then
' replace underscore with space
sName = Replace(fil.Name, "_", " ")
' rename the file
fil.Name = sName
End If
Next
' echo the job is completed
WScript.Echo "Completed!"
The Replace function does only simple string replacements. It doesn't support wildcards or patterns. You're looking for regular expressions:
Set re = New RegExp
re.Pattern = "[0-9_.]"
re.Global = True
For Each fil In fol.Files
fil.Name = re.Replace(fil.Name, " ")
Next

how do i get all .csv files from a folder for the present day using vbscript

Scan folder and list only .csv files which are created on that particular day.
option explicit
dim fileSystem, folder, file, path, myDate
path = "C:\vbs"
Set fileSystem = CreateObject("Scripting.FileSystemObject")
myDate = dateadd("d", -1, FormatDateTime(Now, 2))
Set folder = fileSystem.GetFolder(path)
for each file in folder.Files
if file.DateCreated > myDate then
WScript.Echo file.Name & " created at " & file.DateCreated
If UCase(filesystem.GetExtensionName(objFile.name)) = "csv" then
Wscript.Echo objFile.Name
End If
End If
next
Your
If UCase(filesystem.GetExtensionName(objFile.name)) = "csv" then
transforms the extension to uppercase, but then compares it to a lowercase "csv".
DateAdd's third parameter should be a date; Now (a date) shouldn't be converted to a string.

Converting VBscript to VB Error (checking for right characters)

The following code was written in vbscript and I'm in the process of converting over to visual basic.
On the following line: If Right(LCase(oFile.Name), 3) = "pdf" Then I get the following error: Variable 'Right' is used before it has been assigned a value. A null reference exception could result at runtime. Also is says Object variable or With block variable not set.
To my best of knowledge, I believe it is checking to make sure the filenames right 3 characters are "pdf"?
For Each oFile In oFolder.Files
If Right(LCase(oFile.Name), 3) = "pdf" Then
Data = Replace(oFile.name, ".pdf", "")
Data = Replace(oFile.name, ".PDF", "")
Data = Split(Data, "-")
acct = Data(1)
lob = Data(2)
fileName = clientid & "-" & acct & "-" & lob & "-" & speciesid & "-" & seq & ".pdf"
outputLine = acct & "," & speciesid & "," & lob & "," & oFile.Name & "," & inputDate
oOutFile.WriteLine(outputLine)
End If
Next
You need to put:
Imports Microsoft.VisualBasic
at the beginning of your program. "Right" is a function in this namespace.
http://msdn.microsoft.com/en-us/library/dxs6hz0a(v=vs.80).aspx

Resources