Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
@echo off
setlocal
:: <config>
:: CONFIGURATION STUFF BELOW
:: Base folder where the new sub-folders will be created. Do NOT include a trailing slash [eg, c:\temp is ok, but not c:\temp\]
set "baseDir=C:\TEMPOUTPUT"
:: The second destination to recieve a copy of these files
set "baseDir2=C:\TEMPOUTPUT2"
:: New folder to create. Do not include the timestamp [eg, MyBackupFolder will cause a folder called MyBackupFolder-20180101-123201 to be created]
set "newFolderName=MyBackupFolder"
:: Source files to copy from are located here
set "sourceFolderPath=C:\TEMPINPUT"
:: END CONFIGURATION STUFF
:: </config>
pushd "%~dp0"
>nul 2>nul dir /a-d /s "%sourceFolderPath%\*" && (echo Files exist) || (echo No files found & goto :done)
call :getCurrentTimestamp result
set "tmpNewFolderName=%newFolderName%-%result%"
set "fullPath=%baseDir%\%tmpNewFolderName%"
set "fullPath2=%baseDir2%\%tmpNewFolderName%"
echo %tmpNewFolderName%
if not exist "%fullPath%" (mkdir "%fullPath%")
if not exist "%fullPath2%" (mkdir "%fullPath2%")
xcopy /y /e "%sourceFolderPath%\*.*" "%fullPath%"
move /y "%sourceFolderPath%\*.*" "%fullPath2%"
goto :done
:done
popd
endlocal
echo.
pause
echo.
goto :eof
:getCurrentTimestamp
setlocal
:: This will fix a bug that existed due to hours not having leading 0s for some reason. Add leading zero to hour...
call :trimString result %TIME:~0,2%
set hour=0%result%
set hour=%hour:~-2%
set timestamp=%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%-%hour%%TIME:~3,2%%TIME:~6,2%
call :trimString result %timestamp%
set timestamp=%result%
endlocal & set "%~1=%timestamp%"
goto :eof
:trimString
setlocal EnableDelayedExpansion
set input=%~2
for /f "tokens=* delims= " %%a in ("%input%") do set input=%%a
for /l %%a in (1,1,100) do if "!input:~-1!"==" " set input=!input:~0,-1!
endlocal & set "%~1=%input%"
goto :eof
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment