Multiple patterns for listing files using command line - windows

Inside my working directory I have files named after the year, month, day and hour of retrieval, like this:
19980101.00.nc 19980101.03.nc 19980101.06.nc 19980101.09.nc
19980101.12.nc 19980101.15.nc 19980101.18.nc 19980101.21.nc
19980102.00.nc 19980102.03.nc 19980102.06.nc 19980102.09.nc
19980102.12.nc 19980102.15.nc 19980102.18.nc 19980102.21.nc
19980103.00.nc 19980103.03.nc 19980103.06.nc 19980103.09.nc
19980103.12.nc 19980103.15.nc 19980103.18.nc 19980103.21.nc
And using the windows command line I would like to be able to operate with only this files:
19980101.15.nc |
19980101.18.nc |
19980101.21.nc _|_ # these are day 01 files
19980102.00.nc |
19980102.03.nc |
19980102.06.nc |
19980102.09.nc |
19980102.12.nc _|_ # these are day 02 files
So far I've used the command: dir 19980101.*.nc in order to list 19980101 files but I don't know how to omit the non desired files (I only need 19980101.15.nc, 19980101.18.nc and 19980101.21.nc files form day 01). Moreover, I haven't figured out how to combine in a single list file's names of day two (02 files) and files of day one (01 files).
Any thoughts?

The question is very unclear, but here is nevertheless a commented batch file which prompts the user for a file date like 19980102, determines the previous day for input date and runs DIR with the 8 files to print.
#echo off
setlocal EnableExtensions EnableDelayedExpansion
rem Prompt user for date string until a number string with exactly 8 digits is entered.
:EnterFileDate
set "FileDate="
set /P "FileDate=Enter date in format YYYYMMDD: "
rem Has the user nothing entered at all?
if not defined FileDate goto EnterFileDate
rem Has the user entered a string not consisting of only digits?
for /F "delims=0123456789" %%I in ("!FileDate!") do goto EnterFileDate
rem Has the user entered a too long string?
if not "%FileDate:~8%" == "" goto EnterFileDate
rem Has the user entered a too short string?
if "%FileDate:~7%" == "" goto EnterFileDate
rem Has the user entered a string not starting with 19 or 20 for century?
if not "%FileDate:~0,2%" == "19" if not "%FileDate:~0,2%" == "20" goto EnterFileDate
rem Don't care of possible other invalid date strings, but
rem check if the entered date string matches any existing file.
if not exist "%FileDate%.??.nc" (
echo/
echo There is no file "%FileDate%.??.nc".
echo/
goto EnterFileDate
)
rem Discard local environment and pass entered file date
rem to previous environment restored by command ENDLOCAL.
endlocal & set "FileDate=%FileDate%"
set "PrevYear=%FileDate:~0,4%"
set "PrevMonth=%FileDate:~4,2%"
rem Get day in month subtracted by 1 with avoiding to get 01 to 09
rem interpreted as octal number by concatenating this two digits
rem of day in month first with 1 for integer number 101 to 132.
set /A PrevDay=1%FileDate:~6,2% - 101
rem Is day in month greater or equal 1, month must not be changed.
if %PrevDay% GEQ 1 goto BuildPrevDate
set /A PrevMonth=1%PrevMonth% - 101
rem Is month less than 1, year must be changed too.
if %PrevMonth% LSS 1 (
set /A PrevYear-=1
set "PrevMonth=12"
set "PrevDay=31"
goto BuildPrevDate
)
rem Determine last day in new month and year.
if %PrevMonth% == 1 set "PrevDay=31" & goto BuildPrevDate
if %PrevMonth% == 3 set "PrevDay=31" & goto BuildPrevDate
if %PrevMonth% == 4 set "PrevDay=30" & goto BuildPrevDate
if %PrevMonth% == 5 set "PrevDay=31" & goto BuildPrevDate
if %PrevMonth% == 6 set "PrevDay=30" & goto BuildPrevDate
if %PrevMonth% == 7 set "PrevDay=31" & goto BuildPrevDate
if %PrevMonth% == 8 set "PrevDay=31" & goto BuildPrevDate
if %PrevMonth% == 9 set "PrevDay=30" & goto BuildPrevDate
if %PrevMonth% == 10 set "PrevDay=31" & goto BuildPrevDate
if %PrevMonth% == 11 set "PrevDay=30" & goto BuildPrevDate
rem Leap year finding for the years 1901 to 2099.
set /A LeapYear=PrevYear %% 4
if %LeapYear% == 0 (
set "PrevDay=29"
) else (
set "PrevDay=28"
)
set "LeapYear="
:BuildPrevDate
if %PrevDay% LSS 10 set "PrevDay=0%PrevDay%"
if %PrevMonth% LSS 10 set "PrevMonth=0%PrevMonth%"
set "PrevDate=%PrevYear%%PrevMonth%%PrevDay%"
set "PrevDay="
set "PrevMonth="
set "PrevYear="
echo/
dir %PrevDate%.15.nc %PrevDate%.18.nc %PrevDate%.21.nc %FileDate%.00.nc %FileDate%.03.nc %FileDate%.06.nc %FileDate%.09.nc %FileDate%.12.nc
set "FileDate="
set "PrevDate="
echo/
pause
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
dir /?
echo /?
endlocal /?
for /?
goto /?
if /?
pause /?
rem /?
set /?
setlocal /?

Related

Windows batch event reminder for next day

How do I write a batch script, that would search in Dates.txt file of this format:
EventName1 : dd.mm.yyyy
EventName2 : dd.mm.yyyy
...
EventNameN : dd.mm.yyyy
for events with tomorrow's date, and if found, notify the user about them?
I was able to write a script for today's events:
#echo off
setlocal disableDelayedExpansion
IF NOT EXIST Dates.txt GOTO not_found_dates
for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if '.%%i.'=='.LocalDateTime.' set ldt=%%j
set ldt=%ldt:~6,2%.%ldt:~4,2%.%ldt:~0,4%
echo Today: %ldt%
for /f "tokens=1,2 delims=:" %%A in (Dates.txt) do (
if "%%B"==" %ldt%" echo You have %%Atoday!
)
GOTO:EOF
:not_found_dates
echo Dates.txt not found!
GOTO:EOF
But I can't figure out how to find tomorrow's date to compare it with the dates in file.
Some help would be appreciated!
Well, I have finally figured it myself!
#echo off
setlocal DisableDelayedExpansion
if not exist Dates.txt goto not_found_dates
for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if '.%%i.'=='.LocalDateTime.' set ldt=%%j
set d=%ldt:~6,2%
set m=%ldt:~4,2%
set y=%ldt:~0,4%
set ldt=%d%.%m%.%y%
echo ************************
echo * Today: %ldt% *
:loop
set /a d=1%d%-99
if %d% gtr 31 (
set d=1
set /a m=1%m%-99
if %m% gtr 12 (
set m=1
set /a y+=1
)
)
xcopy /d:%m%-%d%-%y% /l . .. >nul 2>&1 || goto loop
set td=0%d%
set td=%td:~-2%
set tm=0%m%
set tm=%tm:~-2%
set ty=%y%
set tomorrow=%td%.%tm%.%ty%
echo * Tomorrow: %tomorrow% *
echo ************************
for /f "tokens=1,2 delims=:" %%A in (Dates.txt) do (
if "%%B"==" %tomorrow%" echo # You have %%Atomorrow!
)
goto :EOF
:not_found_dates
echo Dates.txt not found!
goto :EOF
It works for the Dates.txt file, that uses dates in this format:
EventName1 : 31.05.2016
EventName2 : 30.05.2016
EventName3 : 31.05.2016
EventName4 : 01.06.2016
EventName5 : 31.05.2016
EventName6 : 02.06.2016
EventName7 : 01.06.2016
(Shouldn't forget about single empty spaces before and after colon, and about leading zeros for days and months that are less than 10.)
UPDATE:
At first, set /a d+=1 adds a day.
Then, this line:
xcopy /d:%m%-%d%-%y% /l . .. >nul 2>&1 || goto loop
checks if the date that was formed by set /a d+=1 part, actually exists in the calendar. If the date that was formed doesn't exist, it just "skips" the date, moving to the beginning of the loop to add one more day. This way, the date that doesn't exist can't be set as tomorrow's date.
The if %d% gtr 31 ( part is not doing anything unless it is actually 31st day of month today.
So, despite the if %d% gtr 31 ( part that looks somewhat confusing, this code still works well for months that have less than 31 days in them.
To understand it all better, turn #echo on and trace the changes in the date values.
For example, if we use:
set d=30
set m=04
set y=2016
Output is:
************************
* Today: 30.04.2016 *
* Tomorrow: 01.05.2016 *
************************
Also, for:
set d=28
set m=02
set y=2015
Output:
************************
* Today: 28.02.2015 *
* Tomorrow: 01.03.2015 *
************************
Here is a pure batch file solution to calculate tomorrow's date from current date with remarks explaining the code. The lines with remark command rem can be removed for faster processing the batch file by Windows command processor.
#echo off
setlocal EnableExtensions DisableDelayedExpansion
if "%~1" == "" (
rem Get local date and time in a region independent format.
for /F "skip=1 tokens=1 delims=." %%D in ('%SystemRoot%\System32\wbem\wmic.exe OS get LocalDateTime') do set "LocalDateTime=%%D" & goto GetDate
) else (
rem This is for fast testing determining the date of tomorrow from any
rem date specified as parameter in format yyyyMMdd on calling this batch
rem file from within a command prompt window. The parameter string is
rem not validated at all as this is just for testing the code below.
set "LocalDateTime=%~1"
)
rem Get day, month and year from the local date/time string (or parameter).
:GetDate
set "Day=%LocalDateTime:~6,2%"
set "Month=%LocalDateTime:~4,2%"
set "Year=%LocalDateTime:~0,4%"
rem Define a variable with today's date in format dd.MM.yyyy
set "Today=%Day%.%Month%.%Year%"
rem Increase the day in month by 1 in any case.
rem It is necessary to remove leading 0 for the days 08 and 09 as
rem those two days would be otherwise interpreted as invalid octal
rem numbers and increment result would be 1 instead of 9 and 10.
rem if "%Day:~0,1%" == "0" set "Day=%Day:~1%"
rem set /A Day+=1
rem Faster is concatenating character 1 with the day string to string
rem representing 101 to 131 and subtract 99 to increment day by one.
set /A Day=1%Day%-99
rem The tomorrow's date is already valid if the day of month is less than 29.
if %Day% LSS 29 goto BuildTomorrow
rem Tomorrow is next month if day is equal (or greater) 32.
if %Day% GEQ 32 goto NextMonth
rem Day 31 in month is not possible in April, June, September and November.
rem In February it can't occur that day in month increased from 30 to 31
rem except on calling this batch file with invalid date string 20160230.
if %Day% EQU 31 (
if %Month% == 04 goto NextMonth
if %Month% == 06 goto NextMonth
if %Month% == 09 goto NextMonth
if %Month% == 11 goto NextMonth
)
rem The day 29 and 30 in month is valid for all months except February.
if NOT %Month% == 02 goto BuildTomorrow
rem Determine if this year is a leap year with 29 days in February.
set /A LeapYearRule1=Year %% 400
set /A LeapYearRule2=Year %% 100
set /A LeapYearRule3=Year %% 4
rem The current year is always a leap year if it can be divided by 400
rem with 0 left over (1600, 2000, 2400, ...). Otherwise if the current
rem year can be divided by 100 with 0 left over, the current year is NOT
rem a leap year (1900, 2100, 2200, 2300, 2500, ...). Otherwise the current
rem year is a leap year if the year can be divided by 4 with 0 left over.
rem Well, for the year range 1901 to 2099 just leap year rule 3 would be
rem enough and just last IF condition would be enough for this year range.
set "LastFebruaryDay=28"
if %LeapYearRule1% == 0 (
set "LastFebruaryDay=29"
) else if NOT %LeapYearRule2% == 0 (
if %LeapYearRule3% == 0 (
set "LastFebruaryDay=29"
)
)
if %Day% LEQ %LastFebruaryDay% goto BuildTomorrow
rem Tomorrow is next month. Therefore set day in month to 1, increase the
rem month by 1 and if now greater than 12, set month to 1 and increase year.
:NextMonth
set "Day=1"
set /A Month=1%Month%-99
if %Month% GTR 12 (
set "Month=1"
set /A Year+=1
)
rem The leading 0 on month and day in month could be removed and so both
rem values are defined again as string with a leading 0 added and next just
rem last two characters are kept to get day and month always with two digits.
:BuildTomorrow
set "Day=0%Day%"
set "Day=%Day:~-2%"
set "Month=0%Month%"
set "Month=%Month:~-2%"
rem Define a variable with tomorrow's date in format dd.MM.yyyy
set "Tomorrow=%Day%.%Month%.%Year%"
echo Today is: %Today%
echo Tomorrow is: %Tomorrow%
endlocal
Please read my answer on Why does %date% produce a different result in batch file executed as scheduled task? It explains in full details the FOR command line using WMIC to get current date in region independent format.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
echo /?
endlocal /?
for /?
goto /?
if /?
rem /?
set /?
setlocal /?
wmic OS get /?

Subtract month in Windows batch

I would like to have this run and show the previous month. When I try to subtract the month it makes the last day of the month field not appear.
#echo off
set FirstDay=01
set Month=%date:~4,2%
set Year=%date:~10,4%
if %Month%==01 set LastDay=31 & goto foundate
if %Month%==02 set LastDay=28 & goto foundate
if %Month%==03 set LastDay=31 & goto foundate
if %Month%==04 set LastDay=30 & goto foundate
if %Month%==05 set LastDay=31 & goto foundate
if %Month%==06 set LastDay=30 & goto foundate
if %Month%==07 set LastDay=31 & goto foundate
if %Month%==08 set LastDay=31 & goto foundate
if %Month%==09 set LastDay=30 & goto foundate
if %Month%==10 set LastDay=31 & goto foundate
if %Month%==11 set LastDay=30 & goto foundate
if %Month%==12 set LastDay=31 & goto foundate
:foundate
echo The year is: %Year%
echo The month is: %Month%
echo First day of this month is: %FirstDay%
echo Last day of this month is: %LastDay%
Dates are complicated to work with and easy to get wrong, and if you can avoid rolling your own, do so.
CMD does not come with a native date library, but the .NET System.DateTime library is available via PowerShell. The following PS script shows how to use .NET to do what you're asking.
Get-LastMonthStats.ps1
$lastmonth = (Get-Date).addMonths(-1)
"The year is $($lastmonth.year)"
"The month is $($lastmonth.Month)"
"First day of this month is 01"
"Last day of this month is $([DateTime]::DaysInMonth($lastmonth.year, $lastmonth.month))"
Nearly all of this script is formatting the output. To run from CMD, launch this with the command
powershell.exe -ex bypass -f Get-LastMonthStats.ps1
Alternatively, you can put the whole script on one (very long) command line, if you don't want to create a separate .ps1 file.
powershell -c "$lastmonth = (Get-Date).addMonths(-1); 'The year is ' + $lastmonth.year; 'The month is ' + $lastmonth.Month; 'First day of this month is 01'; 'Last day of this month is ' + [DateTime]::DaysInMonth($lastmonth.year, $lastmonth.month)"
I made a function library that has these functions. Doing what you want with this library is easy, check below.
The functions are at the end of the code. Just copy to the end of your file (after a goto:eof) and call them using specified arguments.
The given code inside :main should do what you are asking for.
Remarks:
Has inbuilt Leap Year check, so if the year is a Leap Year, Februrary will return 29 days.
When copying functions to your file, please keep Author information
Code:
#echo off
:main
:: Recommended to use GetDateIntl function, to get region-independent results (Check functions at the end of this file)
call :GetDateIntl Year Month Day /A
echo/Using GetDateIntl to get current date: %Year%-%Month%-%day%
echo/
:: Call function to get last day of this month (Check functions at the end of this file)
call :GetLastDayOfMonth %Month% %Year% LastDay
echo This month is: %Month%/%Year%
echo First day of any month is always 1
echo Last day of this month is: %LastDay%
echo/
:: Get previous month number
set /A "PMonth= %Month% - 1"
set "PYear=%Year%"
:: Correct PMonth and PYear if this month is December
if %PMonth%==0 ( set "PMonth=12" & set /A "PYear= %PYear% - 1" )
:: Call function to get last day of previous month (Check function after :main)
call :GetLastDayOfMonth %PMonth% %PYear% PLastDay
echo Previous month is: %PMonth%/%PYear%
echo Last day of previous month is: %PLastDay%
goto:eof
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:GetDateIntl yy mm dd [/A]
:: Returns the current date on any machine with regional-independent settings
:: Arguments:
:: yy = variable name for the year output
:: mm = variable name for the month output
:: dd = variable name for the day output
:: /A = OPTIONAL, removes leading 0 on days/months smaller than 10
::
:: By Cyberponk, v1.1 - 11/05/2016
:: v1.0 - 2015
::
SETLOCAL ENABLEEXTENSIONS
if "%date%A" LSS "A" (set toks=1-3) else (set toks=2-4)
for /f "tokens=2-4 delims=(-)" %%a in ('echo:^|date') do (
for /f "tokens=%toks% delims=.-/ " %%i in ('date/t') do (
set '%%a'=%%i
set '%%b'=%%j
set '%%c'=%%k
)
)
if "%'yy'%"=="" set 'yy'=%'aa'%
if %'yy'% LSS 100 set 'yy'=20%'yy'%
endlocal&set %1=%'yy'%&set %4 %2=%'mm'%&set %4 %3=%'dd'%&goto :EOF
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:IsLeapYear %year% IsLeap
:: Checks if a year is a Leap Year (year that has 366 days)
:: Arguments:
:: %year% = year number input. Can be a number or a variable containing a 4 digit number
:: IsLeap = result output containing 1 or 0 (1= is leap year, 0= not yeap lear)
::
:: By Cyberponk, v1.1 - 11/05/2016
::
SETLOCAL ENABLEEXTENSIONS
set /A "IsLeap=(!(%1 %% 4) & !!(%1 %% 100)) | !(%1 %% 400)"
endlocal & set %2=%IsLeap%& goto:EOF
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:GetLastDayOfMonth %month% %year% LastDay
:: Gets the last day of a given month from a given year
:: Arguments:
:: %month% = month number input. Can be a number or a variable containing a number
:: %year% = year for reference (used to check for leap years)
:: LastDay = result output containing last day of the specified month
::
:: By Cyberponk, v1.0 - 11/05/2016
::
SETLOCAL ENABLEEXTENSIONS
:: Mathematical formula to last day of month
set /A "LastDay= 28 + (%1 + (%1/8)) %% 2 + 2 %% %1 + 2 * (1/%1)
:: Add 1 day if year is a Leap Year and month is February
call :IsLeapYear %2 IsLeap
if %LastDay%==28 set /A LastDay=%LastDay% + %IsLeap%
endlocal & set %3=%LastDay%& goto:EOF
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
here's a piece of code that will get the previous month:
#echo off
for /f "" %%# in ('WMIC Path Win32_LocalTime Get month /format:value') do (
for /f %%Z in ("%%#") do set /a %%Z-1
)
if %month% equ 0 set month=12
echo %month%
Better use wmic to get the date parts because it's independent from the machine's date format unlike the %date% variable.
EDIT (as requested in the comments) :
#echo off
setlocal disableDelayedExpansion
for /f "" %%# in ('"WMIC Path Win32_LocalTime Get month,year /format:value"') do (
for /f %%Z in ("%%#") do set "%%Z"
)
set /a month=month-1
if %month% equ 0 set month=12
rem echo %month% -- %year%
set /A "leap=!(year&3) + (!!(year%%100)-!!(year&15))"
if %leap% equ 0 (set "m2=29") else (set "m2=28")
setlocal enableDelayedExpansion
set m1=31
set m3=31
set m4=30
set m5=31
set m6=30
set m7=31
set m8=31
set m9=30
set m10=31
set m11=30
set m12=31
set premonLastDay=!m%month%!
endlocal & set premonLastDay=%premonLastDay%
if %month% equ 12 (
set /a year=year-1
)
echo %premonLastDay%-%month%-%year%
endlocal
I've modified script of cyberponk for following error
Previous month is: -1/2020
after
if "%'yy'%"=="" set 'yy'=%'aa'%
I've added as fix
if %'mm'% LSS 10 set 'mm'=%'mm':~1,1%

Windows Batch File not Validating Year

So I have a windows batch script that validates to see if the date format I entered is in the correct format. Everything is working fine with it except when it gets to the year part of it. It doesn't seem to validate the year in the correct format of xxxx. It will accept any number. Where is it breaking, I can't tell? Fix suggestions? Thank you!
set i=0
for %%a in (31 28 31 30 31 30 31 31 30 31 30 31) do (
set /A i+=1
set dpm[!i!]=%%a
)
set /P "inDate=Please insert FNOL date (MM-DD-YYYY format): "
if "%inDate:~2,1%%inDate:~5,1%" neq "--" goto invalidDate
for /F "tokens=1-3 delims=-" %%a in ("%inDate%") do set "MM=%%a" & set "DD=%%b" & set "YYYY=%%c"
ver > NUL
set /A month=1%MM%-100, day=1%DD%-100, year=1%YYYY%-10000, leap=year%%4
if errorlevel 1 goto invalidDate
if not defined dpm[%month%] goto invalidDate
if %leap% equ 0 set dpm[2]=29
if %day% gtr !dpm[%month%]! goto invalidDate
if %day% lss 1 goto invalidDate
SET fnoldate=%YYYY%-%MM%-%DD%
ECHO.
SET /P confirmdate=You entered a FNOL date of "%fnoldate%". Is this correct? [y/n]
ECHO.
You're missing some components of your batch file.
At the top, you should have the following:
#Echo Off
SetLocal EnableDelayedExpansion
Your code has some GoTo references, but it can't go anywhere. You should add the following at the bottom:
GoTo :EOF
:invalidDate
echo Invalid date.
Hmm. Giving an example of a failure would have been useful.
Let's take an 'invalid' year like 14.
Your calculation for 01-01-14 would be year=114-10000
You really need to check that year has length 4 first.
if "%year:~3%=="" goto invaliddate
if not "%year:~4%=="" goto invaliddate
and you are not then checking after year-is-numeric for year in a valid range
Here is a method to validate the year:
#echo off
set year=1980
for /f "delims=1234567890" %%a in ("%year%") do echo you must enter numeric characters only & goto :EOF
if %year% GEQ 1800 if %year% LEQ 2014 (
echo year is between 1800 and 2014
) else (
echo enter a year between 1800 and 2014
)

Batch Script: Validate Date Input

I have a batch file that I use to create new project folders for clients that walks a user through the creation process and adds the appropriate files and folders to a central location. I need to add an input section so they can put a date (not always current date) in and it is included in the naming of the files.
The issue I have, and I have hunted high and low and can't find my answer, is that I need to dummy proof the date input. I want the user to input the date in the MM-DD-YYYY format including dashes. It needs to then format it into YYYY-MM-DD. It needs to be smart enough that it forces the user to use the required format of MM-DD-YYYY; has to be numbers and dashes, no slashes, the right amount of characters, and so forth.
I haven't been able to find anything close to even remotely get me where I need to be so I am asking the awesome geniuses out there for help in this regard as it is driving me up a wall. Below is my script code. I need this input to go right after the job type is selected. "Please insert date (MM-DD-YYYY format): "
#echo off
setlocal EnableDelayedExpansion
set version=7.95
set projectpath="P:"
set workbookpath="\\server2\Documents\Blanks (DO NOT EDIT)\dryingworkbook_v3r75.xls"
set questions="\\server2\Documents\Blanks (DO NOT EDIT)\Abatement and Mold Questions.txt"
set notes="\\server2\Documents\Blanks (DO NOT EDIT)\Job Notes.docx"
set info="\\server2\Documents\Blanks (DO NOT EDIT)\Job Information.docx"
set bizname=1
ECHO =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
ECHO = Welcome to SERVPRO Project Creation Wizard v%version% =
ECHO =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
ECHO.
:sof
ECHO.
ECHO Is this new project for a Residential or Commercial job?
:loopJobType
SET /P jobtype=Enter [r] for Residential or [c] for Commercial:
ECHO.
IF "%jobtype%" == "r" GOTO :loopResidential
IF "%jobtype%" == "R" GOTO :loopResidential
IF "%jobtype%" == "c" GOTO :loopCommercial
IF "%jobtype%" == "C" GOTO :loopCommercial
GOTO :loopJobType
:loopResidential
ECHO You have chosen to create a new Residential job project.
ECHO.
set type=1
GOTO :loopFirstName
:loopCommercial
ECHO You have chosen to create a new Commercial job project.
ECHO.
set type=2
SET /p bizname=Please enter the business name:
ECHO.
IF "%bizname%"=="" GOTO :loopCommercial
:loopFirstName
SET /P FirstName=Please enter the insured's first name:
IF "%FirstName%"=="" GOTO :loopFirstName
call :format FirstName
:loopLastName
ECHO.
SET /P LastName= Please enter the insured's last name:
IF "%LastName%"=="" GOTO :loopLastName
call :format LastName
SET FullName=%LastName%, %FirstName%
SET FullBizName=%bizname% (%FullName%)
goto :ConfirmProject
:format
set Name=!%1!
set Head=%Name:~0,1%
set Tail=%Name:~1%
for %%a in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do set Head=!Head:%%a=%%a!
for %%a in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do set Tail=!Tail:%%a=%%a!
set %1=%Head%%Tail%
GOTO :eof
:ConfirmProject
ECHO.
IF "%type%" == "1" SET /P yesno=Are you sure you want to add "%FullName%" to the Project directory? [y/n]
IF "%type%" == "2" SET /P yesno=Are you sure you want to add "%FullBizName%" to the Project directory? [y/n]
IF "%yesno%" == "y" GOTO :CreateProject
IF "%yesno%" == "Y" GOTO :CreateProject
IF "%yesno%" == "n" GOTO :sof
IF "%yesno%" == "N" GOTO :sof
GOTO :ConfirmProject
:CreateProject
IF "%type%" == "1" SET ProjectName=%FullName%
IF "%type%" == "2" SET ProjectName=%FullBizName%
:: Create a folder containing a new project.
mkdir "%projectpath%\%ProjectName%"
ECHO.
ECHO.
ECHO Creating a Project directory for "%ProjectName%" ...
:: Create a folder within said project that will contain job documents.
ECHO Creating a Documents directory for "%ProjectName%" ...
mkdir "%projectpath%\%ProjectName%\Documents"
:: (Taken out of use 7-15-13) ECHO Adding a Job Information file for "%ProjectName%" ...
:: (Taken out of use 7-15-13) copy /-Y %info% "%projectpath%\%ProjectName%\Documents\Job Information - %ProjectName%.docx"
ECHO Documents directory creation for "%ProjectName%" finished ...
:: Create a folder within said project that will contain drying workbook(s).
ECHO Creating a Drying Workbook directory for "%ProjectName%" ...
mkdir "%projectpath%\%ProjectName%\Drying Workbooks"
:: Copy a new blank workbook to the project workbook directory and give it the proper name.
ECHO Adding a Drying Workbook for "%ProjectName%" ...
copy /-Y %workbookpath% "%projectpath%\%ProjectName%\Drying Workbooks\DRY 1_%ProjectName%.xls"
ECHO Adding an Abatement and Mold Questions file for "%ProjectName%" ...
copy /-Y %questions% "%projectpath%\%ProjectName%\Drying Workbooks\Abatement and Mold Questions.txt"
ECHO Drying Workbook directory creation for "%ProjectName%" finished ...
:: Create a folder within said project that will contain original photos.
ECHO Creating a Photos directory for "%ProjectName%" ...
mkdir "%projectpath%\%ProjectName%\"Photos
:: Create a folder within said project photo folder that will contain resized photos.
mkdir "%projectpath%\%ProjectName%\Photos\Resized"
mkdir "%projectpath%\%ProjectName%\Photos\Upload"
ECHO Photos directory creation for "%ProjectName%" finished ...
:: Add in Job Notes file.
ECHO Adding a Job Notes files for "%ProjectName%" ...
copy /-Y %notes% "%projectpath%\%ProjectName%\Job Notes - %ProjectName%.docx"
:: Log the creation of the project.
FOR /F "TOKENS=1* DELIMS= " %%A IN ('DATE/T') DO SET CDATE=%%B
For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set date=%%a%%b%%c)
echo off > "%projectpath%\Logs\%ProjectName% - [Project Created %date% by %computername%].txt"
ECHO Logging "%ProjectName%" creation date and time...
ECHO Project directory creation for "%ProjectName%" finished ...
GOTO :OpenProject
:OpenProject
:: Ask if the project should be opened now. If so open and close script, else close script.
set /p reply=Do you want to open the "%ProjectName%" project now? [y/n]
if "%reply%" == "y" %SystemRoot%\explorer.exe "%projectpath%\%ProjectName%"
IF "%yesno%" == "Y" %SystemRoot%\explorer.exe "%projectpath%\%ProjectName%"
GOTO :eof
IF "%yesno%" == "n" GOTO :No
IF "%yesno%" == "N" GOTO :No
exit
:No
ECHO.
ECHO.
ECHO You have successfully created a new project for %ProjectName%.
ECHO.
ECHO Press any key to exit . . .
PAUSE>NUL
:eof
The Batch file below check that the inserted date have the right format and that it represent a valid date, that is, that have the right number of days in each month, even for February on leap years!
#echo off
setlocal EnableDelayedExpansion
set i=0
for %%a in (31 28 31 30 31 30 31 31 30 31 30 31) do (
set /A i+=1
set dpm[!i!]=%%a
)
set /P "inDate=Please insert date (MM-DD-YYYY format): "
if "%inDate:~2,1%%inDate:~5,1%" neq "--" goto invalidDate
for /F "tokens=1-3 delims=-" %%a in ("%inDate%") do set "MM=%%a" & set "DD=%%b" & set "YYYY=%%c"
ver > NUL
set /A month=1%MM%-100, day=1%DD%-100, year=1%YYYY%-10000, leap=year%%4 2>NUL
if errorlevel 1 goto invalidDate
if not defined dpm[%month%] goto invalidDate
if %leap% equ 0 set dpm[2]=29
if %day% gtr !dpm[%month%]! goto invalidDate
if %day% lss 1 goto invalidDate
echo Date correct: %YYYY%-%MM%-%DD%
goto :EOF
:invalidDate
echo Bad date
You can check whether your string is valid easily with the findstr command.
set /p date= Please insert date (MM-DD-YYYY format):
echo %date%| findstr /r "^[0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]$">nul
if errorlevel 1 (
echo invalid date
)
pause
(^ means beginning of line, while $ stands for end of line.)
Now for the reformatting MM-DD-YYYY into YYYY-MM-DD, you can split your string and than reassemble it. Since it's a fixed format, this isn't too hard either:
set yyyy=%date:~6,4%
set mm=%date:~0,2%
set dd=%date:~3,2%
set newDate=%yyyy%-%mm%-%dd%
echo %newDate%
The first number in each command resembles the position where the string will be cut.
The second number resembles the length of the substring.
I made a function :getdate who test the date try it;
It will test if the separators are correct, the value range for thr month and the day and
if the values are NUM.
#ECHO OFF
setlocal enabledelayedexpansion
:GetDate
set /p $D=Enter a date (MM-DD-YYYY) :
set $separate=%$d:~2,1% %$d:~5,1%
for %%a in (%$separate%) do (if "%%a" neq "-" (echo Wrong Separator : %%a
pause
goto:Getdate))
set $D=%$D:-= %
set $c=1
for %%a in (%$d%) do (call:test !$c! %%a
set /a $c+=1)
if !$c!==4 set $DateOK=%$month%-%$day%-%$Year%
echo This DATE IS OK %$dateOK%
exit /b
:test
if %1 equ 1 (echo %2 | findstr [0-9][0-9]
if errorlevel 1 (echo Unvalid value for Month [NOT NUM]: %2
pause
goto:getdate)
if %2 GTR 12 (echo Unvalid value for Month [VALUR RANGE +]: %2
pause
goto:getdate)
if %2 LSS 1 (echo Unvalid value for Month [VALUR RANGE -]: %2
pause
goto:getdate)
set $month=%2)
if %1==2 (echo %2 | findstr [0-9][0-9]
if errorlevel 1 (echo Unvalid value for Day [NOT NUM]: %2
pause
goto:getdate)
if %2 GTR 31 (echo Unvalid value for Day [VALUR RANGE +] : %2
pause
goto:getdate)
if %2 LSS 01 (echo Unvalid value for Day [VALUE RANGE -]: %2
pause
goto:getdate)
set $day=%2)
if %1==3 (echo %2 | findstr [0-9][0-9][0-9][0-9]
if errorlevel 1 (echo Unvalid value for Year [NOT NUM] : %2
pause
goto:getdate)
set $Year=%2)
#ECHO OFF
SETLOCAL enabledelayedexpansion
CALL :getverdate
ECHO DATE %indate% is OK.
GOTO :EOF
::
:: Get and verify date in format mm-dd-yyyy; reformat as yyyy-mmm-dd
::
:regetdate
ECHO "%indate%" is not in format "MM-DD-YYYY" or is invalid
:getverdate
SET /p indate="Please insert date (MM-DD-YYYY format): "
IF NOT "%indate:~2,1%%indate:~5,1%"=="--" GOTO regetdate
SET checkdate=9%indate:-=%
IF NOT "%checkdate:~8%"=="%checkdate:~8,1%" GOTO regetdate
FOR %%a IN (0 1 2 3 4 5 6 7 8 9) DO SET checkdate=!checkdate:%%a=!
IF DEFINED checkdate GOTO regetdate
IF %indate:~3,2%==00 GOTO regetdate
FOR %%i IN (01:31 02:29 03:31 04:30 05:31 06:30 07:31 08:31 09:30 10:31 11:30 12:31) DO (
FOR /f "tokens=1,2delims=:" %%j IN ("%%i") DO IF %%j==%indate:~0,2% if "%%k" geq "%indate:~3,2%" GOTO goodday
)
GOTO regetdate
:goodday
IF "%indate:~-4%" geq "1980" IF "%indate:~-4%" leq "2099" GOTO goodyear
GOTO regetdate
:goodyear
SET /a checkdate=%indate:~-4% %% 4
IF "%indate:~0,2%%indate:~3,2%"=="0229" IF %checkdate% neq 0 GOTO regetdate
SET indate=%indate:~-4%-%indate:~0,2%-%indate:~3,2%
GOTO :eof
Here's another 'get and validate date` routine.
Note that in your code you should never set a variable called date. %date% will return the current date - it's a "magic variable" controlled by CMD. Other such variables include %time%, %random% and %errorlevel%. Setting any of these overrides the system-established value.
You could present the user with three prompts - year, month, day.
set /p y="Please enter year (YYYY): "
set /p m="Please enter month (MM): "
set /p d="Please enter day (DD): "
set date=%y%-%m%-%d%
If you would like to verify the length of the input something like:
if [%y:~4%] NEQ [] echo year entered incorrectly & goto :getDate
You can assume if %y% is greater than four characters - i.e. if %y:~4% is not null - that it has been entered incorrectly (see Dos Tips on string manipulation). The same principal applies for day and month, except they should be two characters.
Obviously for that example you would need to add the label :getDate before the user input.
You may use ReadFormattedLine subroutine for all kind of formatted input. For example, the command below read 3 numbers in a date format; the routine just accept digits, insert the hyphens and continue automatically after read the last digit. If the user delete characters, the hyphens are also deleted automatically.
call :ReadFormattedLine myDate="##-##-####" /M "Please insert date (MM-DD-YYYY format): "
This subroutine is written in pure Batch so it does not require any additional program, and it allows several formatted input operations, like read passwords, convert letters to uppercase, etc. You may download ReadFormattedLine subroutine from Read a line with specific format.

Windows console %DATE% Math

I would like to set the date in a Windows batch file to 7 days ago from today. I would like to do this in the following format.
set today=%date:~10,4%-%date:~4,2%-%date:~-7,2%
Any ideas how to subract the 7 day time delta here ?
I posted the description below in some site some time ago:
The following Batch files convert from Date to Julian Day Number an viceversa:
DATETOJULIAN.BAT:
#ECHO OFF
REM CONVERT DATE TO JULIAN DAY NUMBER
REM ANTONIO PEREZ AYALA
REM GET MONTH, DAY, YEAR VALUES
FOR /F "TOKENS=1-3 DELIMS=/" %%A IN ("%1") DO SET MM=%%A& SET DD=%%B& SET YY=%%C
REM ELIMINATE LEFT ZEROS
SET /A DD=10%DD% %% 100, MM=10%MM% %% 100
REM CALCULATE JULIAN DAY NUMBER
IF %MM% LSS 3 SET /A MM+=12, YY-=1
SET /A A=YY/100, B=A/4, C=2-A+B, E=36525*(YY+4716)/100, F=306*(MM+1)/10, JDN=C+DD+E+F-1524
JULIANTODATE.BAT:
REM CONVERT JULIAN DAY NUMBER TO MONTH, DAY, YEAR
REM ANTONIO PEREZ AYALA
SET /A W=(%1*100-186721625)/3652425, X=W/4, A=%1+1+W-X, B=A+1524, C=(B*100-12210)/36525, D=36525*C/100, E=(B-D)*10000/306001, F=306001*E/10000, DD=B-D-F, MM=E-1, YY=C-4716
IF %MM% GTR 12 SET /A MM-=12, YY+=1
REM INSERT LEFT ZEROS, IF NEEDED
IF %DD% LSS 10 SET DD=0%DD%
IF %MM% LSS 10 SET MM=0%MM%
REM SHOW THE DATE
ECHO %MM%/%DD%/%YY%
This way, to add/subtract a number of days to a date use the following lines:
CALL DATETOJULIAN %DATE%
SET /A NEWDATE=JDN+DAYS
CALL JULIANTODATE %NEWDATE%
Regards...
Reference: http://quasar.as.utexas.edu/BillInfo/JulianDatesG.html
You just need to adjust your date format if it is not MM/DD/YYYY.
AdamEstrada asked about subtracting dates.
I had a tough time subtracting two Julian dates because of the SETLOCAL in the Julian functions. I did it by calling a function.
call:sub_Julians !Julian! %Today_Julian%
:sub_Julians
set /a delta_dates=%~1-%~2
...
goto:eof ::end:age_of_EPSdate
Ok, I needed a batch file to display the current JDAY for an DoD operations center. You can double-click the file and it will display in a CMD window. Then, press any key to exit.
Here's what I came up with:
#echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & 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%"
:: Call the day ordinal number subroutine
call :JDdayNumber %DD% %MM% %YYYY% DayOrdinalNumber
:: Display the result
echo.
echo Today is JDay %DayOrdinalNumber%
echo.
pause,
endlocal & goto :EOF
:: set "datestamp=%YYYY%%MM%%DD%" & set "timestamp=%HH%%Min%%Sec%"
:: set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"
:: echo datestamp: "%datestamp%"
:: echo timestamp: "%timestamp%"
:: echo fullstamp: "%fullstamp%"
:: ============================================================
:: Subroutine: Calculate a day's ordinal number within the year
::JDdayNumber day month year return_
setlocal enableextensions enabledelayedexpansion
if %2 LEQ 2 (
set /a f=%1-1+31*^(%2-1^)
) else (
set /a a=%3
set /a b=!a!/4-!a!/100+!a!/400
set /a c=^(!a!-1^)/4-^(!a!-1^)/100+^(!a!-1^)/400
set /a s=!b!-!c!
set /a f=%1+^(153*^(%2-3^)+2^)/5+58+!s!
)
set /a return_=%f%+1
endlocal & set "%4=%return_%" & goto :EOF
I refactored the code of the JDate and GDate subroutines a little bit from http://www.robvanderwoude.com/datetimentmath.php.
Usage example:
Enter ISO date:
Enter number of days to add: 7
2017-01-01 + 7 days = 2017-01-08
Enter ISO date:
Enter number of days to add:
2017-01-08 + 7 days = 2017-01-15
Enter ISO date:
Enter number of days to add:
2017-01-15 + 7 days = 2017-01-22
Enter ISO date:
Enter number of days to add:
2017-01-22 + 7 days = 2017-01-29
Enter ISO date:
Enter number of days to add:
2017-01-29 + 7 days = 2017-02-05
Enter ISO date: 2017-02-12
Enter number of days to add: -7
2017-02-12 + -7 days = 2017-02-05
Code:
"Date math.bat":
#echo off
call :main %*
goto :eof
:main
setlocal
call :initialize "2017-01-01" "1"
endlocal
goto :eof
:initialize
setlocal
set "previousDate=%~1"
set /a "numberOfDays=%~2"
set /p "previousDate=Enter ISO date: "
set /p "numberOfDays=Enter number of days to add: "
set "currentDate="
call :addIsoDateDays "%previousDate%" "%numberOfDays%" currentDate
echo %previousDate% + %numberOfDays% days = %currentDate%
echo.
call :initialize "%currentDate%" "%numberOfDays%"
endlocal
goto :eof
:stripLeadingZero
setlocal
set "number=%~1"
if %number:~0,1% equ 0 (
set "number=%number:~1%"
)
(
endlocal
set "%~2=%number%"
)
goto :eof
:addLeadingZero
setlocal
set "number=%~1"
if %number% lss 10 (
set "number=0%number%"
)
(
endlocal
set "%~2=%number%"
)
goto :eof
:gregorianToJulianDate
setlocal
set "gregorianYear=%~1"
set "gregorianMonth=%~2"
set "gregorianDay=%~3"
call :stripLeadingZero "%gregorianMonth%" gregorianMonth
call :stripLeadingZero "%gregorianDay%" gregorianDay
set /a "julianYear=(%gregorianYear% + 4800)"
set /a "julianMonth=((%gregorianMonth% - 14) / 12)"
set /a "julianDate=((1461 * (%julianYear% + %julianMonth%) / 4) + (367 * (%gregorianMonth% - 2 - (12 * %julianMonth%)) / 12) - ((3 * ((%julianYear% + %julianMonth% + 100) / 100)) / 4) + (%gregorianDay% - 32075))"
(
endlocal
set "%~4=%julianDate%"
)
goto :eof
:isoToJulianDate
setlocal
set "date=%~1"
set "year="
set "month="
set "day="
for /f "tokens=1-3 delims=-" %%a in ("%date%") do (
set "year=%%a"
set "month=%%b"
set "day=%%c"
)
set /a "julianDate=0"
call :gregorianToJulianDate "%year%" "%month%" "%day%" julianDate
(
endlocal
set "%~2=%julianDate%"
)
goto :eof
:julianToGregorianDate
setlocal
set /a "julianDate=%~1"
set /a "p=(%julianDate% + 68569)"
set /a "q=(4 * %p% / 146097)"
set /a "r=(%p% - ((146097 * %q%) + 3) / 4)"
set /a "s=(4000 * (%r% + 1) / 1461001)"
set /a "t=(%r% - ((1461 * %s%) / 4) + 31)"
set /a "u=(80 * %t% / 2447)"
set /a "v=(%u% / 11)"
set /a "gregorianYear=((100 * (%q% - 49)) + %s% + %v%)"
set /a "gregorianMonth=(%u% + 2 - (12 * %v%))"
set /a "gregorianDay=(%t% - (2447 * %u% / 80))"
call :addLeadingZero "%gregorianMonth%" gregorianMonth
call :addLeadingZero "%gregorianDay%" gregorianDay
(
endlocal
set "%~2=%gregorianYear%"
set "%~3=%gregorianMonth%"
set "%~4=%gregorianDay%"
)
goto :eof
:julianToIsoDate
setlocal
set /a "julianDate=%~1"
set "year="
set "month="
set "day="
call :julianToGregorianDate "%julianDate%" year month day
set "isoDate=%year%-%month%-%day%"
(
endlocal
set "%~2=%isoDate%"
)
goto :eof
:addIsoDateDays
setlocal
set "previousIsoDate=%~1"
set /a "numberOfDays=%~2"
set /a "previousJulianDate=0"
call :isoToJulianDate "%previousIsoDate%" previousJulianDate
set /a "currentJulianDate=(%previousJulianDate% + %numberOfDays%)"
set "currentIsoDate="
call :julianToIsoDate "%currentJulianDate%" currentIsoDate
(
endlocal
set "%~3=%currentIsoDate%"
)
goto :eof
A simpler option is to call a PowerShell command from within your batch script to manipulate the date. The batch script can set the date as 7 days in the past with a single line.
powershell -command "((Get-date).AddDays(-7)).ToString('yyyy-MM-dd')">captureVar && set /p Today=<captureVar
The line starts out by instructing the cmd line to use PowerShell for the commands contained within the double quotes.
powershell -command "the powershell command(s)"
Next it used the PowerShell cmdlet Get-Date , and uses AddDays to change the date from the current value. A negative number will subtract and a positive number will add. The default format looks like
Friday, December 20, 2019 6:18:29 PM
To change the format you must change the date into a string with format instructions
.ToString('dddd MM/dd/yyyy HH:mm:ss.ffff K')
The output of the PowerShell command is redirected into a file named captureVar. Another option would have been to have PowerShell write it to a file.
powershell -command "((Get-date).AddDays(-7)).ToString('yyyy-MM-dd') | set-content 'captureVar'" && set /p Today=<captureVar
I used && to make it a one liner. But you can set the var anytime after value has been written to the file.
set /p Today=<captureVar
You should write in whatever scripting language you are most comfortable in. But remember that your options when writing a batch script isn't just batch commands. Windows has made it very easy to invoke PowerShell commands at anytime within your batch script. You can often find easy solutions to a given obstacle with PowerShell, insert that command into your script, then carry on as usual to finish your batch script.
A couple of things to remember when invoking PowerShell into your batch script:
Batch sees everything in double quotes as the PowerShell command. If you find a solution written in PowerShell that uses double quotes in the command, you must substitute those with single quotes. To escape characters, follow batch scripting rules. Batch variables can be read in the PowerShell command in the same way as batch (%var%). But values created in PowerShell must be redirected to be used later in your batch script. Any $var created in the PowerShell command is lost once the closing quote closes the PowerShell session.
There's an answer with calling powershell.
Though there are still machines running without powershell installed (at the moment of writing this XP,Vista,Windows7,Windows2003 and Windows2008 still have a descent OS share and are coming without installed powershell)
Other option will be using jscript within a bat script
Here's the dayAdder.bat that accepts only one argument - the days you want to add to the current date and prints the result:
#if (#X) == (#Y) #end /* JScript comment
#echo off
cscript //E:JScript //nologo "%~f0" %*
exit /b %errorlevel%
#if (#X)==(#Y) #end JScript comment */
var days=parseInt(WScript.Arguments.Item(0));
Date.prototype.addDays = function(days) {
var date = new Date(this.valueOf());
date.setDate(date.getDate() + days);
return date;
}
var date = new Date();
WScript.Echo(date.addDays(5));
WScript.Echo("Year: " + date.getFullYear());
WScript.Echo("Month: " + date.getMonth());
WScript.Echo("DayOfTeWEek: " + date.getDay());
usage and the output:
E:\scripts>call dayAdder.bat 7
Sun Nov 8 16:27:48 UTC+0200 2020
Year: 2020
Month: 10
DayOfTeWEek: 2
DayOfTheMonth: 3
You can modify it in way that will be suitable for you.

Resources