Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
powershell:qnd:file_operations [2022/04/06 20:30] mguptonpowershell:qnd:file_operations [2022/10/06 20:13] (current) mgupton
Line 8: Line 8:
  
 ~\Desktop ~\Desktop
 +</code>
 +
 +=== User Split-Path to get folder name ===
 +<code powershell>
 +cd $(Split-Path $PROFILE)
 </code> </code>
  
 ====== Common Commands By Example ====== ====== Common Commands By Example ======
 +=== List Items with Filter/Search for files with matching string in name ===
 +Example of finding a file somewhere in the filesystem and outputting the full path and filename.
 +<code powershell>
 +Get-ChildItem -Recurse -Filter "csc.exe" | foreach {$_.Fullname}
 +</code>
 +
 +Use a regex with ''-Match'' operator for more advanced filtering.
 +<code powershell>
 +gci -Recurse | where {$_.Name -Match "^csc.*"} | foreach {$_.Fullname}
 +</code>
 +
 === List Items === === List Items ===
 <code powershell> <code powershell>
Line 34: Line 50:
 $files = ls -Recurse -Exclude $exclude $files = ls -Recurse -Exclude $exclude
 $files = $files | where {$_.FullName -notmatch ".*\\logs"} $files = $files | where {$_.FullName -notmatch ".*\\logs"}
-Compress-Archive -Path $files -DestinationPath $env:userprofile\desktop\webagent.zip+Compress-Archive -Path $files -DestinationPath $home\desktop\webagent.zip
 </code> </code>
  
   * Use the ''-File'' or ''-Directory'' option of ''Get-ChildItem'' to list on those items.   * Use the ''-File'' or ''-Directory'' option of ''Get-ChildItem'' to list on those items.
 +<code powershell>
 +$exclude = @("*.log", "*.tmp", "log*.txt*")
 +$files = ls -File -Recurse -Exclude $exclude
 +Compress-Archive -Path $files -DestinationPath $home\desktop\webagent.zip
 +</code>
  
 === Search through files for string/grep === === Search through files for string/grep ===
Line 74: Line 95:
 </code> </code>
  
 +Unzip zip archives contained in another zip archive.
 +<code powershell>
 +Expand-Archive -DestinationPath .\zips '.\all_reports.zip'
 +ls .\zips\ | % {Expand-Archive -DestinationPath $env:temp $_.FullName}
 +</code>
  
 +=== Create a folder with name following scheme ===
 +<code powershell>
 +$suffix = 1
 +
 +while ((Test-Path "$env:temp\mg-$suffix") -ne $false) {
 +    $suffix += 1
 +}
 +
 +mkdir "$env:temp\mg-$suffix"
 +</code>
 +
 +<code powershell>
 +$names = @("alpha", "beta", "gamma")
 +
 +$name = 0
 +
 +while ($name -lt $names.Count) {
 +    if ((Test-Path "$env:temp\$names[$name]") -eq $false) {
 +        break
 +    }
 +
 +    $name += 1
 +}
 +
 +if ($name -eq $names.Count) {
 +    $name = 0   
 +}
 +
 +mkdir "$env:temp\$($names[$name])"
 +</code>
  
  • powershell/qnd/file_operations.1649277042.txt.gz
  • Last modified: 2022/04/06 20:30
  • by mgupton