1

我是 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 (而不是两个单独的命令)。做这两个动作的最佳方法是什么?此外,我还应该实施其他一些措施来使其变得更好吗?

4

2 回答 2

1

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可以自己迭代文件和目录就可以了。

至于检查扩展名,我有点不知所措,具体来说,您要检查什么扩展名。您正在遍历文件夹,但文件夹上的扩展很少有任何意义。

于 2012-08-03T14:36:03.547 回答
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
    }
}
于 2012-08-06T17:38:14.487 回答