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 かを確認する必要があります (2 つの別々のコマンドを使用する必要はありません)。これらの 2 つのアクションを実行する最善の方法は何ですか? さらに、これを改善するために実装する必要があるものは他にありますか?

4

2 に答える 2

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 に答える
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
)

%これらの変数には、バッチ ファイルでdouble- を使用する必要があることに注意してください。dirお気づきのように、出力をループする必要はありませんfor。ファイルとディレクトリを単独で反復処理できるためです。

拡張子のチェックに関しては、具体的にどの拡張子をチェックしたいのか少し迷っています。フォルダーを繰り返し処理していますが、フォルダーの拡張機能はほとんど意味がありません。

于 2012-08-03T14:36:03.547 に答える