Solved

How to generate unique output file name in VBScript function

Posted on 2015-01-29
3
601 Views
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)

   testfile.Close
   set filesys = nothing

End Function

Open in new window

0
Comment
Question by:monkeybiz12345
  • 2
3 Comments
 
LVL 18

Accepted Solution

by:
SimonAdept earned 500 total points
Comment Utility
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
 

Author Comment

by:monkeybiz12345
Comment Utility
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
 
LVL 18

Expert Comment

by:SimonAdept
Comment Utility
Glad to help. Sorry about missing the filesys. from the final line. I only tested the tempdir version.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now