using batch command file copy on daily basis - batch-file

I need to copy files from one machine to my local disc using batch commands.
My files contain the date in dd-mm-yyyy format. But in case if i am using this:
-%date:/=-%
it is interpreted as "day" and then date which is not really associated with my file name at the source end.
Once I click my bat file it will copy one folder say "xyz dd-mm-yyy" I want to copy only today's folder as on my source machine saved last 7 days folder.
EDIT: my folder name on source machine is Site_info 11-07-2011 and tomorrow one more folder will get added to the same machine with name site_info 12-07-2011. I want to run the bat file machine where it will copy only today's folder.
EDIT2: Thanks for kind support still i am not able to achieve my target.If possible please provide the commands for following situation
My machine path :c:\Documents and Settings\user1\Desktop\SITE_INFO\Site_info 12-07-2011 where Site_info 12-07-2011 will change to site_info 13-07-2011
Source address :- \97.253.72.127\Cdma Site_info\Site_info 12-07-2011 Tomorrow one more folder will get add on this path with dated like \97.253.72.127\Cdma Site_info\Site_info 13-07-2011

If you mean that there's the 'day of week' part preceding the date and you want to get rid of that part, then you'll probably need an additional variable. First you'll cut off the day of week, then replace / with -, like you are already doing. It might be something like this:
…
SET "dateonly=%date:~4%"
SET "dateonly=%dateonly:/=-"
…
COPY \\computer\share\path\whatever-%dateonly%.ext drive:\path\%dateonly%\
…

You could try to puzzle the date the way you want it:
set mydate=%date:~-10,2%-%date:~-7,2%-%date:~-4%
xcopy /E /I "Site_info %mydate%" "Copy\Site_info %mydate%"

Related

How can I find the newest files in a specific folder and all of it's subfolders in a cmd?

I want to keep my database updated, so every time I get the latest files from TFS I want to find the changed files (newest in date) in order to run them and not run ALL of the files.
I want to create a batch file to search, find and copy them to a temp folder so I'll be able to run only them.
A creative solution would be you can enter dir results into a text file and then whichever language you are using just do string slicing which can give u date, memory and name of the file. This is command- dir/a>file replace file with the text file. if u want the text to be entered into the file in append mode then dir/a>>file

Batch File DIR Command to Text File Without Overwrite [duplicate]

This question already has answers here:
How do I get current date/time on the Windows command line in a suitable format for usage in a file/folder name?
(30 answers)
Closed 1 year ago.
I'm trying to preserve the dates of files that I'm backing up onto an external drive, in the unlikely event that the dates get messed up for whatever reason (I had a previous experience where I lost date information and had no backup). I'm doing this through a batch file containing the following:
#ECHO OFF
cd E:\PCBackup
dir /s > dirlist.txt
I would simply run this batch file after running my backup using FreeFileSync. Then, if I need to, I can search the txt file for the filename and see its corresponding date.
However, when this batch file runs, if there is a previous dirlist.txt, then it is overwritten with the new dirlist.txt. So, in a scenario where the dates get messed up and I don't yet realize it, if I run this batch file, it will overwrite the previous dirlist.txt with one that has the messed up dates, and I'd lose the date information!
So, what I think I want it to do is, if dirlist.txt already exists, then create a new one, say something like dirlist1.txt, so that I can have several "backups" of the text file that I can manually delete if necessary.
I've seen that one can instead use >> with something like dir /s >> dirlist.txt to append to an existing file instead of overwriting, but I don't want to append if I don't have to, I'd still like to create a new file.
Is there a way to accomplish this? I'm also open to alternative/simpler ways of preserving the dates, if there are any. Please keep in mind that I know little about CMD commands or programming, outside of a computer science course I took years ago. Thank you.
You will be told there are umpteen duplicate ways to do this so in this 22 nd year of the 1st century :-) Windows has no native way of returning a sequential Iso Date the primary answer will be use powershell and for my locale it needs to be called in a suitable format, introducing a delay.
powershell get-date -format "{yyyy-MMM-ddTHH_mm+01Z}"
Note:- colons : are not allowed, and for me 20 seconds later on one machine (but it does get faster with use) and 12-5 seconds later on this one, I get
2021-07-07T21_55+01Z
but actually its now 2021-Jul-07 21:56
I have found that the MakeCab method is faster and reliable but again the format is not pure sequencing and the Jul will NOT appear before Dec in a file list without significant batch file processing.
2021-Dec-31 23:00:00.txt
2021-Jul-08 21:54:20.txt
So in a .cmd I prefer a more instant result thus my clock is set to International dates (You will need to look at your LOCALE clock setting bottom right for your own construction.)
set isodate=%date:~0,10%
instantly returns
isodate=2021-07-07 and I can then use that for filename
#ECHO OFF
cd E:\PCBackup
set "isodate=%date:~0,10%"
dir /s > %isodate%-dirlist.txt
dir returns includes 2021-07-07-dirlist.txt
If you want to run several times in a day use
#ECHO OFF
cd E:\PCBackup
set "isodate=%date:~0,10%"
set "isotime=%time:~0,2%-%time:~3,2%-%time:~6,2%"
dir /s > %isodate%T%isotime%+01Z-dirlist.txt
Amend that any way you wish for your timezone, thus your own clock whatever your date format be it :-
31/2021/12
look at the way I split %time :~ start#base 0 , # of chars %-
one example for an "English" clock date of 31/12/2021 would be simply reverse to
"isodate=%date:~6,4%-%date:~3,2%-%date:~0,2%"
For American %date%=Thu 07/08/2021 use
"isodate=%date:~10,4%-%date:~4,2%-%date:~7,2%"

Rename .bak file to include date that was 7 days in the past

You'll have to forgive me as I am a beginner when it comes to batch file and Powershell scripting.
We have a backup solution with SQL that is currently being tested, which will perform Full backups once a week and then Differental backups for the reminder of the weekdays. As part of the restore process, we have a Windows batch script file in place which will rename the Full backup file before compressing it with 7Zip. The rename portion of the script looks like this:
cd C:\SQL Backups\Production Backups\eStart_Good
set date=%date:~-4%_%date:~3,2%_%date:~0,2%
ren *%date%* eStart_Good_FullBackup.bak
As this command will get run once a week on the night of a Full backup, it looks for today's date in the form of eg. 2016_03_02 because that's how the naming convention is set when backing up from SQL. This works as it should do.
However, what I want to happen after a Full backup from SQL as occurred is rename the previous eStart_Good_FullBackup file by taking out the 'FullBackup' portion of the name and replace with the date 7 days previous to today's date. So if the next Full backup is set to run on 04/03/2016, I want it to show up in the file name as 2016_02_26 for example.
I know with the rename command I need to change it to:
ren FullBackup eStart_Good_%date%.bak (FullBackup being a wild card.)
But, I've tried a number of combinations to change the set date variable and I keep getting the wrong numbers or pieces of code when echoing the variable.
set date=%date:~-4%_%date:~3,2%_%date:~0,2%
Does anyone have any advice they can give me please?
Working with dates in batch scripts is... complicated. Powershell might be much simpler way for you to work with this. However, sometimes Powershell isn't an option and you just need to do it with a batch script. Here's a script I wrote for a how to figure out 'yesterday': How do I find last Sunday's date and save it in a variable in a Windows batch file. You can make it loop seven times to get seven days ago.
Hope this helps.

Batch file to take backup of the files

I am new to this , Actually my requirement is to take backup of some XML file from one machine to another machine .So my senario would be when i click on the batch file it will create a new folder on the second server (name of the folder should be date and time )and under this folder it paste all the copied files .
I need to create a new folder every time as i am doing version control.
Thanks
VG
The batch file is very simple
#echo off
setlocal
set "copydest=%date%_%time%"
for %%i in (/ - : .) do call set "copydest=%%copydest:%%~s=%%
xcopy "c:\path\to\your\datafiles\*.xml" "c:\parent\%copydest%\"
which should copy the .xml files from the directory c:\path\to\your\datafiles\ to a new directory under c:\parent\which has the date/time as its name.
The precise structure of that date/time depends on your settings. If you don't tell us what your settings are, we'd have to write a book about all of the possible combinations - not going to do that.
The for %%i... line does the removal of the common date/time separators; of these : and / are the important ones since they are invalid in a filename.

How to create a batch script to copy newly created folder with the current date

im not good in batch scripting. Hope someone can help me with this.
What my problem is, everyday my program creates a dated folder. I want a automated daily backup scripts that will do the job, I want only the newly created folder with the current date or yesterdays date to copy and rar then send to other directory and the rest of the files and subdirectory remains. Btw, My Program creates a folder name according to current date.
here is the sample
source todays date: february 26 20013
C:\MyApp\20130226 <new folder
\20130225 <old folder
\20130224 <old folder
destination todays date: february 26 20013
D:\Backup\20130226.rar << newly backup according to current date.
Is this possible? Thank you in advance
Date stuff in batch files is notoriously finicky and tricky.
There are basically two ways of going about that:
Use %DATE% and cut it to appropriate pieces, e.g. for me it would look like this:
> echo %DATE%
2013-02-26
> echo %DATE:~0,4%%DATE:~5,2%%DATE:~8,2%
20130226
This has the problem that it's dependent on the date format in your current locale. As you can see, I am using ISO-8601 (the only sane date format imho) which makes this rather easy.
It works well enough for one-off scripts that you use in a clearly defined environment and have no actual requirements of robustness. I tend to avoid that, though.
Use WMI to get the current date:
> wmic os get localdatetime
LocalDateTime
20130226113553.324000+060
You can store that output in a variable with for /f:
for /f "skip=1" %%x in ('wmic os get localdatetime') do if not defined MyDate set MyDate=%%x
Since that format is fixed you can safely use substrings to access the individual parts:
echo %MyDate:~0,8%
I'll leave the actual file copying as an exercise to you.

Resources