I would like to write a windows batch file for the purpose of renaming a file every 25 seconds. I would also like the batch file to terminate after 300 seconds have passed. How would I go about doing this? Here is what I have thusfar.
我想写一个Windows批处理文件,每25秒重命名一个文件。我还希望批处理文件在300秒后终止。我该怎么做呢?这就是我所拥有的。
START
RENAME test.g test.go
SLEEP 25
RENAME test.go test.g
GOTO START
3 个解决方案
#1
you will need the sleep command, you can download it here.
你需要睡眠命令,你可以在这里下载。
then you can do something like this:
然后你可以做这样的事情:
echo this is a test > test.txt
SET x=
:START
CALL SET /a x = %x% +1
rename test.* test.%x%
CALL SET /a y = %x% * 25
IF '%x%' == '300' GOTO END
CALL SLEEP 25
GOTO START
:END
#2
Well, this is not too difficult. There are a bunch of well-known batch tricks, such as mis-using ping to sleep (which saves you from having to integrate a non-standard tool) and we can then come up with the following:
嗯,这不是太难。有许多众所周知的批处理技巧,例如误用ping进入睡眠状态(这使您无需集成非标准工具),然后我们可以提出以下建议:
@echo off
setlocal
set n=0
:start
ren test.g test.go
ping localhost -n 26>nul 2>&1
ren test.go test.g
set /a n+=25
if %n% LSS 300 goto start
endlocal
setlocal
and endlocal
will ensure that all environment variables we create and modify will remain only in scope of the batch file itself. The command
setlocal和endlocal将确保我们创建和修改的所有环境变量仅保留在批处理文件本身的范围内。命令
ping localhost -n 26 >nul 2>&1
will wait 25 seconds (because the first ping will be immediate and every subsequent one incurs a one-second delay) while throwing away all normal and error output (>nul 2>&1
).
将等待25秒(因为第一次ping将是立即的,并且每个后续的一次都会产生一秒钟的延迟),同时丢弃所有正常和错误输出(> nul 2>&1)。
Finally we are keeping track of how long we waited so far in the %n%
variable and only if n is still below 300 we continue looping. You might as well do it with a for loop, though:
最后,我们将跟踪我们在%n%变量中等待的时间,并且只有当n仍然低于300时,我们才会继续循环。不过,您可以使用for循环执行此操作:
@echo off
setlocal
set n=300
set /a k=n/25
for /l %%i in (1,1,%k%) do (
ren test.g test.go
ping localhost -n 26>nul 2>&1
ren test.go test.g
)
endlocal
which will first calculate how often it would need to loop and then just iterate the calculated number of times.
这将首先计算循环所需的频率,然后迭代计算的次数。
#3
if you are using windows, then i assume you can use vbscript.
如果您使用的是Windows,那么我假设您可以使用vbscript。
Set objFS = CreateObject("Scripting.FileSystemObject")
strFile1 = "file.txt"
strFile2 = "new.txt"
While 1=1
Set objFile1 = objFS.GetFile(strFile1)
objFile1.Name = strFile2
Set objFile1 = Nothing
WScript.Echo "sleeping.."
WScript.Sleep (25 * 60)
Set objFile2 = objFS.GetFile(strFile2)
objFile2.Name = strFile1
Set objFile2 = Nothing
WScript.Sleep (300 * 60)
Wend
save the above as myscript.vbs and on command line
将上面的内容保存为myscript.vbs并保存在命令行中
c:\test> cscript /nologo myscript.vbs
#1
you will need the sleep command, you can download it here.
你需要睡眠命令,你可以在这里下载。
then you can do something like this:
然后你可以做这样的事情:
echo this is a test > test.txt
SET x=
:START
CALL SET /a x = %x% +1
rename test.* test.%x%
CALL SET /a y = %x% * 25
IF '%x%' == '300' GOTO END
CALL SLEEP 25
GOTO START
:END
#2
Well, this is not too difficult. There are a bunch of well-known batch tricks, such as mis-using ping to sleep (which saves you from having to integrate a non-standard tool) and we can then come up with the following:
嗯,这不是太难。有许多众所周知的批处理技巧,例如误用ping进入睡眠状态(这使您无需集成非标准工具),然后我们可以提出以下建议:
@echo off
setlocal
set n=0
:start
ren test.g test.go
ping localhost -n 26>nul 2>&1
ren test.go test.g
set /a n+=25
if %n% LSS 300 goto start
endlocal
setlocal
and endlocal
will ensure that all environment variables we create and modify will remain only in scope of the batch file itself. The command
setlocal和endlocal将确保我们创建和修改的所有环境变量仅保留在批处理文件本身的范围内。命令
ping localhost -n 26 >nul 2>&1
will wait 25 seconds (because the first ping will be immediate and every subsequent one incurs a one-second delay) while throwing away all normal and error output (>nul 2>&1
).
将等待25秒(因为第一次ping将是立即的,并且每个后续的一次都会产生一秒钟的延迟),同时丢弃所有正常和错误输出(> nul 2>&1)。
Finally we are keeping track of how long we waited so far in the %n%
variable and only if n is still below 300 we continue looping. You might as well do it with a for loop, though:
最后,我们将跟踪我们在%n%变量中等待的时间,并且只有当n仍然低于300时,我们才会继续循环。不过,您可以使用for循环执行此操作:
@echo off
setlocal
set n=300
set /a k=n/25
for /l %%i in (1,1,%k%) do (
ren test.g test.go
ping localhost -n 26>nul 2>&1
ren test.go test.g
)
endlocal
which will first calculate how often it would need to loop and then just iterate the calculated number of times.
这将首先计算循环所需的频率,然后迭代计算的次数。
#3
if you are using windows, then i assume you can use vbscript.
如果您使用的是Windows,那么我假设您可以使用vbscript。
Set objFS = CreateObject("Scripting.FileSystemObject")
strFile1 = "file.txt"
strFile2 = "new.txt"
While 1=1
Set objFile1 = objFS.GetFile(strFile1)
objFile1.Name = strFile2
Set objFile1 = Nothing
WScript.Echo "sleeping.."
WScript.Sleep (25 * 60)
Set objFile2 = objFS.GetFile(strFile2)
objFile2.Name = strFile1
Set objFile2 = Nothing
WScript.Sleep (300 * 60)
Wend
save the above as myscript.vbs and on command line
将上面的内容保存为myscript.vbs并保存在命令行中
c:\test> cscript /nologo myscript.vbs