【问题标题】:Windows scripting for loop用于循环的 Windows 脚本
【发布时间】:2012-08-03 14:29:03
【问题描述】:

我是 Windows 脚本的新手。我写了一个小批处理文件来移动一个大目录中的子目录和文件。

@ECHO OFF
for /f %x in ('dir /ad /b') do move %xipad %x\
for /f %x in ('dir /ad /b') do md %x\thumbs
for /f %x in ('dir /ad /b') do move %x\*thumb.png %x\thumbs\
for /f %x in ('dir /ad /b') do move %x\*thumb.jpg %x\thumbs\
for /f %x in ('dir /ad /b') do del %x\%xipad\*thumb.png
for /f %x in ('dir /ad /b') do del %x\%xipad\*thumb.jpg
for /f %x in ('dir /ad /b') do del %x\xml.php
for /f %x in ('dir /ad /b') do del %x\%xipad\xml.php

看起来我可以将所有命令放入一个“for /f %x in...”循环中,然后在里面执行逻辑。我可能应该检查扩展名是 .png 还是 .jpg (而不是两个单独的命令)。做这两个动作的最佳方法是什么?此外,我还应该实施其他一些措施来改善这一点吗?

【问题讨论】:

  • 如果您对 PowerShell 持开放态度,并且可以给我一个简单的英语解释,说明您正在尝试做什么,我会给您一个示例。如果您正在为 Windows 学习一种新的脚本语言,那绝对是您的最佳选择。
  • @ChrisN Pastie.org/private/ke5pc66pl2kzsfsa0tldg

标签: windows scripting batch-file


【解决方案1】:

在这种情况下,PowerShell 看起来有点冗长,但无论如何这里是一个示例。同样,正如我在评论中提到的那样 - 如果您现在正在尝试学习 Windows 脚本语言,请帮自己一个忙,学习 PowerShell!

#Get the directories we're going to work with:
Get-ChildItem -Path d:\rootdirectory\ | ? {$_.PSIsContainer} | % {
    #Remove all xml.php files from current directory and current directory ipad.
    Remove-Item ($_.FullName + "\xml.php")
    #For all the files in the directory move the each "ipad" directory into the directory with the same name.
    If ($_.Name -like *ipad) {  
        #Delete all  png and jpg images with "thumb" in the name from each current directories ipad directory
        Get-ChildItem $_ -filter "*thumb* | ? {($_.Extension -eq "jpg") -or ($_.Extension -eq "png")} | % {
            Remove-Item $_
        }
        #...Then actually move the item
        Move-Item $_ -Destination $_.FullName.Replace("ipad","")}
    }
    #Use else to work on the remainder of the directories:
    else {
        #Create a directory called "thumbs" inside all of the current directories
        $thumbDir = New-Item -ItemType Directory -Path ($_.FullName + "\thumbs\")
        #Move all png and jpg files in the current directory with "thumb" in the name into the "thumbs" directory.
        Get-ChildItem $_ -filter "*thumb* | ? {($_.Extension -eq "jpg") -or ($_.Extension -eq "png")} | % {
            Move-Item $_ -Destination $thumbDir.FullName
    }
}

【讨论】:

    【解决方案2】:

    只需按以下方式执行一个 for 循环:

    for /D %%x in (*) do (
      move %%xipad %%x\
      md %%x\thumbs
      move %%x\*thumb.png %x\thumbs\
      move %%x\*thumb.jpg %x\thumbs\
      del %%x\%%xipad\*thumb.png
      del %%x\%%xipad\*thumb.jpg
      del %%x\xml.php
      del %%x\%%xipad\xml.php
    )
    

    请注意,您必须在批处理文件中为这些变量使用双%。正如您所注意到的,您不需要循环遍历 dir 输出,因为 for 可以自行遍历文件和目录。

    至于检查扩展我有点茫然什么你想检查的扩展,具体来说。您正在遍历文件夹,但文件夹上的扩展很少有任何意义。

    【讨论】:

    • 嗯,好吧,我想我在扩展方面做错了(尽管我的脚本确实有效)。我正在检查的扩展名是文件而不是目录。
    • 当我运行你的脚本版本时,它会在尝试执行任何操作之前将 ipad 附加到每个目录。
    猜你喜欢
    • 2017-10-05
    • 2014-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-09
    • 1970-01-01
    相关资源
    最近更新 更多