简体   繁体   English

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

[英]Windows batch file for renaming file at specified interval

I would like to write a windows batch file for the purpose of renaming a file every 25 seconds. 我想编写一个Windows批处理文件,目的是每25秒重命名一个文件。 I would also like the batch file to terminate after 300 seconds have passed. 我还希望批处理文件在300秒后终止。 How would I go about doing this? 我将如何去做呢? Here is what I have thusfar. 这就是我到目前为止所拥有的。

START

RENAME test.g test.go

SLEEP 25

RENAME test.go test.g

GOTO START

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. setlocalendlocal将确保我们创建和修改的所有环境变量仅保留在批处理文件本身的范围内。 The command 命令

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将立即执行,并且随后的每个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. 最后,我们跟踪%n%变量到目前为止已经等待了多长时间,并且只有当n仍然小于300时,我们才继续循环。 You might as well do it with a for loop, though: 您也可以使用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. 它将首先计算需要循环的频率,然后仅迭代计算的次数。

you will need the sleep command, you can download it here . 您将需要sleep命令,可以在此处下载

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM