Need programming help.
Want to add current date to a command
tried this
date /t > stu.txt
call c:\Bin\MKS\sed -e 's/\//-/g' stu.txt | c:\Bin\MKS\cut -c5-14 >stu2.txt
not sure what to do here
then show current date on this command below
c:\Bin\7ZIP\7za.exe a -t7z c:\Bin\Test11-01-2013.7z #c:\Bin\TestList.txt
thanks my programming is very rusty.
Two things that may answer your question (although with you having an odd mix of unix and Windows going there, I'm not sure this will work for you):
1) The output of date can be formatted directly: for example
date +"%m-%d-%Y"
will give
11-01-2013
2) You can get the output of a command inserted into another command by using backticks:
echo the year is `date +"%Y"`
would result in
the year is 2013
You can see how you could use that to insert your date string into a command; or you can put it in an environment variable first (handy if you have more than one place where you want to insert)
set myDate=`date +"%m-%d-%Y"`
echo $myDate
results in
11-01-2013
and you can include that in a file name (or any other command):
cat file_$myDate.txt
will expand to
cat file_11-01-2013.txt
You should be able to take these concepts and map them to what you are trying to do
Related
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%"
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.
All,
Please guide me, how to print the o/p of below command in dos window? Being new to dos command, I do not know what below string mean?
set ts = %date:~4,2%%date:~7,2%-%time:~0,2%%time:~3,2%
Thanks in advance
Welcome to the cmd prompt.
echo is the command that will display a value to the screen (ex: echo %date%)
set ts is setting a variable (ts) which can later be called like this: %ts%
%date% is a variable that will return the system date. %date:~4,2% will give the month (numeric) and %date:~7,2% gives the day of the month (numeric).
%time% is also a variable, but this on returns the time (24 hour). %time:~0,2% gives the hours (24 hour style so 1pm=13). %time:~3,2% gives the minutes.
That is setting an environment variable named ts. To display the value, add this line after it:
echo %ts%
This is setting a variable using some substring operations.
This %date:~4,2% means:
get the date from the machine;
remove the first 4 characters of it;
from the result, get the first 2 characters;
The rest is repeating this process and concatenating the result in a date and time formatted string.
I'd like to be able set the view of the p4 client (as in the output of 'p4 client -o') programmatically from a batch script.
I'm sure it would be easy in unix (one line) [See update 2] but how can I do it in windows without installing 3rd party software (e.g. grep / sed) or writing a C# program to do it (which seems overkill for the simplicity of what I'd like to do).
UPDATE :
The command above outputs comments along with settings like this;
# View: Lines to map depot files into the client workspace.
View: Path_to_depot Path_to_local
The logic I'd like to apply then is
For each line in output
if line.substring(0,5) equals "View:"
replace line with %newviewsetting%
Or if it's easier
split output with space as delimiter
if the node equals "View:" and previous node not equal to #
set the next node to %myPathToDepotSetting%
UPDATE 2 :
in unix the command would be
p4 client -o | sed 's/^View:.*/View: New view/' | p4 client -i
Which is effectively saying;
Output the text to the command line
Replace lines starting with View with my new View
Input it back to p4
I am a bit unclear as to exactly what you are trying to do. I know nothing about perforce, but quite a bit about batch files.
If you need to programmatically work with the output of p4 client -o then you want the FOR /F command. You can get FOR documentation by typing help for or for /? from a Windows command prompt.
For example, the following would simply echo the output of the command to the screen, disregarding blank lines and lines that begin with the default EOL character (;). But obviously much more can be done.
for /f "delims=" %%A in ('p4 client -o') do (
echo %%A
)
The FOR command is a bit of a beast with a lot of non-intutive solutions for its many eccentricities. If you provide a clearer explanation as to what you need to do, then I might be able to provide better guidance.
This is considerably easier if you can use one of the Perforce APIs. That way you won't be doing so much text parsing in batch. Are you comfortable with perl, python, ruby, or .net?
I'm having trouble generating a timestamp in a Windows batch file, because I get diferent date formats on different Windows versions.
My machine:
>echo %date%
>Tue 11/17/2009
Friends machine:
>echo %date%
>11/17/2009
I guess there has to be some way of getting the date (11/17/2009) from both strings using for /f. I've been trying and googling and can't find the answer.
Is there another way to get a timestamp without using %date%?
Check out doff.exe. I use this a lot for getting timestamps for naming log files. From its web site:
DOFF prints a formatted date and time, with an optional date offset, (e.g -1 prints yesterday's date, +1 prints tomorrow's date). To view all the options available, execute "doff -h". I typically use this utility for renaming log files so that they include a timestamp, (see the third example below). This code should compile under Unix/Linux, as well as DOS.
Sample commands:
C:\>doff
19991108131135
With no parameters the output is the current date/time in the following format: yyyymmddhhmiss
C:\>doff mm/dd/yyyy
11/08/1999
In the above example a date format specification is given.
#echo off
for /f "tokens=1-3 delims=/ " %%a in ('doff mm/dd/yyyy -1') do (
set mm=%%a
set dd=%%b
set yyyy=%%c)
rename httpd-access.log httpd-access-%yyyy%%mm%%dd%.log
The sample batch file above shows a neat way to rename a log file based on yesterday's date. The "for" command executes doff to print yesterday's date, (the "-1" parameter specifies yesterday), then extracts each component of the date into DOS batch file variables. The "rename" command renames "httpd-access.log" to "httpd-access-[yesterday's date].log"
Also check out Microsoft's now.exe, available in the Windows Server 2003 Resource Kit Tools. One bad thing I found out (the hard way) about it is it sets the ERRORLEVEL to the number of characters printed.
Looks like this:
c:\>now
Thu May 19 14:26:45 2011
Help:
NOW : Display Message with Current Date and Time
Usage : NOW [message to be printed with time-stamp]
NOW displays the current time, followed by its command-line arguments.
NOW is similar to the standard ECHO command, but with a time-stamp.
Use VBScript if you want to get independent date time settings:
thedate = Now
yr = Year(thedate)
mth = Month(thedate)
dy = Day(thedate)
hr = Hour(thedate)
min = Minute(thedate)
sec = Second(thedate)
WScript.Echo yr&mth&dy&hr&min&sec
Unfortunately, it can't be done directly, so you need to resort to hacks like GetDate.cmd.
There are lots of VBScript and small external commandline tools available too, which isn't something I'd take a dependency on unless you're already using something of that nature in your overall system.
Personally, I'd be trying to route around it by using PowerShell which neatly sidesteps the issue completely.
You don't need VBScript. You can do it with something like this:
echo %date:~-10,2%/%date:~-7,2%/%date:~-4,4%
Source
As I have posted in here:
Batch: Timestamp to UNIX Time
What about simple 1-line long C program returning UNIX timestamp? You can retrieve value from %errorlevel% in batch script.
#include <stdio.h>
#include <time.h>
int main(void)
{
return (int) time(NULL);
}
In my test in command prompt it worked:
C:\Users\dabaran\Desktop\cs50\src\C>.\time || echo %errorlevel% && set mytstamp=%errorlevel%
1419609373
C:\Users\dabaran\Desktop\cs50\src\C>echo %mytstamp%
1419609373