Run a simple command using PowerShell recursively on a directory

风格不统一 提交于 2019-12-19 05:59:27

问题


What's the fastest way using either DOS scripting or PowerShell to run this simple command on a directory and all its subdirectories:

 convert filename.jpg -resize 620x620 "R:\processed\filename.jpg"

DOS Batch script for single directory:

 FOR %%a in (*.jpg) DO convert %%a -resize 620x620 "R:\processed\%%a"

I want to run this recursively on a directory structure and have the output match the input hierarchy. I figured PowerShell was the easiest way, but I was unable to learn PowerShell in the 5 minutes I have to do this task!

Note: not that it's relevant, but convert is from ImageMagick.


回答1:


In PowerShell:

Use the -recurse switch and pipe to foreach. For e.g.:

dir -recurse -include *.jpg | %{convert  $_.FullName -resize 620x620 "R:\processed\$_"}

(Note that the % sign is an alias of foreach-object).



来源:https://stackoverflow.com/questions/784057/run-a-simple-command-using-powershell-recursively-on-a-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!