Windows CMD - use numbered variables - windows

If this has been asked here before, I'm not finding it.
I think I know a way around my actual problem, but it'd get a bit unwieldy; so this is more of a puzzle, I guess.
I want to set 12 variables:
set 01=01 - January
set 02=02 - February
set 03=03 - March
set 04=04 - April
set 05=05 - May
set 06=06 - June
set 07=07 - July
set 08=08 - August
set 09=09 - September
set 10=10 - October
set 11=11 - November
set 12=12 - December
If I use a CMD file to ask what's contained in %01%, I'm returned the name of the CMD file (which is always %0) plus the number 1. For %10% through %12%, I receive the content of %1, which in my case so far has been empty, plus the second digit of the variable name.
I'm moving files based on date to a storage server. The files are named "2015-01-01 Blah, Blah, Blah.txt", for instance. I can use code obtained here elsewhere to find out the year as token1 and the month as token2.
The storage destination has a folder for 2015, and inside that are subfolders, "01 - January", "02 - February", and so on.
I can always do a thorough "if token2=" each freaking month, for every single file, but I'm wondering whether there's a way to streamline the logic: Move %1 token1\token2, where 01 is expanded to "01 - January", without needing to IF my way through each month.
Code to start from:
#echo off
set 01=01 - January
set 02=02 - February
set 03=03 - March
set 04=04 - April
set 05=05 - May
set 06=06 - June
set 07=07 - July
set 08=08 - August
set 09=09 - September
set 10=10 - October
set 11=11 - November
set 12=12 - December
:: set /p cr_mo= "Pick a month, any month! "
:: echo CR_Mo=%CR_Mo%
:: echo Destination folder: %CR_Mo%
echo 01=%01%
echo 02=%02%
echo 03=%03%
echo 04=%04%
echo 05=%05%
echo 06=%06%
echo 07=%07%
echo 08=%08%
echo 09=%09%
echo 10=%10%
echo 11=%11%
echo 12=%12%

easiest way is to use delayed expansion:
#echo off
setlocal enabledelayedexpansion
set 01=01 - January
echo 01=!01!

Related

Check whether a Windows certificate is valid by date - using CMD

My target it to check whether a certificate (I have its exact name, in the example below it is "Dell") in any Windows computer is valid.
The way I think I can do it, is by taking the expiration date, and compare it with the current date.
So, first, I want to take the expiration date ("not after" field).
The way I did it:
certutil -verifystore Root Dell | findstr/n ^^ | findstr "^[6]:" | for /f "tokens=3-5 delims= " %f in ('more') do #echo %f %g %h
Output: 3/8/3020 8:47 PM
But note this is matching my regional date format. I need it to be generic- in a way it would work for all possible formats
Now, for any Windows computer I have the date.
Next step, is to get the current date with %date% variable, and calculate the seconds (?) difference between the two dates. With that result I could know whether the certificate is expired or not.
I can't find how to do so. Any advice?
Few notes:
No PowerShell
only 1 cmd command
The result of this 1 command should be the seconds difference of the 2 dates
Any other way to verify a certificate is acceptable. The way above is the only 1 I know

Check if file exist with current date

I have an backup tool that runs every day on 2 am,
The backup output saved under existed folder named "collector"
i'm trying to write script bash,
That will check if folder named "repository" exist under the "collector" directory with the current/today date. (i.e in compare to the RHEL date)
in case folder exist with older date, send email using echo.
Appreciate the help.
Find the number of seconds for 0:00h of today (start of the date) wrt a reference time (1970-01-01 00:00:00 UTC), say today.
Then find the number of seconds the the last modified time of the desired file wrt the same reference time, say mdate.
The two values can be compared to find whether the file has created today or not.
today="$(date -d "$(date "+%D")" +%s)" # seconds at the start of the sate
mdate="$(stat -c %Y src.sh)" # seconds to the last modification
if [[ $mdate -ge $today ]]; then
echo "modified today"
else
echo "modified before today"
fi

Create directories based on month name

I'm looking for a way to create the following structure. (starting from a set year, until the current year)
eg :
2008
- January
- Feburary
- ...
- December
2009
- January
- Feburary
- ...
- December
I've got some basic bash skills, but have not figured out the part on how to get the full month names and use those to create these structures.
Thank you.
A straightforward answer:
mkdir -p {2008..2013}/{January,February,March,April,May,June,July,August,September,October,November,December}
If the starting year is user-determined, e.g., in a variable:
starting_year=2008
current_year=$(date +%Y)
for ((y=starting_year;y<=current_year;++y)); do
mkdir -p "$y"/{January,February,March,April,May,June,July,August,September,October,November,December}
done

Print Batch Time in Milliseconds

I am aware that one can print batch time up to the centisecond with #echo %time%. Is there a similar command to get milliseconds as well?
There is a resource kit utility named Timethis that provides up to the millisecond time measurements :
TimeThis : Command Line : dir
TimeThis : Start Time : Wed Oct 24 12:49:56 2012
TimeThis : End Time : Wed Oct 24 12:49:56 2012
TimeThis : Elapsed Time : 00:00:00.093
Use wmic os get LocalDateTime. That's also the reliable way to get locale-independent date and time
for /F "usebackq tokens=2 delims==" %%i in (
`wmic os get LocalDateTime /VALUE`
) do #set ctime=%%i
echo milliseconds: %ctime:~15,3%
(Edit: doh, didn't notice that this has the windows tag. Leaving the answer here in case anyone else clicked on this for Linux)
If you are asking to time the execution length of a program or script, precede the command with time, for example:
time ls -R
and this will give you execution time in milliseconds:
real 0m0.667s
user 0m0.000s
sys 0m0.144s
If you are asking for the current date and time:
Take a look at:
man date
For example,
date +"%X %N"
This will give you the current time in Hour:Minutes:Seconds and then the Nanoseconds
Hope this answers your question
Gnu gdate can make this very simple; try the code below in a batch file, for instance. I use it to create unique filenames for batch processes invoked in command windows, and on my system under Windows 7 it's evident that, at least in a command window, two immediately successive uses of gdate never give identical results.
gdate +"%%Y%%m%%d%%H%%M%%S%%N"
gdate +"%%Y%%m%%d%%H%%M%%S%%N"
set i=
set T=
for /f %%i in ('gdate +%%Y%%m%%d%%H%%M%%S%%N') do set UniqueTime=%%i
echo %UniqueTime%
But I actually use something more like the line below, discarding the last five digits because for my purposes they never seem to matter: the preceding part is always unique. This lets the unique file names be shorter.
gdate +"%%Y%%m%%d%%H%%M%%S%%N" | sed "s/.....$//"
Pretty obviously, the same expression works under Linux, so one can also use it there.
ptime utility is "5ms or better" accurate
ptime echo hallo
ptime 1.0 for Win32, Freeware - http://www.pc-tools.net/
Copyright(C) 2002, Jem Berkes <jberkes#pc-tools.net>
=== echo hallo ===
hallo
Execution time: 0.022 s

How to filter txt file by string with using batch

I know this kind of problem will be very easy for you but I am total beginner in batch mode.
The question is how bat file code should look like to generate a new txt file from specific one .
for example .
I have report txt
Displaying status for license file: 7788#Server01
License Server: server01
License In Use Free
------- ------ ----
Design* 1 6
(user1#host1) 127 server01 7788 4402
Assembly* 0 4
Pro 0 15
AdvSE 2 3
(user2#host2) AdvSE server01 7788 2706
(user3#host3) AdvSE server01 7788 1503
SingleSite_License 1 3
(user4#host4) SingleSite_License server01 7788 2003
Intra_CLIENT_License 1 4
(user2#host2) Intra_CLIENT_License server01 7788 2003
CAD 1 32
^(user2#host2) CAD server01 7788 501
* = License Extensions - Available only on startup.
^ = Borrowed License.
Press any key to continue . . .
What I want to have in new file from this one are only lines :
SingleSite_License 3
Intra_CLIENT_License 9
but both SingleSite_License and Intra_Client_License should be taken from 1st found string -other are not necessary.
report txt can be different and licenses can be showed in different order .
if it not possible - other solution can be just only Free value will be written in new txt file - f.e 3 and 9 . so the last string in line which contains specific words
thank you for any tips
Not sure where you got the figure of 9 for the Intra_CLIENT_License (souldn't it be 4?), but this script will print out the free licences for those two product codes:
#echo off
setlocal enableextensions enabledelayedexpansion
for /f "tokens=1,3" %%a in ('type infile.txt') do (
if "x%%a"=="xSingleSite_License" (
echo %%a %%b
)
if "x%%a"=="xIntra_CLIENT_License" (
echo %%a %%b
)
)
endlocal
I basically cut and pasted your transcript into the infile.txt file and ran this script to get:
SingleSite_License 3
Intra_CLIENT_License 4
(you can replace type infile.txt with whatever command you need to generate that output).
Breakdown:
The setlocal just sets up cmd.exe to allow extensions and delayed expansion of environment variables. All my scripts start with this since it's so useful.
The for loop basically processes one line at a time, grabbing tokens 1 and 3 (a and b) from each.
Then it's a simple matter of checking a for the required licence values and, if it matches, outputting that along with b.
Write a C application. Batch is unnecessarily kludgy and probably can't even do this kind of stream manipulation.
Download UnxUtils and use the Unix stream processors. Might be possible to do with Powershell too.

Resources