Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1258
  • Last Modified:

How to generate unique output file name in VBScript function

Greetings Experts!

I have a function that outputs a text file that contains data for printing a label.  This function works fine but I need to modify it and I'm just not getting it.

Currently, the code specifies a path and file name on the local machine to output.  I need to have the code generate a file name each time it runs and write this file to a network location without using a mapped drive (e.g. \\MYSERVER\MyFolder1\MyFolder2\).

I found some info about .GetTempName and have been trying to use that but simple experiments like inserting the following into my code at line 15 just to see if I can get a file name generated fail with the "Object Required (424") error.  

 ' Generate a filename for the output file
   Set strFileName = filesys.GetTempName
   msgbox "Generated file name is " & strFileName

The end result I want is this:

Instead of outputting the text file c:\temp\license_plate.txt, I want to output the text file \\MYSERVER\MyFolder1\MyFolder2\<randomfilename>.txt

Can someone please help me get from here to there?

Many thanks!



' create text file for Bartender-produced pallet license plate
Function WriteFileLicensePlate()

   Dim strRecDate, strRotationCode, BBDate, strBBDate

      strRecDate = Mid(PostJobReceivingDetails.CodeObject.ReceiptDate,5,2) & "-" & Right(PostJobReceivingDetails.CodeObject.ReceiptDate,2) & "-" & Left(PostJobReceivingDetails.CodeObject.ReceiptDate, 4)
          strRotationCode = Mid(PostJobReceivingDetails.CodeObject.ReceiptDate,4,1) & Mid(PostJobReceivingDetails.CodeObject.ReceiptDate,5,2)
      BBDate = DateAdd("m",16,strRecDate)


   ' Output info for printing Pallet ID tag to a comma/quote delimited text file
   Dim filesys, testfile, strFileName

   Set filesys = CreateObject("Scripting.FileSystemObject")

   ' For Reference: OpenTextFile parameters:  <filename>, IOMode (1=Read,2=write,8=Append), Create (true,false), Format (-2=System Default,-1=Unicode,0=ASCII)
   Set testfile= filesys.CreateTextFile("c:\temp\license_plate.txt", True)

   testfile.Write Chr(34) & PostJobReceivingDetails.CodeObject.StockCode & Chr(34) & ","
   testfile.Write Chr(34) & PostJobReceivingDetails.CodeObject.StockDescription & Chr(34) & ","
   testfile.Write Chr(34) & PostJobReceivingDetails.CodeObject.QtyReceived & Chr(34) & ","
   testfile.Write Chr(34) & PostJobReceivingDetails.CodeObject.PalletNumber & Chr(34) & ","
   testfile.Write Chr(34) & PostJobReceivingDetails.CodeObject.FPNumber & Chr(34) & ","
   testfile.Write Chr(34) & strRecDate & Chr(34)& ","
   testfile.Write Chr(34) & strRotationCode & Chr(34)& ","
   testfile.Write Chr(34) & PostJobReceivingDetails.CodeObject.Customer & Chr(34)& ","
   testfile.Write Chr(34) & BBDate & Chr(34)

   testfile.Close
   set filesys = nothing

End Function

Open in new window

0
monkeybiz12345
Asked:
monkeybiz12345
  • 2
1 Solution
 
SimonCommented:
You just need to omit the word 'Set' from this line
Set strFileName = filesys.GetTempName
 should be
strFileName = filesys.GetTempName
 because GetTempName is a string value that you can't assign to an object variable.

Try this example for starters...
Set filesys = CreateObject("Scripting.FileSystemObject")
Set tempfolder = filesys.GetSpecialFolder(2)
tempname = filesys.GetTempName
Set tempfile = tempfolder.CreateTextFile(tempname)

Open in new window

Creating the file in a UNC path (what you really want to do) should also work...
Set filesys = CreateObject("Scripting.FileSystemObject")
tempname = filesys.GetTempName
tempname=left(tempname,len(tempname)-4) 'trim the .tmp off if required.
Set tempfile = CreateTextFile("\\MYSERVER\MyFolder1\MyFolder2\"& tempname & ".txt")

Open in new window

0
 
monkeybiz12345Author Commented:
This is exactly what I needed. Thank you!  And thank you for your comment about not being able to assign a string value to an object variable.  That's the piece that kept eluding me.

I made a small change to the code to get it to work right for me...

Set filesys = CreateObject("Scripting.FileSystemObject")
tempname = filesys.GetTempName
tempname=left(tempname,len(tempname)-4) 'trim the .tmp off if required.
Set tempfile = filesys.CreateTextFile("\\MYSERVER\MyFolder1\MyFolder2\"& tempname & ".txt")

Open in new window

0
 
SimonCommented:
Glad to help. Sorry about missing the filesys. from the final line. I only tested the tempdir version.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now