create a batch file which copies text files of specified date - batch-file

I want to create a batch file which copies text file by taking user input. As an input user give from date and to date. The name of the text files are like: error_log_04_06_2011
Now for example I want data from 04/06/2011 to 07/06/2011, so i can do that by using xcopy command like this
#ECHO OFF
SET /P f=Please Enter from date(m-d-yyyy):
SET /P t=Please Enter to date(mm-dd-yyyy):
mkdir d:\bkp
xcopy /S /D:%f% /EXCLUDE:%t% C:\Emulator\Log_Data d:\bkp
This will copy all the file from the date mentioned. Now I want to give "to" date also.so i can copy files of a certain period.
One approch is that I use delete command and delete extra files that are copied. But Delete command doesnt have any switch regarding date. So plz help.....

Unfortunately, there isn't much that can help in this area built-in to batch/cmd. dir can take a flag to order its output by modified time, but I'm guessing you want it to depend on just the filenames and it still wouldn't give you the range you want.
The filename you have shown isn't sortable. If they were named in YYYY_MM_DD format, you could find the files in the date range with sort and a for loop.
Without that, you will end up needing to do date math to generate each individual date in your range, convert each date to your filename format, and pass that to copy.
Rob Van Der Woude has a good list of date functions in batch, including the harder date math. It would be a good place to start. http://www.robvanderwoude.com/battech.php#Time

I haven't tried it, but you could use the xcopy /d and /l switches to generate lists of files that are dated after a certain date to avoid doing the maths.
After the xcopy from the first date you could drive a delete in a for /l loop from a list of file dated after the second date.

Related

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%"

XCOPY to only look back one day

I have a current .BAT file that is functional but I need to be more specific with it. Currently, it will only copy over files that do not exist. What I am wanting to do is to only look back ONE day and copy those files. I have posted my file information below. Thank you in advance for the assistance.
XCOPY /y "w:\EFileRequests\*.xml" "\\DIS2\EFilingXML\Archive\"/D
Use robocopy, it has a parameter for exactly what you want.
/MAXAGE:n :: MAXimum file AGE - exclude files older than n days/date.

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.

Removing date and time stamp from a string with spaces in batch

I am making a program that automatically backs up files, stores up to a maximum of five of them, and has an option to restore any of the five files. When the files are backed up, it adds a date and time stamp in the format of YYYYMMDD_HHMMSS_filename.ext. In order to restore the file, it is necessary to first cut out the date stamp.
The method I am currently using to cut the date stamp off of the beginning of the file is as follows.
set VAR=%VAR:~16%
echo %VAR%
The problem being, if the backed up file is called "20120825_140343_file name.txt", the above method will only return "file," omitting anything after the space. The spaces in the file names need to be preserved for them to be recognized by the program using them.
tl;dr I need to cut the string 20120825_140343_file name.txt into just "file name.txt", but my method just returns "file."
If delimiters or something would help I could separate the date stamp and file name with a different character, I.E. 20120825_140343-file-name.txt
Your method, though inelegant and inflexible, should work. This tells me that you are not storing the entire filename in VAR. That is why %var:~16% only results in file, and not file name.txt. I assume that you assign VAR like this somewhere:
SET VAR=%1
You'll need to either do this:
SET VAR=%1 %2
Or insert double-quotes around the file name when you call your batch file, and then set var like this to remove the quotes:
SET VAR=%~1
That should be enough to get your batch to work.
====================================================
But, to answer the question you actually asked, I'll show you a method of extracting "file name.txt" from var that will work even if there are more or even less than 16 prefix characters.
Use the for /f statement, specify the 3rd token, with underscores as a delimiter. Here is a self-contained example. (To run from the command-line, change %%x to %x.
SET VAR=20120825_140343_file name.txt
for /f "tokens=3 delims=_" %%x in ("%VAR%") do set VAR=%%x
ECHO %VAR%
Just remember, this solution will NOT fix your problem if you do not fix your code to assure your VAR variable has the entire filename in it.
Have you tried this?
set VAR="%VAR:~16%"

using batch command file copy on daily basis

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%"

Resources