I have a folder with sequentially named images (a0001, a0002, a0003, etc.) and I was wondering if there was a way to delete every 3 files without touching the others.
我有一个带有顺序命名图像的文件夹(a0001,a0002,a0003等),我想知道是否有办法删除每3个文件而不触及其他文件。
So for example, I have: a0001, a0002, a0003, a0004, a0005 a0006, a0007, a0008, a0009
例如,我有:a0001,a0002,a0003,a0004,a0005 a0006,a0007,a0008,a0009
And after I would like to have: a0001, a0005, a0009
之后我想拥有:a0001,a0005,a0009
3 个解决方案
#1
0
I'm not sure about the syntax, as I usualy don't write batch scripts, but should be something like:
我不确定语法,因为我通常不编写批处理脚本,但应该是这样的:
set z = 0
for /f %%a IN (‘dir /b *’) do {
set z = z + 1;
if (z % 3 == 0) del %%a
}
#2
0
Here's a vbscript you can use
这是你可以使用的vbscript
strFolder = WScript.Arguments(0)
Set objFS = CreateObject( "Scripting.FileSystemObject" )
Set objFolder = objFS.GetFolder(strFolder)
i=0
For Each strFile In objFolder.Files
If i Mod 4 <> 0 Then
WScript.Echo strFile.Name
objFS.DeleteFile(strFile.Name)
End If
i=i+1
Next
usage:
C:\test> cscript //nologo test.vbs c:\folder_to_process
#3
0
Here's a solution. This allows you to specify what to delete (eg c:\temp\*.tmp
) how many files to skip (the default being 3
as you requested) and what order to use (the default being n
- filename). You can use any DIR ordering eg N
(name) or ES
(extension then size), see dir /?
to learn more about ordering. You shouldn't start the order command with /o
(unlike dir) - it's auto-prepended.
这是一个解决方案。这允许您指定要删除的内容(例如c:\ temp \ * .tmp)要跳过的文件数(默认为您请求的3)以及使用的顺序(默认为n - 文件名)。您可以使用任何DIR订购,例如N(名称)或ES(扩展然后大小),请参阅dir /?了解有关订购的更多信息。你不应该用/ o启动命令命令(与dir不同) - 它是自动预先添加的。
Source: ndel.bat
@echo off
::: ndel - Deletes every nth file matching the spec
::: syntax: ndel.bat FILESPEC [skipcount] [sortorder]
::: FILESPEC - Files to be searched through
::: skipcount - number of files to skip (optional - default 3)
::: sortorder - File order (see: DIR for options - default N (name))
:: With no arguments show the above usage text
if "%~1"=="" findstr "^:::" "%~f0"&GOTO:EOF
set find=%1
set evry=4
set ord=n
if "%~2" neq "" set /a evry=(%2+1)
if "%~3" neq "" set ord=%3
set count=0
for /f %%f IN ('dir %find% /b /o%%ord%%') do (
call :test_file "%%f"
)
GOTO:eof
:test_file
set /a _r="%count% %% %evry%"
if %_r%==0 echo %1
::-to delete- if %_r%==0 del %1
set /a count+=1
GOTO:eof
Notes:
- You need to comment out
if %_r%==0 echo %1
and remove the starting comment::-to delete-
to actually preform the delete (once you're happy it works :)). - The first few lines just output usage if you type in
ndel
(without a parameter) - Skip three is the same as saying delete every fourth file, which is why
evry
takes skip+1, and by default is set to 4 (skip 3). -
filespec
can include a folder and complicated wild-card matches (just likedir
). - A subroutine has to be used to
count
properly through thefor
(Environment variables within aFOR
loop are expanded at the beginning of the loop and won't change until after the end of theDO
section) - Calling
ndel c:\temp\*.tmp 0
is the same asdel c:\temp\*.tmp
(since skip 0 = all)
您需要注释掉%_r%== 0 echo%1并删除起始注释:: - 删除 - 以实际执行删除(一旦您满意它工作:))。
如果输入ndel(没有参数),前几行只输出用法
跳过三与删除每四个文件相同,这就是为什么evry采用跳过+ 1,默认设置为4(跳过3)。
filespec可以包含一个文件夹和复杂的通配符匹配(就像dir一样)。
必须使用子程序通过for正确计数(FOR循环中的环境变量在循环开始时展开,直到DO部分结束后才会改变)
调用ndel c:\ temp \ * .tmp 0与del c:\ temp \ * .tmp相同(因为skip 0 = all)
#1
0
I'm not sure about the syntax, as I usualy don't write batch scripts, but should be something like:
我不确定语法,因为我通常不编写批处理脚本,但应该是这样的:
set z = 0
for /f %%a IN (‘dir /b *’) do {
set z = z + 1;
if (z % 3 == 0) del %%a
}
#2
0
Here's a vbscript you can use
这是你可以使用的vbscript
strFolder = WScript.Arguments(0)
Set objFS = CreateObject( "Scripting.FileSystemObject" )
Set objFolder = objFS.GetFolder(strFolder)
i=0
For Each strFile In objFolder.Files
If i Mod 4 <> 0 Then
WScript.Echo strFile.Name
objFS.DeleteFile(strFile.Name)
End If
i=i+1
Next
usage:
C:\test> cscript //nologo test.vbs c:\folder_to_process
#3
0
Here's a solution. This allows you to specify what to delete (eg c:\temp\*.tmp
) how many files to skip (the default being 3
as you requested) and what order to use (the default being n
- filename). You can use any DIR ordering eg N
(name) or ES
(extension then size), see dir /?
to learn more about ordering. You shouldn't start the order command with /o
(unlike dir) - it's auto-prepended.
这是一个解决方案。这允许您指定要删除的内容(例如c:\ temp \ * .tmp)要跳过的文件数(默认为您请求的3)以及使用的顺序(默认为n - 文件名)。您可以使用任何DIR订购,例如N(名称)或ES(扩展然后大小),请参阅dir /?了解有关订购的更多信息。你不应该用/ o启动命令命令(与dir不同) - 它是自动预先添加的。
Source: ndel.bat
@echo off
::: ndel - Deletes every nth file matching the spec
::: syntax: ndel.bat FILESPEC [skipcount] [sortorder]
::: FILESPEC - Files to be searched through
::: skipcount - number of files to skip (optional - default 3)
::: sortorder - File order (see: DIR for options - default N (name))
:: With no arguments show the above usage text
if "%~1"=="" findstr "^:::" "%~f0"&GOTO:EOF
set find=%1
set evry=4
set ord=n
if "%~2" neq "" set /a evry=(%2+1)
if "%~3" neq "" set ord=%3
set count=0
for /f %%f IN ('dir %find% /b /o%%ord%%') do (
call :test_file "%%f"
)
GOTO:eof
:test_file
set /a _r="%count% %% %evry%"
if %_r%==0 echo %1
::-to delete- if %_r%==0 del %1
set /a count+=1
GOTO:eof
Notes:
- You need to comment out
if %_r%==0 echo %1
and remove the starting comment::-to delete-
to actually preform the delete (once you're happy it works :)). - The first few lines just output usage if you type in
ndel
(without a parameter) - Skip three is the same as saying delete every fourth file, which is why
evry
takes skip+1, and by default is set to 4 (skip 3). -
filespec
can include a folder and complicated wild-card matches (just likedir
). - A subroutine has to be used to
count
properly through thefor
(Environment variables within aFOR
loop are expanded at the beginning of the loop and won't change until after the end of theDO
section) - Calling
ndel c:\temp\*.tmp 0
is the same asdel c:\temp\*.tmp
(since skip 0 = all)
您需要注释掉%_r%== 0 echo%1并删除起始注释:: - 删除 - 以实际执行删除(一旦您满意它工作:))。
如果输入ndel(没有参数),前几行只输出用法
跳过三与删除每四个文件相同,这就是为什么evry采用跳过+ 1,默认设置为4(跳过3)。
filespec可以包含一个文件夹和复杂的通配符匹配(就像dir一样)。
必须使用子程序通过for正确计数(FOR循环中的环境变量在循环开始时展开,直到DO部分结束后才会改变)
调用ndel c:\ temp \ * .tmp 0与del c:\ temp \ * .tmp相同(因为skip 0 = all)