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:

01  $FolderPath=Get-Location
02 $FolderPathString=$FolderPath.ToString()
03 $NameProject=Read-Host 'Type your project name'
04 New-Item -Name $NameProject -ItemType directory
05 $NewFolderPatch=$FolderPathString+'\'+$NameProject
06 #Schematic
07 $FilePath=$NewFolderPatch+'\'+$NameProject+'.dch'
08 Copy-Item D:\Projekty_PCB\DipTrace\Projekty\Template\Template.dch $FilePath
09 #PCB Project
10 $FilePath=$NewFolderPatch+'\'+$NameProject.Substring(0,$NameProject.Length-2)+'.dip'
11 Copy-Item D:\Projekty_PCB\DipTrace\Projekty\Template\Template.dip $FilePath
12 #BOM
13 $FilePath=$NewFolderPatch+'\'+$NameProject+'_BOM.csv'
14 Copy-Item D:\Projekty_PCB\DipTrace\Projekty\Template\Template.csv $FilePath
15 #Output folder
16 $FileName=$NameProject+'_Output'
17 New-Item -Name $FileName -ItemType directory -Path $NewFolderPatch
18 #Simulations folder
19 $FileName=$NameProject+'_Simulations'
20 New-Item -Name $FileName -ItemType directory -Path $NewFolderPatch
21 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:

01 $FolderPath=Get-Location

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

02 $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.

03 $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”

04 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”.

05 $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”.

06 #Schematic

It’s just a comment.

07 $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.

08 Copy-Item D:\Projekty_PCB\DipTrace\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.

09 #PCB Project
10 $FilePath=$NewFolderPatch+‘\’+$NameProject.Substring(0,$NameProject.Length-2)+‘.dip’
11 Copy-Item D:\Projekty_PCB\DipTrace\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.

12 #BOM
13 $FilePath=$NewFolderPatch+‘\’+$NameProject+‘_BOM.csv’
14 Copy-Item D:\Projekty_PCB\DipTrace\Projekty\Template\Template.csv $FilePath

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

15 #Output folder
16 $FileName=$NameProject+‘_Output’
17 New-Item -Name $FileName -ItemType directory -Path $NewFolderPatch

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

18 #Simulations folder
19 $FileName=$NameProject+‘_Simulations’
20 New-Item -Name $FileName -ItemType directory -Path $NewFolderPatch

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

21 cmd /c pause |out-null

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

Leave a Reply

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