1

I'm new to Windows scripting. I wrote a small batch file to move subdirectories and files around in a large directory.

@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 

It looks like I can put all of my commands into a single "for /f %x in..." loop and then do the logic inside. I should probably check if the extension is .png or .jpg (not with two separate commands). What's the best way to do these two actions? In addition is there something else I should implement to make this better?

2
  • 1
    If you're open to PowerShell and you can give me a plain english explanation of what you're trying to do I'd give you a sample. If you're learning a new scripting language for Windows that's definitely the way to go.
    – Chris N
    CommentedAug 4, 2012 at 20:54
  • @ChrisN pastie.org/private/ke5pc66pl2kzsfsa0tldgCommentedAug 6, 2012 at 17:38

2 Answers 2

1

PowerShell looks a little more verbose in this instance, but here's an example anyway. Again, as I mentioned in my comment - if you're trying to learn a scripting language for Windows right now, do yourself a favor and learn 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 } } 
    1

    Just do a single for loop the following way:

    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 ) 

    Note that you must use a double-% in batch files for those variables. And as you notice you don't need to loop over dir output because for can iterate over files and directories on its own just fine.

    As for checking the extension I'm a little at a loss what extension you want to check, specifically. You're iterating over folders but extensions on folders rarely have any significance.

    2
    • Hm, well I guess I was doing it wrong as far as extensions go (although my script did work). The extensions I am checking for are on files not directories.CommentedAug 3, 2012 at 14:40
    • When I run your version of the script it appends ipad to every directory before trying to do any of the actions.CommentedAug 3, 2012 at 14:49

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.