Solved

How to generate unique output file name in VBScript function

Posted on 2015-01-29
3
737 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
[X]
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
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

726 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