Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


How to generate unique output file name in VBScript function

Posted on 2015-01-29
Medium Priority
Last Modified: 2015-01-29
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)

   set filesys = nothing

End Function

Open in new window

Question by:monkeybiz12345
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 18

Accepted Solution

Simon earned 2000 total points
ID: 40578354
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


Author Comment

ID: 40578733
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

LVL 18

Expert Comment

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

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : All lightning effects with instructions : http://www.mediaf…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question