[BAT] 以当前时间为名创建文件夹,将本地文件夹里的文件拷贝到远程共享目录,而且保证本地和Jenkins上运行都成功

时间:2021-10-31 13:06:01
@echo off

rem connect to szotpc801
net use * /del /yes
NET USE X: \\10.66.234.95\d$ Autotest123 /user:SZDOMAIN1\autotester set AutoPath=%~dp0
%AutoPath:~0,2%
pushd %AutoPath% cd /d %AutoPath% set sourcePath=%AutoPath%\TestResult\PA
set targetPath=X:\\AutomationReport\PA set tempStr=%date:/=-%-%time::=-%
set directoryName=%tempStr: =-%
echo I will create a directory : %directoryName% if exist %targetPath%\%directoryName% (echo y|cacls %targetPath%\%directoryName% /p everyone:f >nul 2>nul &&rd /s /q %targetPath%\%directoryName%) else echo directory doesn't exist,create it md %targetPath%\%directoryName%
xcopy /d %sourcePath% %targetPath%\%directoryName% rem delete reports generated 7 days ago
forfiles /p %targetPath% /s /m *.* /d -7 /c "cmd /c del /f @path">nul 2>nul
for /f "tokens=*" %%a in ('dir /b /ad /s^|sort /r') do rd "%%a" 2>nul net use * /del /yes exit

  要点:

1.NET USE 需要用IP地址,不能用主机名

2.%date%,%time%在本地和Jenkins上的格式不一样,不能通过取第几个字符的方式来组成文件夹名称

3.文件夹名不能包含斜杠,冒号,空格,需要将它们替换成横杠或其他字符

%date:/=-%    %time::=-%    %tempStr: =-%