Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
powershell:qnd:file_operations [2022/04/05 00:35] – mgupton | powershell:qnd:file_operations [2022/10/06 20:13] (current) – mgupton | ||
---|---|---|---|
Line 8: | Line 8: | ||
~\Desktop | ~\Desktop | ||
+ | </ | ||
+ | |||
+ | === User Split-Path to get folder name === | ||
+ | <code powershell> | ||
+ | cd $(Split-Path $PROFILE) | ||
</ | </ | ||
====== Common Commands By Example ====== | ====== Common Commands By Example ====== | ||
+ | === List Items with Filter/ | ||
+ | Example of finding a file somewhere in the filesystem and outputting the full path and filename. | ||
+ | <code powershell> | ||
+ | Get-ChildItem -Recurse -Filter " | ||
+ | </ | ||
+ | |||
+ | Use a regex with '' | ||
+ | <code powershell> | ||
+ | gci -Recurse | where {$_.Name -Match " | ||
+ | </ | ||
+ | |||
=== List Items === | === List Items === | ||
+ | <code powershell> | ||
+ | $exclude = @(" | ||
+ | $files = ls -Recurse -Exclude $exclude | ||
+ | Compress-Archive -Path $files -DestinationPath $home\desktop\webagent.zip | ||
+ | </ | ||
+ | |||
<code powershell> | <code powershell> | ||
$base_path = " | $base_path = " | ||
Line 21: | Line 43: | ||
ls -Recurse -Exclude $exclude_dir | ls -Recurse -Exclude $exclude_dir | ||
+ | </ | ||
+ | |||
+ | Example of using '' | ||
+ | <code powershell> | ||
+ | $exclude = @(" | ||
+ | $files = ls -Recurse -Exclude $exclude | ||
+ | $files = $files | where {$_.FullName -notmatch " | ||
+ | Compress-Archive -Path $files -DestinationPath $home\desktop\webagent.zip | ||
+ | </ | ||
+ | |||
+ | * Use the '' | ||
+ | <code powershell> | ||
+ | $exclude = @(" | ||
+ | $files = ls -File -Recurse -Exclude $exclude | ||
+ | Compress-Archive -Path $files -DestinationPath $home\desktop\webagent.zip | ||
</ | </ | ||
Line 42: | Line 79: | ||
Example | Example | ||
<code powershell> | <code powershell> | ||
- | $file=Get-ChildItem | + | $files = ls -Path .\* -Exclude |
- | Compress-Archive -Path $file -DestinationPath $env: | + | Compress-Archive -Path $files -DestinationPath $env: |
</ | </ | ||
Line 58: | Line 95: | ||
</ | </ | ||
+ | Unzip zip archives contained in another zip archive. | ||
+ | <code powershell> | ||
+ | Expand-Archive -DestinationPath .\zips ' | ||
+ | ls .\zips\ | % {Expand-Archive -DestinationPath $env:temp $_.FullName} | ||
+ | </ | ||
+ | === Create a folder with name following scheme === | ||
+ | <code powershell> | ||
+ | $suffix = 1 | ||
+ | |||
+ | while ((Test-Path " | ||
+ | $suffix += 1 | ||
+ | } | ||
+ | |||
+ | mkdir " | ||
+ | </ | ||
+ | |||
+ | <code powershell> | ||
+ | $names = @(" | ||
+ | |||
+ | $name = 0 | ||
+ | |||
+ | while ($name -lt $names.Count) { | ||
+ | if ((Test-Path " | ||
+ | break | ||
+ | } | ||
+ | |||
+ | $name += 1 | ||
+ | } | ||
+ | |||
+ | if ($name -eq $names.Count) { | ||
+ | $name = 0 | ||
+ | } | ||
+ | |||
+ | mkdir " | ||
+ | </ | ||