gpt4 book ai didi

Windows 脚本 for 循环

转载 作者:可可西里 更新时间:2023-11-01 11:59:26 25 4
gpt4 key购买 nike

我是 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 的脚本语言,请帮自己一个忙,学习 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
}
}

关于Windows 脚本 for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11797703/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com