powershell:qnd:file_operations

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/04 17:16] mguptonpowershell:qnd:file_operations [2022/10/06 20:13] (current) mgupton
Line 1: Line 1:
 ====== File Operations ====== ====== File Operations ======
 +
 +====== Path References ======
 +<code powershell>
 +$env:userprofile\Desktop
 +
 +$Home\Desktop
 +
 +~\Desktop
 +</code>
 +
 +=== User Split-Path to get folder name ===
 +<code powershell>
 +cd $(Split-Path $PROFILE)
 +</code>
 +
 +====== 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 ===
 +<code powershell>
 +$exclude = @("*.log", "*.tmp", "log*.txt")
 +$files = ls -Recurse -Exclude $exclude
 +Compress-Archive -Path $files -DestinationPath $home\desktop\webagent.zip
 +</code>
 +
 +<code powershell>
 +$base_path = "$home\downloads"
 +$exclude_dir_files = @("$base_path\tmp\*",
 +  "$base_path\log\*",
 +  "*.tmp",
 +  "*.log"
 +)
 +
 +ls -Recurse -Exclude $exclude_dir
 +</code>
 +
 +Example of using ''-Exclude'' and additional regex filtering with ''-notmatch'' operator. This is useful for excluding folders.
 +<code powershell>
 +$exclude = @("*.log", "*.tmp", "log*.txt*")
 +$files = ls -Recurse -Exclude $exclude
 +$files = $files | where {$_.FullName -notmatch ".*\\logs"}
 +Compress-Archive -Path $files -DestinationPath $home\desktop\webagent.zip
 +</code>
 +
 +  * 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 20: Line 79:
 Example Example
 <code powershell> <code powershell>
-$file=Get-ChildItem -Path .\* -Exclude "*.tmp, *.log" +$files ls -Path .\* -Exclude @("*.tmp""*.log") 
-Compress-Archive -Path $file -DestinationPath $env:userprofile\desktop\webapp1.zip+Compress-Archive -Path $files -DestinationPath $env:userprofile\desktop\webapp1.zip
 </code> </code>
  
Line 36: 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.1649092603.txt.gz
  • Last modified: 2022/04/04 17:16
  • by mgupton