Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于按指定间隔重命名文件的Windows批处理文件_Windows_Batch File_Loops_Timer - Fatal编程技术网

用于按指定间隔重命名文件的Windows批处理文件

用于按指定间隔重命名文件的Windows批处理文件,windows,batch-file,loops,timer,Windows,Batch File,Loops,Timer,我想写一个windows批处理文件,以便每25秒重命名一个文件。我还希望批处理文件在300秒后终止。我该怎么做呢?这是我得到的,苏斯法 START RENAME test.g test.go SLEEP 25 RENAME test.go test.g GOTO START 您将需要sleep命令,您可以 然后你可以这样做: echo this is a test > test.txt SET x= :START CALL SET /a x = %x% +1 rename tes

我想写一个windows批处理文件,以便每25秒重命名一个文件。我还希望批处理文件在300秒后终止。我该怎么做呢?这是我得到的,苏斯法

START

RENAME test.g test.go

SLEEP 25

RENAME test.go test.g

GOTO START

您将需要sleep命令,您可以

然后你可以这样做:

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

嗯,这不太难。有一系列众所周知的批处理技巧,例如错误地使用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
endlocal
将确保我们创建和修改的所有环境变量仅保留在批处理文件本身的范围内。命令

ping localhost -n 26 >nul 2>&1
将等待25秒(因为第一次ping将立即进行,随后的每一次ping都会导致1秒延迟),同时丢弃所有正常和错误输出(
>num2>&1

最后,我们将跟踪到目前为止在
%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

它将首先计算循环所需的频率,然后迭代计算的次数。

如果您使用的是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
将上述内容另存为myscript.vbs并保存在命令行中

c:\test> cscript /nologo myscript.vbs