Solved

How to generate unique output file name in VBScript function

Posted on 2015-01-29
3
668 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:
Simon earned 500 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

0
 

Author Comment

by:monkeybiz12345
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

0
 
LVL 18

Expert Comment

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

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Vb script to unzip a files and rename the files 12 108
Check version 13 75
VBS Script not working correctly. 1 41
problems with mysql ODBC Connector and vbscript 4 31
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…
This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

810 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