批处理脚本上的数组
Posted
技术标签:
【中文标题】批处理脚本上的数组【英文标题】:Arrays on batch script 【发布时间】:2019-02-15 19:54:29 【问题描述】:大家,
我正在尝试使用svnadmin dump --incremental
命令进行差异备份。我的想法非常基本,使用命令 dir 读取多个 svn 存储库并将其放入名为“input”的文本文件中,逐行读取并放入变量中。
使用完整备份脚本获取存储库的最新版本,并写入名为“输出”的文本文件并放入变量中。
一切都完成了,但我不能使用 svnadmin 转储,因为我需要循环内的变量(数组)。我该怎么做?请帮帮我,我试了很多次还是不行):
@echo off
cls
:: =================== CONFIG ============================================
:: Path of the dir containing your repos [Note Trailing slash]
SET repodir=E:\svn\repositorios
:: Path of the dir in which to create you dumps [Note Trailing slash]
SET repodump=E:\Backup\SVN
:: File to read (multiple directories)
SET "File2ReadInput=E:\Backup\SVN\input.txt"
SET "File2ReadOutput=E:\Backup\SVN\output.txt
SET "File2ReadOutputNew=E:\Backup\SVN\New\output_new.txt
:: =================== CONFIG ============================================
:: =================== SCRIPT ============================================
rem deleting old file output.txt
del E:\Backup\SVN\output.txt
rem deleting old file output_new.txt
del E:\Backup\SVN\New\output_new.txt
rem creating new file output.txt
for /f "delims=" %%f in ("E:\Backup\SVN\*.data") do type %%f >> E:\Backup\SVN\output.txt
rem creating input file
dir %repodir% /ad /b > E:\Backup\SVN\input.txt
rem reading input file
setlocal EnableExtensions EnableDelayedExpansion
for /f "delims=" %%a in ('Type "%File2ReadInput%"') do (
set /a count+=1
set "Line[!count!]=%%a"
)
For /L %%i in (1,1,%Count%) do (
echo "Var%%i" is assigned to ==^> "!Line[%%i]!"
echo E:\svn\repositorios\!Line[%%i]!
set REPO_NAME=!Line[%%i]!
:: getting new transactions to compare
for /F "delims=" %%g in ('svnlook youngest E:\svn\repositorios\!Line[%%i]!') do (
echo %%~g
) > E:\Backup\SVN\New\last_rev_!Line[%%i]!_new.data
)
:: creating a new file with new datas
for /f "delims=" %%h in ("E:\Backup\SVN\New\*.data") do type %%h >> E:\Backup\SVN\New\output_new.txt
rem reading output file (dento do segundo for porque eu preciso do !Line[%%i]!)
setlocal EnableExtensions EnableDelayedExpansion
for /f "delims=" %%b in ('Type "%File2ReadOutput%"') do (
set /a out_count+=1
set "Output[!out_count!]=%%b"
)
for /L %%n in (1,1,%out_count%) DO (
echo "Var%%n" is assigned to ==^> "!Output[%%n]:~0,-1!"
set PREVIOUS=!Output[%%n]!
)
for /F "delims=" %%u in ('svnadmin dump -r!Output[%%n]:~0,-1!:HEAD E:\svn\repositorios\!Line[%%i]! --incremental') do (
echo %%~u
)> > E:\Backup\SVN\svn_backup_diff_!Line[%%i]!.dmp
【问题讨论】:
我建议你阅读this answer 【参考方案1】:如果没有完全看透您的代码 IMO 第 47..50 行的括号设置不正确,请尝试更改为:
(for /F "delims=" %%g in ('svnlook youngest E:\svn\repositorios\!Line[%%i]!') do echo %%~g
) > E:\Backup\SVN\New\last_rev_!Line[%%i]!_new.data
像 Notepad++ 这样的编辑器有助于查找/可视化相应的括号对。
【讨论】:
以上是关于批处理脚本上的数组的主要内容,如果未能解决你的问题,请参考以下文章