Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

powershell - Translating OS X Bash Script for Windows

I use Hedge to transfer Magic Lantern video files shot on my Canon 5D Mark III.

On OS X, I'm able to use Automator to set up a bash script, to execute an mlv_dump to transfer the files from MLV into cDNG sequences.

The script I use currently is:

cd "$(dirname "$1")"
for f in "$@"; do
    if [[ -f $f ]]; then
        filename=${f##*/};
        folder=${filename%.*}
        mkdir "${folder}";

        ~/mlv_dump --dng $f -o ${folder}/${folder}_;
    fi
done

Can this easily translate into a Windows equivalent?

Thanks,
Thomas

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As with any translation between programming languages, there's (a) an as-literal-as-possible approach, which contrasts with (b), an not-immediately-obvious-but-in-the-spirit-of-the-target-language approach.
(b) is always preferable in the long run.

Use PowerShell, because it is the - far superior - successor to the "Command Prompt" (cmd.exe) and its batch files.
The code below is an attempt at (b), in PowerShell (v3+ syntax).

I encourage you to study the code and post an explanation of it in an answer of your own, so that others may benefit too.

To help with the analysis, consider the following resources:

PowerShell-idiomatic translation of your code:

param(
  [Parameter(Mandatory, ValueFromRemainingArguments)]
  [System.IO.FileInfo[]] $LiteralPath
)

$outputBaseFolder = Split-Path -Parent $LiteralPath[0].FullName
foreach ($f in $LiteralPath) {
  if ($f.exists) {
    $outputFolder = Join-Path $outputBaseFolder $f.BaseName
    New-Item -ItemType Directory $outputFolder
    & "$HOME/mlv_dump" --dng $f.FullName -o "$outputFolder/$($f.BaseName)_"
  } else {
    Write-Warning "Item doesn't exist or is not a file: $($f.FullName)"
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...