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

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

Related

Batch Script that gives unique time stamp to rename bulk files

I used this script to rename files in bulk with a unique time stamp in a batch file. The idea is to use the second and millisecond to create a unique number, which is added to the filename. The why? The filenames being used is so similar in the naming convention that it is triggering errors on the import program into the database. What doesn't trigger the error is the uniqueness of the filename, which is why I thought of a second and millisecond stamp to the prefix. Now, the code works as intended, but the stamp is not unique. It is as if the code pulls the value from the system and applies to all of the files in the folder instead of one file at a time.
Assume my file names are like this:
File1.txt
File2.txt
File3.txt
File4.txt
When the script is run, it renames the files like this:
1410File1.txt
1410File2.txt
1410File3.txt
1410File4.txt
I was hoping the script would do it like this:
1410File1.txt
1411File2.txt
1412File3.txt
1413File4.txt
The code I used to help me with this problem:
#echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set dt=%%a
set YYYY=%dt:~0,4%
set MM=%dt:~4,2%
set DD=%dt:~6,2%
set HH=%dt:~8,2%
set Min=%dt:~10,2%
set Sec=%dt:~12,2%
set Mil=%dt:~14,6%
set stamp=%Sec%%Mil%
CHDIR /D "c:\TEST\FILES\"
for %%a in (*.*) do ren "%%a" "%stamp%%%a"
Is there an additional programming code that is needed to ensure each filename is renamed differently? I'm on Windows 10 using a simple .bat file. On any given day, I would end up renaming anywhere from 600 up to 1000 files in one sitting so any help would be greatly appreciated!
Thanks in advance!
You answered your own question:
Now, the code works as intended, but the stamp is not unique. It is as if the code pulls the value from the system and applies to all of the files in the folder instead of one file at a time.
Take a look at your code. You
Compute the timestamp once at the start of the run:
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set dt=%%a
set YYYY=%dt:~0,4%
set MM=%dt:~4,2%
set DD=%dt:~6,2%
set HH=%dt:~8,2%
set Min=%dt:~10,2%
set Sec=%dt:~12,2%
set Mil=%dt:~14,6%
set stamp=%Sec%%Mil%
cd down to the desired directory:
CHDIR /D "c:\TEST\FILES\"
iterate over the files in the directory, adding the same timestamp to each file:
for %%a in (*.*) do ren "%%a" "%stamp%%%a"
You need to recompute the timestamp on every iteration of the final loop.
I would also add a pause of a few milliseconds in the loop to ensure that at least 1 millisecond has elapsed between computations of the timestamp.

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.

robocopy file structure - rename file at destination if its newer

I would like to robocopy a directory and it's subdirectories to another directory. If a file at source is newer then I would like to make a copy of this file by adding a date/time stamp at end of the filename at destination and do the copy to destination.
I do not see any switches in the robocopy to do this. Can someone guide me how to do this.
Robocopy doesn't have a renaming switch, but you can use the rename command on the resulting files to add timestamps. Here's an example batch file:
#echo off
for /f "tokens=1-3 delims=. " %%a in ('date /t') do (set mydate=%%c-%%a-%%b)
for /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do (set mytime=%%a%%b)
dir Directory1\ /b > list
robocopy Directory1\ Backup\
for /f %%f in (list) do rename Backup\%%f %%~nf%mydate%_%mytime%%%~xf
Note that you will need to change the delimiters of the date depending on which country standard you follow. You can get that by executing date /t

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"