BoxunloX
asked on
Need a Script to Create Multiple Folders on a Server
Hello all,
I stood up a file server running Server 2008 that I need to create approximately 157 individual folders on the will reside in a "Backups" directory that I have also created. I can't create the folders through a logon script so I need to do it locally on the machine. Does anyone have a script to simply create and name a bunch of folders?
I stood up a file server running Server 2008 that I need to create approximately 157 individual folders on the will reside in a "Backups" directory that I have also created. I can't create the folders through a logon script so I need to do it locally on the machine. Does anyone have a script to simply create and name a bunch of folders?
One last not. If you don't want to specify a directory to create the folder in and just a list of folders you could do that as well and just copy and run the script in the directory you want the folder to be created in. So if you copied all 3 files to C:\temp\ and use the below .csv file it would create:
C:\temp\folder1\
C:\temp\folder2\
C:\temp\folder3\
C:\temp\folder4\
C:\temp\folder5\
Create a file and put the below code in (name the file folder.csv)
C:\temp\folder1\
C:\temp\folder2\
C:\temp\folder3\
C:\temp\folder4\
C:\temp\folder5\
Create a file and put the below code in (name the file folder.csv)
folder1,
folder2,
folder3,
folder4,
folder5
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I found a simple one-line command that did exactly what I was looking for.
Easy enough to do in a simple Batch script (BAT) using the MD command, like:
md c:\dir1
md c:\dir2
. . .
my question is, is there any logic to the names of the folders that you need to create, or is it a list in a txt file or something. If so we could enhance the BAT script to be smarter and save work. But need to better understand the set of folders you need to create.
~bp
md c:\dir1
md c:\dir2
. . .
my question is, is there any logic to the names of the folders that you need to create, or is it a list in a txt file or something. If so we could enhance the BAT script to be smarter and save work. But need to better understand the set of folders you need to create.
~bp
For Something like that I would just use something like Excel and build a quick and dirty batch file
Final ColB ColC ColD
MD c:\backup\Folder1\ MD c:\backup\ Folder1\
Column A use the formula =+b1&C1&D1
in Column b type MD plus a space
in column c your master Backup Path
Col D is where you put the folder names you want
Fill in this list first
Then Copy Contents of A1, b1 and C1 down to fill in the three column til you have everything covered
Then do File SaveAs - Type TXT - Name it Myfolders.txt
Close the excel Spreadsheet then open the Myfolders.txt
The formulas will have changed to the real words - just delete column B, C &D
Then resave the file - Type txt to Folder.txt
Put it in the root folder of C:\
then in a command box (Run CMD) type ren folders.txt *.bat
then just type myfolders and the folders will be created
Final ColB ColC ColD
MD c:\backup\Folder1\ MD c:\backup\ Folder1\
Column A use the formula =+b1&C1&D1
in Column b type MD plus a space
in column c your master Backup Path
Col D is where you put the folder names you want
Fill in this list first
Then Copy Contents of A1, b1 and C1 down to fill in the three column til you have everything covered
Then do File SaveAs - Type TXT - Name it Myfolders.txt
Close the excel Spreadsheet then open the Myfolders.txt
The formulas will have changed to the real words - just delete column B, C &D
Then resave the file - Type txt to Folder.txt
Put it in the root folder of C:\
then in a command box (Run CMD) type ren folders.txt *.bat
then just type myfolders and the folders will be created
Try this vbScript
' One absolute path per line
On Error Resume Next
Dim arrFolderName(200), i, objArgs, objFSO, strNextLine, x
Const ForReading = 1
Set objArgs = WScript.Arguments
Set objFSO = CreateObject("Scripting.Fi leSystemOb ject")
'''''''''''''''''''''''''' '''''''''' '''''''''' '''''''''' '''''''''' '''''''''' '''''''''' ''''''''''
' Check if inputfile exists
'''''''''''''''''''''''''' '''''''''' '''''''''' '''''''''' '''''''''' '''''''''' '''''''''' ''''''''''
if not (objFSO.FileExists(objArgs (0))) Then
wscript.echo "Can't find "& objArgs(0) & vbcrlf & "This file must reside in the same dir as this script."
WScript.Quit(1)
end If
'''''''''''''''''''''''''' '''''''''' '''''''''' '''''''''' '''''''''' '''''''''' '''''''''' ''''''''''
' Read input file into an array
'''''''''''''''''''''''''' '''''''''' '''''''''' '''''''''' '''''''''' '''''''''' '''''''''' ''''''''''
Set objTextFile = objFSO.OpenTextFile(objArg s(0), ForReading)
i = 0
Do While objTextFile.AtEndOfStream <> True
strNextLine = objTextFile.Readline
If(Len(strNextLine)<>0)The n 'If line isn't null. More error checking maybe needed.
arrFolderName(i) = strNextLine
WScript.Echo i & "=" & strNextLine
End If
i = i + 1
Loop
objTextFile.close
'''''''''''''''''''''''''' '''''''''' '''''''''' '''''''''' '''''''''' '''''''''' '''''''''' ''''''''''
' Main loop - Create the folders if they don't exist
'''''''''''''''''''''''''' '''''''''' '''''''''' '''''''''' '''''''''' '''''''''' '''''''''' ''''''''''
For x = 0 To i - 1
If Not objFSO.FolderExists(arrFol derName(x) ) Then
Set objFolder = objFSO.CreateFolder(arrFol derName(x) )
Else
WScript.Echo arrFolderName(x) & " already exists."
End If
Next
Set objFSO = Nothing
WScript.Quit
' Usage: cscript CreateFolders.vbs inputfile.csv' One absolute path per line
On Error Resume Next
Dim arrFolderName(200), i, objArgs, objFSO, strNextLine, x
Const ForReading = 1
Set objArgs = WScript.Arguments
Set objFSO = CreateObject("Scripting.Fi
''''''''''''''''''''''''''
' Check if inputfile exists
''''''''''''''''''''''''''
if not (objFSO.FileExists(objArgs
wscript.echo "Can't find "& objArgs(0) & vbcrlf & "This file must reside in the same dir as this script."
WScript.Quit(1)
end If
''''''''''''''''''''''''''
' Read input file into an array
''''''''''''''''''''''''''
Set objTextFile = objFSO.OpenTextFile(objArg
i = 0
Do While objTextFile.AtEndOfStream <> True
strNextLine = objTextFile.Readline
If(Len(strNextLine)<>0)The
arrFolderName(i) = strNextLine
WScript.Echo i & "=" & strNextLine
End If
i = i + 1
Loop
objTextFile.close
''''''''''''''''''''''''''
' Main loop - Create the folders if they don't exist
''''''''''''''''''''''''''
For x = 0 To i - 1
If Not objFSO.FolderExists(arrFol
Set objFolder = objFSO.CreateFolder(arrFol
Else
WScript.Echo arrFolderName(x) & " already exists."
End If
Next
Set objFSO = Nothing
WScript.Quit
' Main script
''''''''''''''''''''''''''
filepath = " C:\Backups\ "
call Create_folder(filepath)
''''''''''''''''''''''''''
' Function to Create 157 folders, parameter should be filepath'
''''''''''''''''''''''''''
Function Create_folder(filepath)
Set fso = createobject("scripting.fi
For i = 1 to 157
fso.createfolder filepath&"Folder"&i
next
set fso = nothing
End Function
The script uses a CSV input file and will create as many or few folder as possible. You just input the full path you want the folder created at and your good to go. The .bat is optional, but makes it easier to run the script. Hope this helps if no one provides any VB for you.
Create a file and put the below code in (name the file create_folders.pl)
Open in new window
Create a file and put the below code in (name the file folder.csv)
Open in new window
Create a file and put the below code in (name the file create_folders.pl)
Open in new window