Create File using VBScript, Powershell and Command Prompt (cmd)

Description

In this article, I am going to give code samples to Create File using VBScript, and commands to Create File in Powershell and Command Line (cmd).

Summary

Create File using VBScript code

   You can create new File easily by using following vbscript, follow the below steps run the VB Script file.

  1. Copy the below example VB Script code and paste it in notepad or a VBScript editor.
  2. Change the value for strDirectory and strFile if you want to give your own path and name otherwise simply leave it.
  3. Save the file with a .vbs extension, for example: CreateFile.vbs
  4. Double-click the vb script file (or Run this file from command window) to Create new File.

Click the following link to get vbscript source code as a file Download CreateFile.vbs

' CreateFile.vbs
' Sample VBScript to Create File .
' Author: https://www.morgantechspace.com/
' ------------------------------------------------------' 

Option Explicit
Dim objFSO, objFolder, objShell, objFile
Dim intAnswer, strDirectory, strFile

strDirectory = "C:\UsersAdministratorDesktop"
strFile = "TestVBS.txt"

' Create the File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Check that the given folder strDirectory is exists or not
If objFSO.FolderExists(strDirectory) Then
   Set objFolder = objFSO.GetFolder(strDirectory)
Else
Set objFolder = objFSO.CreateFolder(strDirectory)
WScript.Echo "New folder '" & strDirectory &"' Created"
End If

If objFSO.FileExists(strDirectory &""& strFile) Then

intAnswer = _
    Msgbox("The file '" & strFile &"' already exists. Do you want to override existing file?",  vbYesNo, "Override File")

If intAnswer = vbYes Then
Set objFile = objFSO.CreateTextFile(strDirectory &""& strFile)
      Wscript.Echo "New File '" & strFile &"' created successfully by VBScript"
Else
     ' nothing will happen
End If

Else
Set objFile = objFSO.CreateTextFile(strDirectory &""& strFile)
      Wscript.Echo "New File '" & strFile &"' created successfully by VBScript"
End If 

If err.number = vbEmpty then
  ' Create File process completed
Else WScript.echo "VBScript Error: " & err.number
End If

WScript.Quit

' End of VBScript to create a file using VBScript

Create File using VBScript by giving File Name as Dynamic Input

   You can create new File easily by using following vbscript by giving File name as dynamic input, follow the below steps run the VB Script file.

   1. Copy the below example VB Script code and paste it in notepad or a VBScript editor.
   2. Change the value for strDirectory, if you want to give your own path otherwise simply leave it.
   3. Save the file with a .vbs extension, for example: CreateFileByDynamicFileName.vbs
   4. Double-click the vb script file (or Run this file from command window) to Create new File.
   5. Then the input window will ask you enter a file name, enter new file name and click OK to create new file.

Click the following link to get vbscript source code as a file Download CreateFileByDynamicFileName.vbs

' CreateFileByDynamicFileName.vbs
' Sample VBScript to Create File .
' Author: https://www.morgantechspace.com/
' ------------------------------------------------------' 

Option Explicit
Dim objFSO, objFolder, objShell, objFile
Dim intAnswer, strDirectory, strFile

strDirectory = "C:\UsersAdministratorDesktop"

Do
   strFile = InputBox ("Please enter file name")
   If strFile = "" then
      Msgbox "No file name entered"
   end if
Loop Until strFile <> ""


' Create the File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Check that the given folder strDirectory is exists or not
If objFSO.FolderExists(strDirectory) Then
   Set objFolder = objFSO.GetFolder(strDirectory)
Else
Set objFolder = objFSO.CreateFolder(strDirectory)
WScript.Echo "New folder '" & strDirectory &"' Created"
End If

If objFSO.FileExists(strDirectory &""& strFile) Then

intAnswer = _
    Msgbox("The file '" & strFile &"' already exists. Do you want to override existing file?",  vbYesNo, "Override File")

If intAnswer = vbYes Then
Set objFile = objFSO.CreateTextFile(strDirectory &""& strFile)
      Wscript.Echo "New File '" & strFile &"' created successfully by VBScript"
Else
     ' nothing will happen
End If

Else
Set objFile = objFSO.CreateTextFile(strDirectory &""& strFile)
      Wscript.Echo "New File '" & strFile &"' created successfully by VBScript"
End If 

If err.number = vbEmpty then
  ' Create File process completed
Else WScript.echo "VBScript Error: " & err.number
End If

WScript.Quit

' End of VBScript to create a file by VBScript

Create File using PowerShell Cmdlet

  You can create new File or Folder using New-Item Cmdlet.

  Use this command to create new file in PowerShell.

 New-Item C:UsersAdministratorDesktoptestfile.txt -type file
Create File in PowerShell Cmdlet

  Use this command to create new Folder in PowerShell.

 New-Item C:UsersAdministratorDesktoptestFolder -type directory

Create File from Command Prompt

   There is no direct command to create new File like mkdir for create new folder. here, I am going to create new file by using the command copy con filename.txt.

Follow the steps to create new file from command line inputs:

   1. First we need to change the current location into new location where we want to create new file. we can achieve this by using command CD.
       Type this command and click Enter.

cd "C:\UsersAdministratorDesktop"

 

   2. And type this command, click Enter

copy con testfile.txt

   3. Then, you can type whatever the text you want to write in newly created file.
   4. Then press Ctrl + Z then release both buttons. This will return a symbol ^Z.
   5. Then click Enter to save new file.

Advertisement

Leave a Comment