Search code examples
vbscriptwscript.shell

vbscript output to text on wndows startup


I'm looking for vbscript that do the following tasks

Script Tasks

  1. execute on startup of the computer,
    the way is being executed is via putting it in startup folder of windows in C:\Documents and Settings\Admin\Start Menu\Programs\Startup

  2. if output text file is exist, write down some text and exit

  3. if the text file is not exist then it echo out full destination

the code is as provided, any help would be appreciated

'create txt.vbs
'vbscript
Set WshShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
CurrentDirectory = objFSO.GetAbsolutePathName(".")
FilePath = CurrentDirectory & "\test.txt"

Existcode = objFSO.FileExists(FilePath)
' wscript.echo "FileExists code:" & Existcode

if Existcode = False then
    Existcode = objFSO.FileExists(FilePath)
    'for debugging
    wscript.echo "file not exist" & vbCrLf _
    & "FileExists code:" & Existcode

    Set objFile = objFSO.CreateTextFile(FilePath,True)
    strtext = "file created:" & vbCrLf & chr(34) & "New Line" & chr(34)
    objFile.Write strtext & vbCrLf
    objFile.Close
else
    'for debugging
    wscript.echo "file exist" & vbCrLf _
    & "FileExists code:" & Existcode & vbCrLf & vbCrLf _
    & FilePath & vbCrLf _
    & CurrentDirectory & vbCrLf
end if
wscript.echo "end"

when get executed by clicking on it, either via a batch file, the script works with no errors and the output is as expected

while it's executed from startup folder by windows, it show all echo that i set for debugging but doesn't create the output file neither write to text in it, but also it read it as exist i'm not sure why


Solution

  • the vbscript cannot write text to startup folder, however changes are the following

    CurrentDirectory = objFSO.GetAbsolutePathName(".")
    FilePath = CurrentDirectory & "\test.txt"
    

    become

    DesktopDirectory = WshShell.SpecialFolders("Desktop")
    FilePath = DesktopDirectory & "\test.txt"