Create a Folder from an Environmental Variable (VBS)

Purpose: Create a folder, which name is received from an environmental variable.

You might need to create a folder (deliver a file, get the string, etc.) based on a custom environmental variable.  In my scenario, I had an installation done differently across the domain. The only way to deliver a file to the installation directory was to either check which folder exists in the workstation (and hope you found all the permutations) or use the environmental variable (that was same in each workstation). I chose the environmental variable. In this example, a usually existing environmental variable %WINDIR% (the location of the windows installation) is used.

Usage: cscript.exe IT_APP_VBS_CreateFolderFromEnvironmentalVariable (no parameters exist)

Configuration: You need to change the folder location and environmental variable to match.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
' *********************************************************************************
' ** Script Name: IT_APP_VBS_CreateFolderFromEnvironmentalVariable
' ** Created on: 2.1.2013
' ** Author: Jyri Lehtonen / http://it.peikkoluola.net
' **
' ** Purpose: Create a folder, which name is received from an environmental variable
' ** Usage: IT_APP_VBS_CreateFolderFromEnvironmentalVariable (no parameters exist)
' **
' ** License: This program is free software: you can redistribute it and/or modify
' ** it under the terms of the GNU General Public License as published by
' ** the Free Software Foundation, either version 3 of the License, or
' ** (at your option) any later version.
' **
' ** This program is distributed in the hope that it will be useful,
' ** but WITHOUT ANY WARRANTY; without even the implied warranty of
' ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' ** GNU General Public License for more details.
' ** 
' ** History: 
' ** 1.0 / Jyri Lehtonen / 2.1.2013 / Initial version.
' ** 1.1 / Jyri Lehtonen / 1.4.2013 / Created better configuration options.
' *********************************************************************************
 
Set wshShell = CreateObject( "WScript.Shell" )
 
'Get the Environmental Variables value 
TargetDir = wshShell.ExpandEnvironmentStrings( "%WINDIR%" )
 
'Create a folder
set filesys=CreateObject("Scripting.FileSystemObject")
If  Not filesys.FolderExists(TargetDir) Then
   filesys.CreateFolder(TargetDir)
End If
' *********************************************************************************
' ** Script Name: IT_APP_VBS_CreateFolderFromEnvironmentalVariable
' ** Created on: 2.1.2013
' ** Author: Jyri Lehtonen / http://it.peikkoluola.net
' **
' ** Purpose: Create a folder, which name is received from an environmental variable
' ** Usage: IT_APP_VBS_CreateFolderFromEnvironmentalVariable (no parameters exist)
' **
' ** License: This program is free software: you can redistribute it and/or modify
' ** it under the terms of the GNU General Public License as published by
' ** the Free Software Foundation, either version 3 of the License, or
' ** (at your option) any later version.
' **
' ** This program is distributed in the hope that it will be useful,
' ** but WITHOUT ANY WARRANTY; without even the implied warranty of
' ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' ** GNU General Public License for more details.
' ** 
' ** History: 
' ** 1.0 / Jyri Lehtonen / 2.1.2013 / Initial version.
' ** 1.1 / Jyri Lehtonen / 1.4.2013 / Created better configuration options.
' *********************************************************************************

Set wshShell = CreateObject( "WScript.Shell" )

'Get the Environmental Variables value 
TargetDir = wshShell.ExpandEnvironmentStrings( "%WINDIR%" )

'Create a folder
set filesys=CreateObject("Scripting.FileSystemObject")
If  Not filesys.FolderExists(TargetDir) Then
   filesys.CreateFolder(TargetDir)
End If

 

You might need to modify the received string. In a case, where you know the naming template, you can simply remove characters from the LEFT or the RIGHT side of the string to modify the string to your needs.

The following example will reduce the original string C:\Windows to C:\

1
2
3
4
5
6
' IF YOU NEED TO MODIFY THE RECEIVED STRING (From the left side)
' *********************************************************************************
Set wshShell = CreateObject( "WScript.Shell" )
TargetDir = wshShell.ExpandEnvironmentStrings( "%WINDIR%" )
TargetDir = LEFT(TargetDir, (LEN(TargetDir)-7))
WScript.echo TargetDir
' IF YOU NEED TO MODIFY THE RECEIVED STRING (From the left side)
' *********************************************************************************
Set wshShell = CreateObject( "WScript.Shell" )
TargetDir = wshShell.ExpandEnvironmentStrings( "%WINDIR%" )
TargetDir = LEFT(TargetDir, (LEN(TargetDir)-7))
WScript.echo TargetDir

 

The following example will reduce the original string c:\Windows to Windows

1
2
3
4
5
6
' IF YOU NEED TO MODIFY THE RECEIVED STRING (From the right side)
' *********************************************************************************
Set wshShell = CreateObject( "WScript.Shell" )
TargetDir = wshShell.ExpandEnvironmentStrings( "%WINDIR%" )
TargetDir = RIGHT(TargetDir, 7)
WScript.echo TargetDir
' IF YOU NEED TO MODIFY THE RECEIVED STRING (From the right side)
' *********************************************************************************
Set wshShell = CreateObject( "WScript.Shell" )
TargetDir = wshShell.ExpandEnvironmentStrings( "%WINDIR%" )
TargetDir = RIGHT(TargetDir, 7)
WScript.echo TargetDir

Leave a Reply

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


+ four = eight

Post Navigation