Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

batch script subroutine: Passing arguments

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
like image 579
Fractal Avatar asked Sep 25 '13 13:09

Fractal


People also ask

Can you pass arguments to a batch file?

In the batch script, you can get the value of any argument using a % followed by its numerical position on the command line. The first item passed is always %1 the second item is always %2 and so on. If you require all arguments, then you can simply use %* in a batch script.

What is %% A in batch script?

Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. ( <set> ) Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.

What is %~ n0 in batch file?

%0 is the name of the batch file. %~n0 Expands %0 to a file Name without file extension.

How do I pass values from one batch file to another?

Answers. write your first script to call the second script with the generated time of the first script appended as a variable to the second script. Your second script catches the %var% as '%1' variable from first script. First script might look like this.


1 Answers

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
like image 70
dbenham Avatar answered Oct 18 '22 17:10

dbenham