I am writing a simple batch script that allows the user to input a beginning date and an end date and it transfers files with the date in the name. It takes in the year month day like so '20220215'. For example, the user types '20211110' and then '20211215' it grabs the files from November 11th, 2021 to December 15th, 2021. I am trying to grab the day and month from the input string but the problem is the values '08' and '09' give the script trouble because of hex values and such. So I do cases for the file day which works fine because it is at the end of the string. The issue is the month. Here is the code:
REM Works fine
IF !temp_var:~6! == 08 (
SET /A file_day = 8
) ELSE IF !temp_var:~6! == 09 (
SET /A file_day = 9
) ELSE (
SET /A file_day = !temp_var:~6!
)
REM Crashes script
IF !temp_var:~4, -2! == 08 (
SET /A file_month = 8
) ELSE IF !temp_var:~4, -2! == 09 (
SET /A file_month = 9
) ELSE (
SET /A file_month = !temp_var:~4, -2!
The error I recieve when I run the script is
-2 was unexpected at this time.
Which I found is simply at the first IF statement at the month block. But everytime I search up how to grab digits from a variable, it uses the %var:~#:#% format.
I have comtemplated switching to another language or way to do this but I figured the task I am trying to automate was simple and could be achieved with putting together a batch to run a few commands haha.
Thank you for your help!
CodePudding user response:
This program generate a series of succesive dates:
@echo off
setlocal
echo Date format: YYYYMMDD
set /P "startDate=Enter start date: "
set /P "endDate=Enter end date: "
echo/
echo %startDate%
set /A "YYYY=%startDate:~0,4%, m=1%startDate:~4,2%-100, d=1%startDate:~6%-100"
:nextDate
set /A "d =1, nextMon=!(d-(31 ((m m/8)&1)-!(m-2)*(2-!(YYYY%%4)))), d=d*!nextMon nextMon, m =nextMon, nextYear=!(m-13), m=m*!nextYear nextYear, YYYY =nextYear, MM=100 m, DD=100 d"
set "nextDate=%YYYY%%MM:~1%