Creation of files in a given folder programmatically using Visual Basic Script (VBScript)

If a user must create, for instance, 20 files with names 1.tex, 2.tex, 3.tex, …, 20.tex, he will probably do it manually. Even the use of Total Commander with its hot key Shift+F4 for creation of files will not save you from the boring and stupid work. But a programmer is not a user and you can write the following Visual Basic Script (VBScript) with file name, for example, “CreateFilesScript.vbs”.

———————————————————————————

'(C) Author: Denis Kutlubaev, Alwawee Company, Moscow, 2011
'This script creates files with names 25.tex to 36.tex in a given folder
'folderPath - path to folder where files must be created
'fileName - generated file name
'filePathAndName - generated full path and file name
'start and stopIndex - numbers to start and stop file naming and creation

Dim folderPath, fileName, filePathAndName
Dim fileNumber, i
Set FSO = CreateObject("Scripting.FileSystemObject")

folderPath = "c:\Documents and Settings\wzbozon\Рабочий стол\LaTeX\Lessons\"

For i = 25 To 36
fileNumber = i
fileName = CStr(fileNumber) & ".tex"
filePathAndName = folderPath & fileName
FSO.CreateTextFile(filePathAndName)
Next

———————————————————————————

It is noteworthy that in Visual Basic Script there are some restrictions. For example, the loop

For i = 25 To 36

cannot be unhardcoded.

Also, you cannot define step for loop, it is always 1.

In this line

fileName = CStr(fileNumber) & ".tex"

there is a concatenation of a string, produced by CStr operator from a number and a file extension.

If you write a VBScript, you will wish to debug it. To debug it, write in a command line:
wscript //X CreateFilesScript.vbs

After this you can choose Visual Studio as a default debugger for VBScript and you will catch the line, where the script fails. Unfortunately, without much information about the bug.

Some useful links:

  1. http://msdn.microsoft.com/en-us/library/te2585xw(v=vs.80).aspx – Concatenation of strings in VBScript
  2. http://www.tizag.com/vbscriptTutorial/vbscriptforloop.php – Loops in VBScript
  3. http://www.script-coding.com/ – Good source for script writing (in Russian)

Leave a Reply

Your email address will not be published. Required fields are marked *