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){ … } or for($i=1;$i -le 10; $i++){echo @($i*2)} |
Batch run (jobs)
Below is example of batch run a python script in designated virtual environment as a parallelizable job. Note the variables is passed into the job using -ArgumentList keyword. @() is a sub-expression operator. It can be used to create empty array by arr=@(). It is used to run algorithmic operation (multiplication) here.
for($i=4;$i -le 10; $i++){Start-Job {conda activate ENV d:; cd “D:\PATH”; python .\SCRIPT.py –DS $args[0]} -ArgumentList @($i*10);}
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,measurereturn number of rows, similar tocount- powershell comparison operators:
-eqis equal and-nenot equal foreachsyntax:foreach(condition){operations}ifsyntax,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 |