DOS/PS commands
windows cmd or powershell
Personal note
if you also have python experience, some helpful analogs are:
python | PS(powershell) | |
variables | var | $var |
for loop | for i in ls: … | foreach($i in $ls){ … } |
File management
rename with PS
Replace the same section of strings with Powershell.
Ref: {StackExchange}
1 |
get-childitem *.mp3 | foreach { rename-item $_ $_.Name.Replace("Radiohead -", "") } |
Ref: {Link}
1 |
Get-ChildItem *.jpg | Rename-Item -NewName { $_.Name -replace '.jpg','.png' } |
or a more linux way, thanks to the aliases in powershell
1 |
$files=ls; foreach($fs in $files){$fn = $fs.Name; mv $fn $fn.replace('Pattern A', 'Pattern B')} |
A demo of for loop and if statement
1 |
foreach($f in ls){echo $f.Name; $count=(ls $f|measure).Count; if($count -ne 10000){echo $count}} |
- $(var_name) to specify variables
|
is pipeline,measure
return number of rows, similar tocount
- powershell comparison operators:
-eq
is equal and-ne
not equal foreach
syntax:foreach(condition){operations}
if
syntax,if(condition){statement}
Zip & Unzip
powershell
1 |
foreach($fs in $files){Expand-Archive $fs -DestinationPath "D:\AYX\Rawdon\Supplement_231005\ehm New data\unzipped\$fs.Name.replace('.zip', '')" } |
similarly, to zip, use Compress_archive
command as
1 |
Compress_Archive -Path {Files, add to the list} -DestinationPath {YourName.zip} |
dos with 7zip
When unzipping 7z files but do not have permission to install, one can use 7zr.exe {Download Link}.
But one must call it with full directory. I tried doskey that claims to resemble ‘alias’ in linux, but it returns help docs only.
1 2 |
{full_dir}\7zr.exe \? # see help doskey {full_dir}\7zr.exe 7zr # the alias |