使用 Windows 7,我需要一个 .bat 文件,每当我将文件放入文件夹中时,它都会执行其中的文件。例如,我希望它执行带有 .sas 后缀的文件,然后在执行后重命名它们。我希望程序按顺序运行,而不是同时运行。我对 bat 编程不太熟悉。
我可以运行以下命令,
cd C:\Users\ABC\Dropbox\XYZ\Runlibrary
timeout /t 15
"C:\Program Files\SASHome2-94\SASFoundation\9.4\sas.exe" -sysin "program5.sas"
ren "program5.sas" "program5.done"
但现在想要一个循环,并能够让它执行文件夹中的所有 .SAS 程序。例如
do (the following 1 million times)
timeout /t 15
for %f in (*.sas) do "C:\Program Files\SASHome2-94\SASFoundation\9.4\sas.exe" -sysin "%f"
for %f in (*.sas) do ren "*.sas" "*.done"
想法、链接或示例代码?
答案1
这是我准备的东西,我还没有测试过这些,但是尝试一下,因为它会循环直到你杀死它:
@echo off
:: Setting max_count to '0' makes this an infinite loop, you can set it to 10000000 if you want or leave it at 0
set max_count=0
:: Location where the .sas files are stored; '.' means current directory
set sas_dir=.
:: counter should be '0'
set counter=0
:: Starting the loop
:start_loop
set /a counter+=1
:: Looking for .sas files to execute
if exist "%sas_dir%\*.sas" for /f "tokens=1* delims=" %%f in ('dir /b /o:n /a:-d "%sas_dir%\*.sas"') do (
echo INFO: Running %%f
call "C:\Program Files\SASHome2-94\SASFoundation\9.4\sas.exe" -sysin "%%~pnxf"
rename "%%~pnxf" "%%~pnf.done"
)
:: Stats
echo INFO: Looped Count: %counter%; Reloop Time: 15 secs
:: Waiting 15 secs to reloop
timeout /t 15 >nul
:: Checking Max Counter
if "%max_count%"=="%counter%" (
echo INFO: Looping completed with "%max_count%" counts, exiting...
goto end
)
:: relooping
goto start_loop
:end
我发现这个网站提供了有关for
循环和其他批处理相关问题的信息:http://judago.webs.com/batchforloops.htm