Simple Windows PowerShell File Commands

Working with files is a big part of being a chess engine enthusiast. Here are a few commands that will help.

First a couple that don’t involve the Windows PowerShell.

  • You may be familiar with how you can use CTRL+V to paste the latest addition to the clipboard. But you can also use WIN+V to open a list of the last ten things you put on the clipboard, and you can use the mouse to choose which one. This means if you need to copy and paste three things, you can use CTRL+C on all three, then WIN+V to select them each.
  • If you want to combine all the PGNs in a directory (for example) you would click to the right of the path in the Windows Explorer address bar, and type in cmd to replace it. This brings up the Command Prompt, in which you type copy *pgn filename.pgn …. though of course you would use your own filename.

That same Command Prompt copy command can be run in the PowerShell as well:

cmd.exe /c copy *pgn filename.pgn

or it can be run directly:

Get-Content -Path "C:\Path\To\Directory*" | Set-Content -Path "C:\Path\To\Output\combined_file.txt"

That last command takes all the files in a directory, as before, and puts them all together. Each time you run this, you would overwrite the output file. To append to it instead, use:

Get-Content -Path "C:\Path\To\Directory*" | Add-Content -Path "C:\Path\To\Output\combined_file.txt"

So it’s the exact same, except that you use Add-Content instead of Set-Content.

To zip all the files in a directory, use:

Get-ChildItem *.pgn | ForEach-Object {Compress-Archive -Path $_.FullName -DestinationPath ($_.BaseName + '.zip')}

To instead zip all the folders in a directory, use:

Get-ChildItem -Directory | ForEach-Object {Compress-Archive -Path $_.FullName -DestinationPath ($_.Name + '.zip')}

Leave a Reply

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