Batch job archiving cleanup - date

I am using below script to append today's date and move files to an archive directory in following format:
XX-XXXX__2019-04-01.xlsx
XX-XXXX__2019-04-01.txt
Where XX-XXXX is a variable filename and 2019-04-01 in this example is today's date. I would now like to expand the archiving process to check the current date and also delete files older than 2 days - i.e. from 30 March and older. Any suggestions on how this can be achieved will be greatly appreciated, thanks!
#echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set DateTime=%%a
set Yr=%DateTime:~0,4%
set Mon=%DateTime:~4,2%
set Day=%DateTime:~6,2%
set Hr=%DateTime:~8,2%
set Min=%DateTime:~10,2%
for %%I in ("C:\Temp\Test_File\*.xlsx") do move "%%~I" "C:\Temp\Test_File\Archive\Processed\%%~nI__%Yr%-%Mon%-%Day%%%~xI"
for %%I in ("C:\Temp\Test_File\*.txt") do move "%%~I" "C:\Temp\Test_File\Archive\Logs\%%~nI__%Yr%-%Mon%-%Day%%%~xI"

Related

How to create a batch that creates a directory named the current date and time, and then copy files in it?

I have to seperate codes.
This creates a directory with only the date, but i don't know how to put the time on the end.
for /f "tokens=1* delims=" %%a in ('date /T') do set datestr=%%a
mkdir c:\%date:/=%
And I have this to copy the files:
robocopy "%appdata%\saves" "C:\Users\redfi\OneDrive\Savesbackup" /e /xf
They both work individually, but I want to put them in one batch.
I want it to create the directory with the current date and time, and then copy the saves in it. So I can restore older saves if I want.
Thank you!
I managed to figure it out. You might need to edit the date formats.
echo off
set CUR_YYYY=%date:~0,4%
set CUR_MM=%date:~5,2%
set CUR_DD=%date:~8,2%
set CUR_HH=%time:~0,2%
if %CUR_HH% lss 10 (set CUR_HH=0%time:~1,1%)
set CUR_NN=%time:~3,2%
set CUR_SS=%time:~6,2%
set CUR_MS=%time:~9,2%
set SUBFILENAME=%CUR_YYYY%.%CUR_MM%.%CUR_DD%_%CUR_HH%.%CUR_NN%
mkdir C:\Users\redfi\OneDrive\Minecraftbackup\%SUBFILENAME%
robocopy "%appdata%\.minecraft\saves" "C:\Users\redfi\OneDrive\Minecraftbackup\%SUBFILENAME%" /e /xf

Strip date from filename using batch script

I have multiple files with dates on them I would like to strip.
exOpTimer01232018.txt
exOpProcess01232018.txt
exOpFac01232018.txt
exOpProd01232018.txt
I would like to have a batch script remove the date and leave result such as
exOpTimer.txt
exOpProcess.txt
exOpFac.txt
exOpProd.txt
These are monthly file and the date stamp changes every month.
I have tried doing
RENAME C:\temp\*????????.txt *.txt
But wasn't successful.
Example based on my comment:
#Echo Off
For /F "Delims=" %%A In ('Where .:exOp*.txt 2^>Nul') Do Call :Loop "%%A"
Pause
Exit
:Loop
Set "fName=%~n1"
Ren %1 "%fName:~,-8%%~x1"

Batch: Xcopy current date

So https://technet.microsoft.com/en-us/library/cc771254.aspx describes the /d parameter of the xcopy command as allowing you to only copy the source file changed on or after the specified date, so I wanted to use the underneath command but with the current date. Does anyone know how to get the current date in batch and properly format it within the below command?
xcopy /d [:MM-DD-YYYY]
#echo off
for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if '.%%i.'=='.LocalDateTime.' set ldt=%%j
set yyyy=%ldt:~0,4%
set mm=%ldt:~4,2%
set dd=%ldt:~6,2%
echo xcopy source destination /D:%mm%-%dd%-%yyyy% /L
remove echo in front of xcopy, edit source and destination. /L is to list only, no destructive command.

Re-naming a file name to include yesterday's date using command prompt

I am trying to rename some log files to yesterday's date when the batch file creates a new file of same name every night.
We can rename the file to today's date using the below cmd
ren SampleDTE.TXT SampleDTE-%date:~10,4%%date:~7,2%%date:~4,2%_%time:~0,2%%time:~3,2%.TXT
This results in file renamed to // SampleDTE-YYYYDDMM_hhmm.TXT
SampleDTE-20132712_1243.TXT
I wanted to know how to re-name the file to yesterday's date. Something like
SampleDTE-20132612_1243.TXT
Thanks in advance
The easy way - assuming that you run this regularly, once per day
FOR /f %%a IN (sampledteyesterday.txt) DO ECHO ren SampleDTE.TXT SampleDTE-%%a_%time:~0,2%%time:~3,2%.txt
> sampledteyesterday.txt ECHO %date:~10,4%%day%%date:~4,2%
Note - ren command simply ECHOed. when verified, remove the ECHO keyword before the REN to activate.
You'll need to set up your sampledteyesterday.txt file containing a single line YYYYDDMM for yesterday to initialise.
Suggestion: use YYYYMMDD which sorts easier or more logically...
You will have to use a variable and do the math:
set /a day=%date:~7,2% - 1
ren SampleDTE.TXT SampleDTE-%date:~10,4%%day%%date:~4,2%_%time:~0,2%%time:~3,2%.TXT
To avoid date arithmetics, you can store yesterday date in, eg, file.
yesterday.txt (contains today and yesterday):
20131227 20131226
Batch file:
REM Get today (to check if yesterday.txt is valid):
SET today=%DATE:~10,4%%DATE:~7,2%%DATE:~4,2%
REM Read file:
FOR /F "TOKENS=1,2" %%d IN (yesterday.txt) DO (
SET stored_today=%%d
SET yesterday=%%e
)
REM If stored_today not equal to today, assume yesterday is stored_today and update file:
IF NOT "%stored_today%" == "%today%" (
SET yesterday=%stored_today%
>yesterday.txt ECHO %stored_today% %today%
)
REM Test if yesterday is set, exit otherwise.
IF "%yesterday%"=="" ECHO Yesterday unknown! Try again tomorrow.&GOTO:EOF
To make it work correctly first time, yesterday.txt must be manually filled.
This will get yesterdays date, using VBS in a batch file.
It's reliable in all locales, whereas the %date% variable can be different on different computers, and different users.
#echo off
set day=-1
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%day%,now) : d=weekday(s)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^& right(100+month(s),2)^& right(100+day(s),2)
for /f %%a in ('cscript /nologo "%temp%\%~n0.vbs"') do set "result=%%a"
del "%temp%\%~n0.vbs"
set "YYYY=%result:~0,4%"
set "MM=%result:~4,2%"
set "DD=%result:~6,2%"
set "date-yesterday=%yyyy%-%mm%-%dd%"
echo Yesterday was "%date-yesterday%"
pause

Batch file copy with date and time

I'm trying to figure this out need some help. I need to make a program that copy's my Log file and rename with date and time to different directory. Prior to loading the program not sure how to do it if any help would be great.
Here's a batch file that will copy all log files to an archive folder appending the date to each:
#Echo Off
#For /F "tokens=1,2,3 delims=/ " %%A in ('Date /t') do #(
Set Day=%%A
Set Month=%%B
Set Year=%%C
Set All=%%C%%B%%A
)
#For %%a in ("*.log") do copy %%a "archive\%%~na_%All%.LOG"