1

I want to make a VBScript create a text file the downloads folder but I can't figure out how to do it. Any help?

  • 1
    This may help you:- https://stackoverflow.com/questions/2198810/creating-and-writing-lines-to-a-file – Abhishek Dutt Mar 21 '21 at 09:11
  • If you're working with `WScript`, try this: [https://stackoverflow.com/questions/9465278/vbscript-how-to-check-if-txt-file-exists-and-whe-not-create-empty-one](https://stackoverflow.com/questions/9465278/vbscript-how-to-check-if-txt-file-exists-and-whe-not-create-empty-one) – Stylerr. Mar 23 '21 at 22:51

2 Answers2

1

You can get the Temporary folder by this way :

Set ws = CreateObject("WScript.Shell")
TempFolder = ws.ExpandEnvironmentStrings("%Temp%")

And the entire vbscript can be written as below to create a text file into the temporary folder :

Option Explicit
Dim ws,fso,TempFolder,FileFullPath,FileName,objFile
Set ws = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
TempFolder = ws.ExpandEnvironmentStrings("%Temp%")
FileName = "testfile.txt"
FileFullPath = TempFolder & "\" & FileName
wscript.echo "The Temparary folder is located : " & vbCrlf & TempFolder
wscript.echo "The Full Path of the text file is : " & vbCrlf & FileFullPath
Set objFile = fso.CreateTextFile(FileFullPath, True)
' Write something into text file
objFile.WriteLine("This is a test.")
objFile.Close
' If we want to open the text file created with Notepad
ws.run "Notepad " & FileFullPath

The download folder can be found here :

Set ws = CreateObject("WScript.Shell")
DownloadFolder = ws.ExpandEnvironmentStrings("%userprofile%") & "\Downloads"
wscript.echo DownloadFolder

And the entire script for the creation of a text file in the Download folder :

Option Explicit
Dim ws,fso,DownloadFolder,FileFullPath,FileName,objFile
Set ws = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
DownloadFolder = ws.ExpandEnvironmentStrings("%userprofile%") & "\Downloads"
FileName = "testfile.txt"
FileFullPath = DownloadFolder & "\" & FileName
wscript.echo "The Download folder is located : " & vbCrlf & DownloadFolder
wscript.echo "The Full Path of the text file is : " & vbCrlf & FileFullPath
Set objFile = fso.CreateTextFile(FileFullPath, True)
' Write something into text file
objFile.WriteLine("This is a test.")
objFile.Close
' If we want to open the text file created with Notepad
ws.run "Notepad " & FileFullPath
Hackoo
  • 18,337
  • 3
  • 40
  • 70
-1

Create Text File in Documents

Option Explicit

Const FileName = "New.txt"

Dim wss: Set wss = CreateObject("WScript.Shell")
Dim FolderPath: FolderPath = wss.SpecialFolders("MyDocuments")

Dim fso: Set fso = CreateObject("Scripting.FileSystemObject")
Dim FilePath: FilePath = fso.BuildPath(FolderPath, FileName)
Dim fsoFile:Set fsoFile = fso.CreateTextFile(FilePath, True)
fsoFile.Close

Get Documents Folder Path

Option Explicit

Dim wss: Set wss = CreateObject("WScript.Shell")
Dim FolderPath: FolderPath = wss.SpecialFolders("MyDocuments")

MsgBox "Your documents folder is located in '" _
    & Folderpath & "'.", vbInformation, "Documents"

Dim RunString: RunString = "explorer.exe /e," & FolderPath
wss.Run RunString
VBasic2008
  • 44,888
  • 5
  • 17
  • 28