WSH (VBScript): Windows 7 New File Context Menu Item


/ Published in: Visual Basic
Save to your folder(s)

A modification of Bill James' New Folder context menu script ( http://billsway.com/vbspage/ ), this script instead creates a "New File" item in the right click context menu of Directory Backgrounds. Upon running the New File item, you will be prompted for a filename that defaults to "new.txt". Once a valid filename has been entered (not an existing file) the script will then create the file. This allows the filename to be chosen in the dialog before any file creation has been executed. This has been tested to work on Windows 7 but may work on Vista as well.

Make the code below into a vbs file in the location you want it to be installed in (eg. C:\Windows\NewFile.vbs). To install the context menu item, run the vbs file. To uninstall the file, run the vbs file again.


Copy this code and paste it in your HTML
  1. ' NewFile.vbs - Create right click context menu item for drives and
  2. ' directories (folders) allowing the creation of a new file.
  3. ' To Install or Un-install, double click this file.
  4.  
  5. ' Requires WSH 2.0 +
  6.  
  7. ' Original New Folder script ���© Bill James - [email protected] - rev 14/Nov/2001
  8. ' http://billsway.com/vbspage/
  9.  
  10. ' New File Revision by Karl Horky
  11. ' v0.1 05 April 2010
  12.  
  13. Option Explicit
  14. Dim fso, ws, Args, Title
  15. Set fso = CreateObject("Scripting.FileSystemObject")
  16. Set ws = CreateObject("Wscript.Shell")
  17. Set Args = WScript.Arguments
  18. Title = "Create New File Tool"
  19.  
  20. 'Validate correct version for script.
  21. If WScript.Version < 5.1 Then
  22. ws.Popup "You need Windows Script Host 2.0 + to " & _
  23. "run this script.", , Title, 0 + 48 + 4096
  24. Call Cleanup
  25. End If
  26.  
  27. 'If script called directly, check setup & uninstall.
  28. If Args.Count = 0 Then
  29. Call Setup
  30. End If
  31.  
  32. 'Disable multiple drag and drop
  33. If Args.Count > 1 Then
  34. Call Cleanup
  35. End If
  36.  
  37. Dim ParentFldr
  38. 'If a file was dragged to script, exit
  39. On Error Resume Next
  40. Set ParentFldr = fso.GetFile(Args(0))
  41. If Err.Number = 0 Then
  42. Call Cleanup
  43. End If
  44. Set ParentFldr = Nothing
  45. On Error GoTo 0
  46.  
  47. Call MakeNewFile
  48.  
  49. Call Cleanup
  50.  
  51. Sub MakeNewFile
  52. Dim NewFile, DirectoryPath, NewFilePath
  53. NewFile = InputBox("Name for New File?", Title, "new.txt")
  54. If NewFile = "" Then Call Cleanup
  55. On Error Resume Next
  56. DirectoryPath = fso.GetFolder(Args(0))
  57. If Right(DirectoryPath,1)<>"\" Then DirectoryPath = DirectoryPath & "\"
  58. NewFilePath = DirectoryPath & NewFile
  59. If fso.FileExists(NewFilePath)=true Then
  60. ws.Popup Chr(34) & NewFilePath & Chr(34) & " already exists.", ,Title, 0 + 48 + 4096
  61. Call MakeNewFile
  62. Else
  63. fso.CreateTextFile NewFilePath
  64. If Err.Number = 58 Then
  65. Err.Clear:On Error GoTo 0
  66. ws.Popup Chr(34) & NewFile & Chr(34) & " already exists.", ,Title, 0 + 48 + 4096
  67. Call MakeNewFile
  68. ElseIf Err.Number = 52 Then
  69. Err.Clear:On Error GoTo 0
  70. ws.Popup Chr(34) & NewFile & Chr(34) & " contains invalid character(s).", ,Title, 0 + 48 + 4096
  71. Call MakeNewFile
  72. End If
  73. End If
  74. End Sub
  75.  
  76. Sub Setup
  77. 'Write Reg Data if not existing or if path is invalid.
  78. Dim p
  79. On Error Resume Next
  80. p = ws.RegRead("HKCR\Directory\Background\shell\NewFile (*)\command\")
  81. p = Mid(p, 10, Len(p) - 15)
  82. Err.Clear:On Error GoTo 0
  83. If NOT fso.FileExists(p) Then
  84. If ws.Popup("Do you want to Install the Folder context menu for " & _
  85. "creating a new file?", , Title, 4 + 32 + 4096) <> 6 Then
  86. Call Cleanup
  87. End If
  88. ws.RegWrite "HKCR\Directory\Background\shell\NewFile (*)\","&New File"
  89. ws.RegWrite "HKCR\Directory\Background\shell\NewFile (*)\command\", _
  90. "WScript " & chr(34) & WScript.ScriptFullName & _
  91. chr(34) & " " & chr(34) & "%V" & chr(34)
  92. ws.Popup "Setup complete. Right click on any Directory Background in Windows " & _
  93. "Explorer and select the " & chr(34) & "New File" & chr(34) & _
  94. " option to create a new file there." & vbcrlf & vbcrlf & _
  95. "To Un-install, run this script again.", , Title, 64 + 4096
  96. Else
  97. If ws.Popup("Do you want to Un-install the Folder context menu for " & _
  98. "creating a new file?", , Title, 4 + 32 + 4096) <> 6 Then
  99. Call Cleanup
  100. End If
  101. ws.RegDelete "HKCR\Directory\Background\shell\NewFile (*)\command\"
  102. ws.RegDelete "HKCR\Directory\Background\shell\NewFile (*)\"
  103. ws.Popup "Un-install complete.", , Title, 64 + 4096
  104. End If
  105. Call Cleanup
  106. End Sub
  107.  
  108. Sub Cleanup
  109. Set ws = Nothing
  110. Set fso = Nothing
  111. Set Args = Nothing
  112. WScript.Quit
  113. End Sub

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.