Windows Powershell scripts: Copy without node_modules and .next folders/directories; List node_modules and .next folders

Quick-Info

CopyWoNmnx.ps1 Powershell script that copies contents of a folder/directory excluding node_modules and .next folders. It uses robocopy command to do the main work.

ListNmnx.ps1 Powershell script that lists node_modules and .next folders in a folder/directory. It ignores subfolders within the node_modules or .next folders it finds.

CopyWoNmnx.ps1 Powershell script

My post: Using robocopy to copy folder without node_modules subfolder(s) and using WinMerge and TreeSize to check, Mar. 2024 covers how robocopy can be used to copy folder without node_modules subfolders. Adding .next to node_modules as a subfolder to be skipped in robocopy command is trivial.


CopyWoNmnx.ps1 is a convenience Powershell script that wraps above robocopy command usage. The script copies contents of a folder/directory excluding node_modules and .next folders. It uses robocopy command to do the main work.

Usage: script-name Source-Folder-Name

Output folder name is generated by concatenating script defined suffix (currently: -wo-nmnx) to Source-Folder-Name. The script checks if file/folder of this generated name exists and if so gives a suitable error message and then aborts.
The script also strips trailing backslash from Source-Folder-Name if present before generating output folder name. Note that Powershell adds trailing backslash by default to folder names when tab is used to step through files and folders being specified in a command on console.

ListNmnx.ps1 Powershell script


ListNmnx.ps1 script lists folders/directories with names matching script variable $ListFolders array elements (currently: node_modules and .next), in a user specified folder/directory or current directory. It also provides a total count of the folders it lists.
It ignores subfolders within the matching $ListFolders array elements (node_modules and .next) it finds. It also ignores folders present in script variable $ExcludeFolders array (currently: .git).

Usage: script-name [optional-path] 

The script uses Get-ChildItem calling it recursively.

Notes

These notes were made while writing the above scripts. These notes add to my previous Powershell related post: Windows PowerShell script and commands to list files and folders modified in past x days excluding specified folders like .git and node_modules, May 2024.
...
The following is an interesting post which provides a list all node_modules for command: 
How to Delete ALL node_modules folders on your machine and Free up HD space!, https://medium.com/@MarkPieszak/how-to-delete-all-node-modules-folders-on-your-machine-and-free-up-hd-space-f3954843aeda , Aug. 2019.

FOR /d /r . %d in (node_modules) DO @IF EXIST "%d" echo %d
lists all node_modules directories but they included sub directories within node_modules itself named node_modules. So output list is longer than what I ideally need which is only the first folder having node_modules and ignoring subfolders in node_modules.

This issue prompted me to explore writing ListNmnx.ps1 script.
...
...

Section "Param" in https://ss64.com/ps/syntax-args.html covers Param usage in PS. I could not locate a Param reference page in limited net search. So this article is very useful.







From: Capitalization guidelines (for PowerShell), https://github.com/PoshCode/PowerShellPracticeAndStyle/issues/36 :
  • Global Variable Names: $PascalCase
  • Local Variable Names: $camelCase 
  • Function Names: PascalCase
  • Function/method arguments: PascalCase
----
https://www.manageengine.com/products/desktop-central/returning-error-code-on-scripts-how-to.html states, "$LASTEXITCODE holds the last error code in the powershell script."

'write-host $LASTEXITCODE' or simply, '$LASTEXITCODE' on console prints out exit code of last command executed. 


Demo-Choices.ps1, https://gist.github.com/kpatnayakuni/da1c1d6e4d9b6e457727a9394af5170d shows how to prompt user with a choice in PS.


`n is used in Powershell to append a new line to the standard output.

The use of `n in Powershell is similar to \n in C++ or Java.
-----

Check if Arguments Exists in PowerShell, https://java2blog.com/check-if-arguments-exists-powershell/
==============

Use Write-Host or Write-Output in CopyWoNmnx.ps1 script?

I think Write-Host should be used as the script is simply an interactive front-end to the robocopy command that is executed if user chooses to proceed. If Write-Host is used then redirecting output to log.txt file by '.\copywonmnx.ps1 .\Test\ > log.txt' results in copywonmnx script interactions to be seen on console as is appropriate, and only robocopy output is redirected to log.txt. If Write-Output is used then only $host.UI.PromptForChoice data is shown on console, which I feel is too limited. So I have decided to stick to using Write-Host in CopyWoNmnx.ps1 script.

==================

Use Write-Host or Write-Output in ListNmnx.ps1 script?

The use may want to redirect the main output of this script to a file. So the main output statements should use Write-Output.

But usage message and debugging messages should go only to console and so they should use Write-Host.

I also wanted to get some exposure to Write-Error (writes to error stream) and so am using it for error messages. Note that error stream can be redirected to file by using 2> err.txt or similar. Example of full command redirecting output to log file and error to error file:
.\ListNmnx.ps1 xxx > log.txt 2> err.txt

========================

Variable scoping, recursion, and me - or - How do I accumulate a counter across recursive calls?, https://www.reddit.com/r/PowerShell/comments/agdk2w/variable_scoping_recursion_and_me_or_how_do_i/  ... explains how to increment a variable across recursions.
In my code, $ListFolderOccurrences++ within recursive function did not increment across recursions and final value I got was 0 (its initial value). Using $script:ListFolderOccurrences++ as suggested in a response in above article solved the issue with increment happening across recursions.
...
How to join String array into one String in PowerShell, https://morgantechspace.com/2021/01/how-to-join-string-array-into-one-string-in-powershell.html ... In my code, I used:
$NmnxFolders = $ListFolders -join ","
...
Using Write-Output to write a long string broken into two strings in source file, as one line. This is the code I am using:
"$ListFolderOccurrences folders found matching folder list: $NmnxFolders. " +
  "Note that subfolders within matched folders are ignored." | Write-Output
---
Comma separator results in multiple lines output.

Comments