0

Basically, I want to create a new file and write in it in a directory on the PC, pointed to by the %TEMP% variable. However, the revised code below does not work:

Dim oFile
Dim shell
Set oShell = CreateObject("WScript.Shell")
user = oShell.ExpandEnvironmentStrings("%Temp%")
Set oFile = CreateObject("Wscript.Shell")
Set oFile = oFile.CreateTextFile("%Temp%\d.txt")
oFile.WriteLine "here is my contant"
oFile.Close

Error Message:

run time error
line no: 3
object required

Old Code

Dim fso, tf
Set fso = CreateObject("Scripting.FileSystemObject")
FileName = "%TEMP%\myfile.txt"
Set tf = fso.CreateTextFile(FileName, True)

If I use the file name "C:\myfile.txt" it works fine.

Error Message:

Path not found

Richard Chambers
  • 16,643
  • 4
  • 81
  • 106
mini kumari
  • 13
  • 1
  • 5
  • 1
    Possible duplicate of [Can I pick up environment variables in vbscript WSH script?](https://stackoverflow.com/questions/904739/can-i-pick-up-environment-variables-in-vbscript-wsh-script) – user692942 Oct 15 '18 at 19:36

2 Answers2

3

In VBA, you can just use Environ("TEMP") to expand the Environment variable - if this does not work in VBScript, you may need to bind the WScript.Shell object and use the ExpandEnvironmentStrings property instead, like so:

Set oShell = CreateObject("WScript.Shell")
FileName = oShell.ExpandEnvironmentStrings("%TEMP%") & "\myfile.txt"
Set oShell = Nothing

Following from comments below

Here is a "fully fixed" code:

'Declare variables/objects first
Dim fso AS Object, oFile AS Object
Dim oShell AS Object, FileName AS String

'This bit turns "%TEMP%" into a real file path
Set oShell = CreateObject("WScript.Shell")
FileName = oShell.ExpandEnvironmentStrings("%Temp%\d.txt")
Set oShell = Nothing 'Tidy up the Objects we no longer need

'This bit creates the file
Set fso = CreateObject("Scripting.FileSystemObject")
Set oFile = fso.CreateTextFile(FileName)
oFile.WriteLine "here is my content"
oFile.Close
Set oFile = Nothing 'Tidy up the Objects we no longer need
Set fso = Nothing 'Tidy up the Objects we no longer need
Chronocidal
  • 6,827
  • 1
  • 12
  • 26
  • 2
    `oShell.ExpandEnvironmentStrings("%TEMP%\myfile.txt")` – Ansgar Wiechers Oct 15 '18 at 16:18
  • sir not working :Dim oShell,tf Set oShell = CreateObject("WScript.Shell") FileName = oShell.ExpandEnvironmentStrings("%TEMP%")& "\myfile.txt" Set tf = oShell.CreateTextFile(FileName, True) – mini kumari Oct 15 '18 at 16:20
  • 1
    @minikumari "Doesn't work" is not a valid problem description. How *exactly* does your modified attempt "not work"? Does it still give you an error? A different one? Please edit your question. – Ansgar Wiechers Oct 15 '18 at 18:28
  • @minikumari If you use a `MsgBox` to display `FileName` after you set it, does it display a valid path (navigate there manually?) Also, do you actually have permissions to view/edit the folder, or is it restricted? – Chronocidal Oct 15 '18 at 22:42
  • `Dim oFile Dim shell Set oShell = CreateObject("WScript.Shell") user = oShell.ExpandEnvironmentStrings("%Temp%") Set oFile = CreateObject("Wscript.Shell") Set oFile = oFile.CreateTextFile("%Temp%\d.txt") oFile.WriteLine "here is my contant" oFile.Close` **error in line no: 3 object required.** – mini kumari Oct 16 '18 at 07:20
  • @minikumari You have `Dim shell` instead of `Dim oShell As Object` - the object names don't match, and leaving all of your Variable/Object types defaulting to `Variant` is *very* sloppy coding... (Also, a good practice in 1-line-VisualBasic is to use `:` for a new-line, as VB will actually *read* it as a new-line - the opposite of `_` as "continued on next line") – Chronocidal Oct 16 '18 at 07:26
  • @ Chronocidal Error Message: Run-time error "438" Object doesn't support this property or method – mini kumari Oct 16 '18 at 07:35
  • @minikumari That's because you're creating 2 `WScript.Shell` objects, instead of making the second one `Scripting.FileSystemObject`. Try this: `Dim fso AS Object, oFile AS Object:Dim oShell AS Object, FileName AS String:Set oShell = CreateObject("WScript.Shell"):FileName = oShell.ExpandEnvironmentStrings("%Temp%\d.txt"):Set oShell = Nothing:Set fso = CreateObject("Scripting.FileSystemObject"):Set oFile = fso.CreateTextFile(FileName):oFile.WriteLine "here is my content":oFile.Close:Set oFile = Nothing:Set fso = Nothing` – Chronocidal Oct 16 '18 at 07:36
  • If this is VBScript then this doesn't work, so something is wrong somewhere. – user692942 Oct 16 '18 at 10:25
0

you can use Environ("temp") to write to C:\Users\[username]\AppData\Local\Temp

oxwilder
  • 756
  • 5
  • 14