sambec22
asked on
date batch file
I need a batch file that will get the current system date and then subtract one day. Create a folder with this date as it's name. Then copy files from a specfic location (like C:\data) to this dated folder. Can this be done with a batch file and can some one show me the code needed?
this should do part of the job, you'll have to modify last line to create the folder and copy files
@echo off
set /a d=%date:~0,2%
set /a m=%date:~3,2%
set /a y=%date:~6,4%
:loop
set /a d-=1
if %d% lss 1 (
set d=31
set /a m-=1
if %m% lss 1 (
set m=12
set /a y-=1
)
)
xcopy /d:%m%-%d%-%y% /h /l "%~f0" "%~f0\" >nul 2>&1 || goto loop
echo %d%/%m%/%y%
set foldername=%y%%m%%d%
md %foldername%
using powershell
$foldername = [DateTime]::Now.Date.AddDa ys(-1).ToS tring('yyy yMMdd')
md "$foldername"
copy c:\Data\*.* -Recurse "c:\$foldername"
$foldername = [DateTime]::Now.Date.AddDa
md "$foldername"
copy c:\Data\*.* -Recurse "c:\$foldername"
My article here shows hwo to get yesterdays date with a single line of VBScript embedded in a batch file. The example at the bottom there gives you basically what you want:
https://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_1153-Using-dates-in-batch-files-scripts.html
If usign %date% to get the date and time it WILL cause some problems when your date format changes due to running as a different user or PC etc.
echo wscript.echo year(date - 1) ^& right(100 + month(date-1),2) ^& right(100+day(date-1 ),2) > "%temp%\dateparts.vbs"
for /f "tokens=1 delims=" %%a in ('cscript //nologo "%temp%\dateparts.vbs"') do set yesterday=%%a
echo Yesterday was %yesterday%
md C:\archive\%yesterday%
xcopy /s c:\data\*.* C:\archive\%yesterday%
Steve
https://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_1153-Using-dates-in-batch-files-scripts.html
If usign %date% to get the date and time it WILL cause some problems when your date format changes due to running as a different user or PC etc.
echo wscript.echo year(date - 1) ^& right(100 + month(date-1),2) ^& right(100+day(date-1 ),2) > "%temp%\dateparts.vbs"
for /f "tokens=1 delims=" %%a in ('cscript //nologo "%temp%\dateparts.vbs"') do set yesterday=%%a
echo Yesterday was %yesterday%
md C:\archive\%yesterday%
xcopy /s c:\data\*.* C:\archive\%yesterday%
Steve
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Thanks for posting this online. I used this code in a file naming convention that required today's date -1.
-byd2k
-byd2k
@byd2k
Great, glad it was helpful.
~bp
Great, glad it was helpful.
~bp
http://www.dostips.com/DtTipsDateTime.php that is a starting point julien days -1