以指定的时间间隔重命名文件的Windows批处理文件

问题描述:

我想编写一个windows批处理文件,以便每隔25秒重命名文件。我还希望批处理文件在300秒后终止。我会如何去做这件事?这是我迄今为止所做的。以指定的时间间隔重命名文件的Windows批处理文件

START 

RENAME test.g test.go 

SLEEP 25 

RENAME test.go test.g 

GOTO START 

你需要睡眠命令,就可以download it here

那么你可以做这样的事情:

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 

那么,这是不是太困难。有一堆知名批技巧,如误用平睡(从有到整合非标准的工具可以节省您的),然后我们就可以拿出如下:

@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 

setlocalendlocal将确保我们创建和修改的所有环境变量仅保留在批处理文件本身的范围内。命令

ping localhost -n 26 >nul 2>&1 

将等待25秒(因为第一平将是立即和以后每一个即被一第二延迟),而扔掉所有正常和错误输出(>nul 2>&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