创建批处理文件以将结果复制到目录

时间:2014-04-30 15:15:53

标签: batch-file

我正在使用硒运行测试。测试运行后,我想将结果复制到具有今天日期的目录。如果我再次运行测试,我想将新结果复制到具有今天日期的新目录中,然后是运行编号。因此,如果它是我第二次运行测试,它将是“今天的日期 - 运行2”。最终我希望能够自动增加文件夹名称中的运行编号,但是现在我对它进行了硬编码。此外,我知道我可以在我的文件夹名称中使用时间戳来帮助解决这个问题,但这是我不想做的事情。

以下是我的批处理文件中的内容。当我运行测试然后第一次复制结果时,它成功为变量a创建了一个文件夹,并将结果复制到变量a中。问题是当我第二次运行测试并尝试复制新结果时。它成功地为变量b创建了一个文件夹,并将结果复制到变量b中,但它也将这些结果再次复制到变量a中。从而覆盖我在该文件夹中已有的内容。

title Copy the Chrome results to the results folder

REM Code for creating a folder with today's date
REM set date=%date:~10,4%-%date:~4,2%-%date:~7,2%\%time:~0,2%%time:~3,2%
set a=%date:~10,4%-%date:~4,2%-%date:~7,2%
set b=%date:~10,4%-%date:~4,2%-%date:~7,2%-Run 2


REM Create HTML directory and copy results
if EXIST "C:\SeleniumGrid\Results\%a%\Basic_Survey\test_output\html\Chrome\" (
md "C:\SeleniumGrid\Results\%b%\Basic_Survey\test_output\html\Chrome\"
copy "C:\Automation Workspace\Survey\Basic_Survey\test-output\html\*.*" "C:\SeleniumGrid\Results\%b%\Basic_Survey\test_output\html\Chrome\"
)
ELSE (
md "C:\SeleniumGrid\Results\%a%\Basic_Survey\test_output\html\Chrome\"
copy "C:\Automation Workspace\Survey\Basic_Survey\test-output\html\*.*" "C:\SeleniumGrid\Results\%a%\Basic_Survey\test_output\html\Chrome\"
)

REM Create screenshot directory and copy results
if EXIST "C:\SeleniumGrid\Results\%a%\Basic_Survey\test_output\screenshots\Chrome\" (
md "C:\SeleniumGrid\Results\%b%\Basic_Survey\test_output\screenshots\Chrome\"
copy "C:\Automation Workspace\Survey\Basic_Survey\test-output\Report_for_Basic_Survey_Chrome\screenshots\*.*" C:\SeleniumGrid\Results\%b%\Basic_Survey\test_output\screenshots\Chrome\
)
ELSE (
md "C:\SeleniumGrid\Results\%a%\Basic_Survey\test_output\screenshots\Chrome\"
copy "C:\Automation Workspace\Survey\Basic_Survey\test-output\Report_for_Basic_Survey_Chrome\screenshots\*.*" C:\SeleniumGrid\Results\%a%\Basic_Survey\test_output\screenshots\Chrome\
)

1 个答案:

答案 0 :(得分:1)

这应该为您提供date-Run 2格式,其中第一个文件夹名称为date,然后在每次使用时自动递增运行编号。

检查第一个位置并计算正确的数字 xcopy命令可以随时创建文件夹。

@echo off
title Copy the Chrome results to the results folder

REM Code for creating a folder with today's date
set "a=%date:~10,4%-%date:~4,2%-%date:~7,2%"
set c=0

if not EXIST "C:\SeleniumGrid\Results\%a%\Basic_Survey\test_output\html\Chrome\" (
   set "name=%a%"
   goto :start
)

:loop
set /a c+=1
set  "name=%a% - Run %c%"
if EXIST "C:\SeleniumGrid\Results\%name%\Basic_Survey\test_output\html\Chrome\" goto :loop

:start

REM Create HTML directory and copy results
xcopy "C:\Automation Workspace\Survey\Basic_Survey\test-output\html\*.*" "C:\SeleniumGrid\Results\%name%\Basic_Survey\test_output\html\Chrome\"

REM Create screenshot directory and copy results
xcopy "C:\Automation Workspace\Survey\Basic_Survey\test-output\Report_for_Basic_Survey_Chrome\screenshots\*.*" C:\SeleniumGrid\Results\%name%\Basic_Survey\test_output\screenshots\Chrome\