Home > Software engineering >  Creating multiple files using batch
Creating multiple files using batch

Time:10-19

I'm trying to generate multiple files for a specific situation, but my loop statement does not work properly. The batch file have three entries, such as:

Unit code = e.g (095)
Initial date = e.g (20211001)
Final date =  e.g (20211010)

I'm expecting 10 files, like this. Because from the initial date until the final date we have 10 days, in this example, but could be 15, 20...

2v09520211001.rep
2v09520211002.rep
2v09520211003.rep
2v09520211004.rep

My code

Echo off        
cls
color 1f
title Bat automatiza 2V
:menu
Time /t
Date /t
echo *********************************
echo * Automatize files generate     *
echo *********************************
set /p op= Unit code
set /p op1= Initial date
set /p op2= Final date
for(int i = %op1%; i < %op2%; i  ) {
    do 
    echo > 2v%op%%op1%%op2%.rep
}

Thank you in advance.

CodePudding user response:

Here is a complete code with reading the data from a text file or prompting the user of the batch file for the data with validating the strings before using them to create the empty files with the appropriate file names.

@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem Undefine the three environment variables used to hold the data.
set "UnitCode="
set "InitialDate="
set "FinalDate="

rem Load the data from file Data.txt in batch file directory if existing.
if exist "%~dp0Data.txt" for /F "usebackq tokens=1,2* delims== " %%G in ("%~dp0Data.txt") do set "%%G%%H=%%~I"
if defined UnitCode set "UnitCode=%UnitCode:"=%"
if defined InitialDate set "InitialDate=%InitialDate:"=%"
if defined FinalDate set "FinalDate=%FinalDate:"=%"

rem Check each value and prompt user on value not defined or invalid.
call :CheckValue UnitCode "unit code" ".[0123456789][0123456789]*." 0 "%UnitCode%"
call :CheckValue InitialDate "initial date in format 20yyMMdd" ".20[0123456789][0123456789][01][0123456789][0123][0123456789]." 1 "%InitialDate%"
call :CheckValue FinalDate "final date in format 20yyMMdd" ".20[0123456789][0123456789][01][0123456789][0123][0123456789]." 1 "%FinalDate%"

rem Check if the initial date is before or equal the final date.
if %InitialDate% LEQ %FinalDate% goto CreateFiles
echo ERROR: The initial date %InitialDate% is after the final date %FinalDate%!
goto :EOF

:CheckValue
if "%~5" == "" goto GetValue
set "Value=%~5"
goto ValidateValue

:GetValue
set "Value="
set /P "Value=Please enter %~2: "
if not defined Value goto GetValue
set "Value=%Value:"=%"
if not defined Value goto GetValue

:ValidateValue
setlocal EnableDelayedExpansion
rem The value must match the regular expression or it is invalid.
echo "!Value!"| %SystemRoot%\System32\findstr.exe /R /X %3 >nul
if errorlevel 1 (
    echo ERROR: !Value! is not valid for %~2^^!
    endlocal
    goto GetValue
)
endlocal
set "%1=%Value%"
if %4 == 0 goto :EOF

rem Additional checks for validating a date value.
set "Year=%Value:~0,4%"
set /A Month=1%Value:~4,2% - 100
set /A Day=1%Value:~6,2% - 100
if %Month% == 0 goto InvalidDate
if %Month% GTR 12 goto InvalidDate
if            
  • Related