Script generating project files

I mentioned in one of my posts about how I call my projects. It’s about naming folders, schemes, PCB designs and BOMs (Bill Of Materials). I used to do everything manually by myself. I created a folder, created the schema as a new one and saved it under the proper name. I did the same with other files and folders.

However, one day I came to the conclusion that it may be worth automating this process. I thought maybe it would be worth to write a script that, after giving the name of the project, will create all files and folders. I choose a PowerShell, because I personally use Windows, and in addition to the script do not need to install any additional programs or libraries. Everything is already installed with the installation of Windows.

Creating new files or copying?

I use DipTrace software to draw diagrams and PCB design. At first, I wanted the newly created files to be created by the script. As for the schema file (* .dch), it was not a problem. The problem appeared with the PCB project file (* dip). Well, after the file was created by the script (“project_name.dip”), DipTrace could not open such a file. He reported an error when opening. Finally, I decided to create a folder with file templates for the project. The folder is located in the main folder with projects.

Folder with templates
Files to copy

Instead of creating files, the script copies them, at the same time changing their name to the name given earlier in the script.

Script

Let’s look at what the script looks like:

$FolderPath=Get-Location
$FolderPathString=$FolderPath.ToString()
$NameProject=Read-Host 'Type your project name'
New-Item -Name $NameProject -ItemType directory
$NewFolderPatch=$FolderPathString+'\'+$NameProject
#Schematic
$FilePath=$NewFolderPatch+'\'+$NameProject+'.dch'
Copy-Item D:\SynologyDrive\Projekty_PCB\Projekty\Template\Template.dch $FilePath
#PCB Project
$FilePath=$NewFolderPatch+'\'+$NameProject.Substring(0,$NameProject.Length-2)+'.dip'
Copy-Item D:\SynologyDrive\Projekty_PCB\Projekty\Template\Template.dip $FilePath
#BOM
$FilePath=$NewFolderPatch+'\'+$NameProject+'_BOM.csv'
Copy-Item D:\SynologyDrive\Projekty_PCB\Projekty\Template\Template.csv $FilePath
#Output folder
$FileName=$NameProject+'_Output'
New-Item -Name $FileName -ItemType directory -Path $NewFolderPatch
#Simulations folder
$FileName=$NameProject+'_Simulations'
New-Item -Name $FileName -ItemType directory -Path $NewFolderPatch

cmd /c pause |out-null

How to use the script?

Personally, I work on Windows 10. I will describe how to work with the script on this system. First, it’s best to run “Windows PowerShell ISE”.

Windows PowerShell ISE

Then paste the copied script that I presented earlier. After that, save the script as * .ps1 file. This is an extension of files with the PowerShell script. The file name is arbitrary.

Alternatively, you can create a plain text file, and save it with the extension * .ps1. Then open the file in any text editor, paste the script I’ve provided before and save it.

And now the most important: the script will create project files and folders in the place where it will be called.
This means that before creating files, we need to move our script file (* .ps1) to the place where we want to create our project.

Once the script has been placed in the right place, right click and select “Run with PowerShell” from the menu.

The script will run. The script will ask us for the name of the project. We give it and click Enter.

After that, all files will be created.

To close the script, press Enter again.

Folder with project files
Project files

How does the script work?

Let’s look at the lines of the script one by one:

$FolderPath=Get-Location

“$FolderPath” is a variable in which the script will store the path to the disk from which the script was run.

$FolderPathString=$FolderPath.ToString()

The .ToString() function changes the access path stored in the variable to a text value and stores it in the “$FolderPathString” variable.

$NameProject=Read-Host 'Type your project name'

A message will be displayed asking for the name of the project. After entering the name, this name is saved in the variable “$NameProject”

New-Item -Name $NameProject -ItemType directory

A folder is created with the same name as the one given earlier and stored in the variable “$NameProject”.

$NewFolderPatch=$FolderPathString+'\'+$NameProject

The next files will be created in the previously created folder, therefore in this line we create a new path to the file and store it in the variable “$NewFolderPatch”.

#Schematic

It’s just a comment.

$FilePath=$NewFolderPatch+'\'+$NameProject+'.dch'

We prepare the path to create an electronic circuit diagram, along with the name of the new file compatible with the project.

Copy-Item D:\SynologyDrive\Projekty_PCB\Projekty\Template\Template.dch $FilePath

The file from the folder with templates is copied (the folder with the templates does not change position, that is why the path is directly to the folder on the disk) to the folder with the project files. In addition, the variable “$FilePatch”, also change the file name.

#PCB Project
$FilePath=$NewFolderPatch+'\'+$NameProject.Substring(0,$NameProject.Length-2)+'.dip'
Copy-Item D:\SynologyDrive\Projekty_PCB\Projekty\Template\Template.dip $FilePath

The script copies the PCB design template, renames it and copies it to the project folder. The design revision number is omitted in the name.

#BOM
$FilePath=$NewFolderPatch+'\'+$NameProject+'_BOM.csv'
Copy-Item D:\SynologyDrive\Projekty_PCB\Projekty\Template\Template.csv $FilePath

The script copies the BOM.csv template, renames it and copies it to the project folder.

#Output folder
$FileName=$NameProject+'_Output'
New-Item -Name $FileName -ItemType directory -Path $NewFolderPatch

The script creates a folder for storing files for the PCB manufacturer.

#Simulations folder
$FileName=$NameProject+'_Simulations'
New-Item -Name $FileName -ItemType directory -Path $NewFolderPatch

A folder is created in which simulations of electronic circuits will be stored.

cmd /c pause |out-null

This line causes the script window not to be closed when it is finished.