The best way to prevent this in the future is to use source control. You can create a free account with github or dev.azure.com which will allow you to have a copy of your code online.
https://azure.microsoft.com/en-us/services/devops/?nav=min
Getting started with github
https://learn.microsoft.com/en-us/learn/modules/introduction-to-github/