我们有一个目录,里面有 40 多个文件,这些文件有不同的文件名。它们都以相同的文件名结构开始,但末尾附加了一个日期。
例子:
FILE.txt.01012013
FILE.txt.01022013
FILE.txt.01032013
我需要创建一个批处理文件来执行一些复杂的事情和一些不太复杂的事情:
- 仅选择一个文件。
- 将该文件重命名为其他名称。(例如:TEST.txt)注意:重命名文件的名称始终为 TEST.txt)
- 将重命名的文件从当前目录移动到新目录。
- 15 分钟后...再次从步骤 1 开始。(注意:这需要持续运行,直到原始目录中没有剩余文件。)
我尝试过的方法:我对批处理文件的技能水平非常基础,因此我一直在尝试在网上搜索建议。我可以找到重命名文件的代码(但您必须说明原始文件名)。我可以找到使用文件名中的 * 来查找文件的代码,但我认为这会选择目录中的所有文件。我需要它一次对一个文件进行一次,每 15 分钟一次。一旦文件被重命名并移动到新目录...就会有一个文件观察程序进程获取该文件(在此示例中为 TEST.txt)并提取数据。一旦数据被提取,文件就会被删除。这意味着当下一个文件被重命名为 TEST.txt 并移动到目录时,没有任何理由覆盖前一个文件。
答案1
这看起来像是您想要的,但是您需要填补空白。
set count=0
for /f %%i in (file_path\*.*) do set /a count+=1
set test=%count%
:loop
if %test% neq %count% timeout /nobreak 900 >nul
set /a num=%random% %% %count% + 1
set /a count-=1
if %count% leq 0 goto end
for /f "skip=%num%" %%i in (file_path\*.*) do (
ren %%i TEST.txt
move TEST.txt file_path\
goto loop
)
:end
它能做什么:
- 找出文件夹中有多少个文件(您需要更改)。
- 生成一个随机数,其最大值为文件夹中的文件数量。
- 从最大数字中减去一个,以备下次使用。
- 跳过随机数量的文件(由随机数指定)并选择下一个。
- 将文件重命名为 TEXT.txt 并将其移动到 file_path\(您需要更改)。
- 等待 15 分钟(900 秒)。
希望有所帮助。
注意,您需要将 **file_path** 更改为适当的文件。
答案2
试试这个,它可以完成你要求的所有事情:
@echo off&setlocal enabledelayedexpansion
set "startfolder=folder1"
set "targetfolder=folder2"
cd /d "%startfolder%"
:randomstart
set /a filecnt=0
for %%i in (*) do set /a filecnt+=1
if %filecnt% equ 0 echo(no file found in %Startfolder%&goto:eof
set /a rd=(%random%%%%filecnt%)+1
set /a cnt=0
for %%i in (*) do set /a cnt+=1&if "!cnt!" equ "%rd%" set "randomfile=%%~i"
echo "%randomfile%"
move "%randomfile%" "%targetfolder%\test.txt"
ping -n 900 localhost >nul
goto:randomstart
答案3
这是一个处理文件的简单批处理脚本:
@echo off
rem set your file-match pattern here. Could be *.* for all files, or whatever you wish.
set "zzmatchpattern=FILE.txt.*"
rem set filename for renamed files
set "zzfinalname=TEST.txt"
rem set source folder here
set "zzsourcepath=C:\source\"
rem set destination folder here
set "zzdestpath=C:\dest\"
rem set your loop delay here
set "zzdelayminutes=15"
rem **********************************
rem might be good to add some error checking here
rem **********************************
:start
rem:start
set /a zzdelayseconds=zzdelayminutes*60
:restart
rem:restart
for %%f in ("%zzsourcepath%%zzmatchpattern%") do call :work "%%~f"
timeout /t %zzdelayseconds%
goto :restart
:work
rem:work
set "zz_w1=%~1"
echo.
echo Renaming file "%zz_w1%" to "%zzfinalname%"
ren "%zz_w1%" "%zzfinalname%">nul 2>&1
echo Moving file: "%zzsourcepath%%zzfinalname%" to "%zzdestpath%"
move "%zzsourcepath%%zzfinalname%" "%zzdestpath%">nul 2>&1
timeout /t %zzdelayseconds%
rem Go get next file, if any.
goto :EOF
如果您想要更完整的解决方案,下面的批处理脚本包括:
- 对输入值进行相当完整的验证(错误检查)。
- 通过简单的修改,就可以安全地从命令行输入值。
- 开始时,检查现有目标文件是否存在并等待外部进程删除文件。
- 检查并报告失败的重命名和移动操作。
- 退出/失败后恢复进程文件重命名但未移动。
- 允许(可选)提示延迟(Choice.exe)允许“立即继续”和优雅“退出”。
@echo off
rem set your file-match pattern here. Could be *.* for all files, or whatever you wish.
set "zzmatchpattern=FILE.txt.*"
rem set filename for renamed files
set "zzfinalname=TEST.txt"
rem set source folder here
set "zzsourcepath=C:\source"
rem set destination folder here
set "zzdestpath=C:\dest"
rem set your loop delay here
set "zzdelayminutes=15"
rem select Prompted-delay, or simple Timeout-delay. P=Prompted, otherwise simple Timeout
rem For Prompted-delay "Choice.exe" must be present on the computer.
set "zzdelaytype=T"
rem **********************************
rem error checking
rem **********************************
:checksourcepath
rem:checksourcepath
rem insure source path is valid (exists), and has trailing slash
if "%zzsourcepath%."=="." goto :sourcepatherror
set zzt1=%zzsourcepath:~-1,1%
if not "%zzt1%."=="\." set "zzsourcepath=%zzsourcepath%\"
if not exist "%zzsourcepath%*.*" goto :sourcepatherror
goto :checkdestpath
:sourcepatherror
rem:sourcepatherror
echo.
echo Error processing source path: [%zzsourcepath%].
echo.
call :cleanexit
exit /b 1
:checkdestpath
rem:checkdestpath
rem insure dest path is valid (exists), and has trailing slash
if "%zzdestpath%."=="." goto :destpatherror
set zzt1=%zzdestpath:~-1,1%
if not "%zzt1%."=="\." set "zzdestpath=%zzdestpath%\"
if not exist "%zzdestpath%*.*" goto :createdestpath
goto :checkname
:createdestpath
rem:createdestpath
md "%zzdestpath%" >nul 2>&1
if not exist "%zzdestpath%*.*" goto :destpatherror
goto :checkname
:destpatherror
rem:destpatherror
echo.
echo Error processing destination path: [%zzdestpath%].
echo.
call :cleanexit
exit /b 2
:checkname
rem:checkname
if "%zzfinalname%."=="." goto :nameerror
goto :checkdelayminutes
:nameerror
rem:nameerror
echo.
echo Error: Rename target filename cannot be empty.
echo.
call :cleanexit
exit /b 3
:checkdelayminutes
rem:checkdelayminutes
set zzt1=0
set zzt2=1
set /a zzt1=zzt2*zzdelayminutes
if "%zzt1%."=="." goto :minute1serror
if %zzt1% LEQ 0 goto :minutes1error
if %zzt1% GEQ 1441 goto :minutes2error
goto :checkpattern
:minutes1error
rem:minutes1error
echo.
echo Error: Minutes must be a number greater than 0.
echo.
call :cleanexit
exit /b 4
:minutes2error
rem:minutes2error
echo.
echo Error: Minutes must be a number not greater than 1440 (1 day).
echo.
call :cleanexit
exit /b 5
:checkpattern
rem:checkpattern
rem pattern must have at least 1 "*"
if "%zzmatchpattern%."=="." goto :patternerror
echo "%zzmatchpattern%"|find "*">nul
if %errorlevel% EQU 0 goto :start
rem goto :patternerror
:patternerror
rem:patternerror
echo.
echo Error: Pattern cannot be empty, and must contain at least 1 "*".
echo.
call :cleanexit
exit /b 6
:start
rem:start
set /a zzdelayseconds=zzdelayminutes*60
set /a zzpartialdelay=zzdelayseconds/3
set zzexiting=0
:restart
rem:restart
rem if destination file already exists, wait a bit more
if not exist "%zzdestpath%%zzfinalname%" goto:checkaborted
call :waitexternal
if %zzexiting% NEQ 0 exit /b %zzexiting%
goto :restart
:checkaborted
rem:checkaborted
if not exist "%zzsourcepath%%zzfinalname%" goto :work1
echo.
echo Completing previously started file move.
echo Moving file: "%zzsourcepath%%zzfinalname%" to "%zzdestpath%"
move "%zzsourcepath%%zzfinalname%" "%zzdestpath%">nul 2>&1
goto :restart
:work1
rem:work1
set zzdelayflag=1
set zzsuccess=0
for %%f in ("%zzsourcepath%%zzmatchpattern%") do call :work2 "%%~f"
if %zzexiting% NEQ 0 exit /b %zzexiting%
echo %zzsuccess% file(s) processed.
if %zzdelayflag% EQU 0 goto :checksuccess
call :dodelay %zzdelayseconds%
if %zzexiting% NEQ 0 exit /b %zzexiting%
goto :restart
:checksuccess
rem:checksuccess
if %zzsuccess% NEQ 0 goto :restart
echo.
echo Failed to rename all existing files, exiting.
call :cleanexit
set zzexiting=7
exit /b %zzexiting%
goto :EOF
:work2
rem:work2
if %zzexiting% NEQ 0 exit /b %zzexiting%
set "zz_w1=%~1"
set zzdelayflag=0
echo.
echo Renaming file "%zz_w1%" to "%zzfinalname%"
ren "%zz_w1%" "%zzfinalname%">nul 2>&1
if exist "%zz_w1%" goto :renameerror
if not exist "%zzsourcepath%%zzfinalname%" goto :renameerror
goto :movefinalfile
:renameerror
rem:renameerror
echo Error: Failed to rename file "%zz_w1%" to "%zzfinalname%"
echo.
rem Rename failed, skip it and get next file (immediately), if any.
goto :EOF
:movefinalfile
rem:movefinalfile
rem Rename success, move the file
if not exist "%zzdestpath%%zzfinalname%" goto :domove
call :waitexternal
if %zzexiting% NEQ 0 exit /b %zzexiting%
goto :movefinalfile
:domove
rem:domove
echo Moving file: "%zzsourcepath%%zzfinalname%" to "%zzdestpath%"
move "%zzsourcepath%%zzfinalname%" "%zzdestpath%">nul 2>&1
if exist "%zzsourcepath%%zzfinalname%" goto :moveerror
goto :movecomplete
:moveerror
rem:moveerror
echo Error: Failed to move file "%zz_w1%" to "%zzdestpath%%zzfinalname%"
rem Move failed, restore (un-rename) file and skip it and get next file (immediately), if any.
for %%g in ("%zz_w1%") do set "zzt1=%%~nxg"
echo Restore file: "%zzsourcepath%%zzfinalname%" to "%zzt1%"
ren "%zzsourcepath%%zzfinalname%" "%zzt1%">nul 2>&1
echo.
goto :EOF
:movecomplete
rem:movecomplete
set /a zzsuccess+=1
call :dodelay %zzdelayseconds%
if %zzexiting% NEQ 0 exit /b %zzexiting%
rem echo.
rem Go get next file, if any.
goto :EOF
:dodelay
rem:dodelay
set zztime=%1
if /I "%zzdelaytype%."=="P." goto :dopauseddelay
echo.
timeout /t %zztime%
goto :EOF
:dopauseddelay
rem:dopauseddelay
echo.
echo Waiting (%zztime% seconds) to process next file... You can wait to
echo continue after delay or Q(uit) or C(ontinue) now.
choice /c cq /n /t %zztime% /d c /m "Press Q(uit) or C(ontinue now) or No Action (wait) to continue after delay. "
if %errorlevel% EQU 1 goto :EOF
echo.
echo User selected Q(uit), exiting.
call :cleanexit
set zzexiting=254
exit /b %zzexiting%
goto :EOF
:waitexternal
rem:waitexternal
echo.
echo Waiting for externl process...
call :dodelay %zzpartialdelay%
if %zzexiting% NEQ 0 exit /b %zzexiting%
goto :EOF
:cleanexit
rem:cleanexit
set "zzdelayflag="
set "zzdelayminutes="
set "zzdelayseconds="
set "zzdelaytype="
set "zzdestpath="
set "zzexiting="
set "zzfinalname="
set "zzmatchpattern="
set "zzpartialdelay="
set "zzsourcepath="
set "zzsuccess="
set "zzt1="
set "zzt2="
set "zztime="
set "zz_w1="
goto :EOF
如果您需要修改或解释任何内容,请告诉我。