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

@echo off

rem connect to szotpc801
net use * /del /yes
NET USE X: \10.66.234.95d$ Autotest123 /user:SZDOMAIN1autotester 

set AutoPath=%~dp0
%AutoPath:~0,2%  
pushd %AutoPath%

cd /d %AutoPath%

set sourcePath=%AutoPath%TestResultPA
set targetPath=X:\AutomationReportPA

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: =-%