14

My understanding is that in order to get the date from a file passed into a subroutine as an argument, you must re-set that argument as a variable within the subroutine. Is this correct? This doesn't make since to me, so I am wondering if I do not fully understand what is going on. I can use the passed in argument in practically any other subroutine code except for date extraction.

set setupEXE=setup.exe

CALL :SUB_CheckCorrectDate %setupEXE%
GOTO EOF
::----------------------------------

:SUB_CheckCorrectDate
set filename=%1%

:: SUCCESSFUL
for %%x in (%filename%) do set FileDate=%%~tx
@For /F "tokens=1-3 delims=-/ " %%A in ('@echo %FileDate%') do @( 
Set file_Month=%%A
Set file_Day=%%B
Set file_Year=%%C
)

:: GET ERROR    
for %%x in (%1%) do set FileDate=%%~tx
@For /F "tokens=1-3 delims=-/ " %%A in ('@echo %FileDate%') do @( 
Set file_Month=%%A
Set file_Day=%%B
Set file_Year=%%C
)    

GOTO:EOF

:: ------------------
:EOF
Fractal
  • 1,748
  • 5
  • 26
  • 46

2 Answers2

19

Use %1 to access the parameter, not %i%.

The argument variables have the same modifiers as FOR variables, so you can use %~t1.

No need to execute a command in your FOR /F. It is simpler to process a string literal using in ("string").

No need for :EOF label. Every script has an implicit :eof. I like to use exit /b instead.

@echo off
setlocal
set "setupEXE=setup.exe"

call :SUB_CheckCorrectDate "%setupEXE%"
exit /b

::----------------------------------

:SUB_CheckCorrectDate
set "filename=%~1"
for /F "tokens=1-3 delims=-/ " %%A in ("%~t1") do ( 
  set "file_Month=%%A"
  set "file_Day=%%B"
  set "file_Year=%%C"
)
exit /b
Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
dbenham
  • 127,446
  • 28
  • 251
  • 390
  • You added `setlocal`. Is there no need for `endlocal`, is it maybe implicitly done via the `exit` command? – Wolf Mar 28 '18 at 10:03
  • 1
    @Wolf - Yes, there is always an implicit ENDLOCAL at the end of a script or Called subroutine for any remaining SETLOCAL that were instantiated in that routine and have not yet been ended. This even happens if a routine or script ends because it has reached the end of file, even without an explicit EXIT /B. – dbenham Mar 28 '18 at 13:40
  • Ah thanks, good to know. On the page [setlocal | Microsoft Docs](https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/setlocal "setlocal | Microsoft Docs"), they missed to add the `exit` option. Is this a new feature? And, even more important, are the local environments properly stacked on each other? – Wolf Mar 29 '18 at 08:19
  • 1
    @Wolf - Yes, they stack properly, and the behavior has been consistent since the introduction of SETLOCAL. And Microsoft batch/cmd.exe documentation is generally crap - loads of missing information, and sometimes simply wrong. – dbenham Mar 29 '18 at 11:35
0

if you seriously want to write batch scripts, you should take care for the syntax:

@echo off &setlocal
set "setupEXE=setup.exe"

CALL :SUB_CheckCorrectDate "%setupEXE%"
GOTO EOF
::----------------------------------

:SUB_CheckCorrectDate
set "filename=%~1"

:: SUCCESSFUL
for %%x in ("%filename%") do set "FileDate=%%~tx"
For /F "tokens=1-3 delims=-/ " %%A in ("%FileDate%") do ( 
    Set "file_Month=%%A"
    Set "file_Day=%%B"
    Set "file_Year=%%C"
)

:: GET ERROR    
for %%x in ("%~1") do set "FileDate=%%~tx"
For /F "tokens=1-3 delims=-/ " %%A in ("%FileDate%") do ( 
    Set "file_Month=%%A"
    Set "file_Day=%%B"
    Set "file_Year=%%C"
)    

GOTO:EOF

:: ------------------
:
Endoro
  • 37,015
  • 8
  • 50
  • 63
  • ...and maybe also take care to the right way of returning from sub routines via `exit /b` – Wolf Mar 28 '18 at 09:53
  • 4
    @Endoro _**"if you seriously want to write batch scripts, you should ..."**_ use `Rem` instead of palliatives like `::` etc. Downvote due to this bad mentoring preambule. – user6698332 Jul 31 '18 at 06:16