同时循环批量

这是我想要的,在BACKUPDIR里面,我想执行cscript /nologo c:\deletefile.vbs %BACKUPDIR%直到文件夹内的文件数大于21( countfiles保存它)。 这是我的代码:

 @echo off SET BACKUPDIR=C:\test for /f %%x in ('dir %BACKUPDIR% /b ^| find /v /c "::"') do set countfiles=%%x for %countfiles% GTR 21 ( cscript /nologo c:\deletefile.vbs %BACKUPDIR% set /a countfiles-=%countfiles% ) 

Solutions Collecting From Web of "同时循环批量"

 set /a countfiles-=%countfiles% 

这将设置countfiles为0.我认为你想减少1,所以用这个来代替:

 set /a countfiles-=1 

我不确定如果for循环会工作,最好尝试这样的事情:

 :loop cscript /nologo c:\deletefile.vbs %BACKUPDIR% set /a countfiles-=1 if %countfiles% GTR 21 goto loop 

一个while循环可以用cmd.exe模拟:

 :while1 if %countfiles% leq 21 ( :: change countfile here goto :while1 ) 

例如,以下脚本:

 @echo off setlocal enableextensions enabledelayedexpansion set /a "x = 0" :while1 if %x% leq 5 ( echo %x% set /a "x = x + 1" goto :while1 ) endlocal 

输出:

 0 1 2 3 4 5 

对于你的具体情况,我会从以下开始。 你最初的描述有点混乱。 我假设你想删除该目录中的文件,直到20或更少:

 @echo off set backupdir=c:\test :loop1 for /f %%x in ('dir %backupdir% /b ^| find /v /c "::"') do set countfiles=%%x if %countfiles% gtr 20 ( cscript /nologo c:\deletefile.vbs %backupdir% goto :loop1 ) 

这对我来说非常有用,我用以下方式在活动目录中添加用户:

 :: This file is used to automatically add list of user to activedirectory :: First ask for username,pwd,dc details and run in loop :: dsadd user cn=jai,cn=users,dc=mandrac,dc=com -pwd `1q`1q`1q`1q @echo off setlocal enableextensions enabledelayedexpansion set /a "x = 1" set /p lent="Enter how many Users you want to create : " set /p Uname="Enter the user name which will be rotated with number ex:ram then ram1 ..etc : " set /p DcName="Enter the DC name ex:mandrac : " set /p Paswd="Enter the password you want to give to all the users : " cls :while1 if %x% leq %lent% ( dsadd user cn=%Uname%%x%,cn=users,dc=%DcName%,dc=com -pwd %Paswd% echo User %Uname%%x% with DC %DcName% is created set /a "x = x + 1" goto :while1 ) endlocal 
 @echo off set countfiles=10 :loop set /a countfiles -= 1 echo hi if %countfiles% GTR 0 goto loop pause 

在第一个“设置countfiles”10你看到的是它会循环回声嗨是你想循环的东西

…我已经5年了

可以用do while ... enddo循环来完成。