I am passing command line arguments to a batch script and setting that to a variable like so:
SET dirWhereKept=%1
My problem is, I call a function inside the batch script with 3 arguments. When trying to get those 3 arguments, it gets the ones passed in via the command line instead:
FOR /f "delims=" %%i IN ('DIR /B') DO (
IF EXIST "%%~i\" (
rem nothing
) ELSE (
CALL :checkIfWantedFile %%i "%%~xi" %%~zi
)
)
:checkIfWantedFile
SET file=%~1
SET fileExtension=%~2
SET fileSizeInBytes=%~3
ECHO FILE: %file%
ECHO EXTENSION: %fileExtension%
ECHO SIZE: %fileSizeInBytes%
For example, if I pass in "Avatar ECE (2009)"
as the command line argument (which is a directory) and then when calling the function I pass:
%%i
:Avatar.mp4
"%%~xi"
:".mp4"
%%~zi
: some_int
When I do the ECHO
's in checkIfWantedFile
, the output will be:
FILE: Avatar ECE (2009)
EXTENSION:
SIZE:
As it's getting the command line arguments and not the function ones.
I've had a look at this and some others but cannot get it to work.
EDIT:
The intent of this batch script is to go into a given directory (supplied by the command line arguments) and extract and video file (.mp4
, .mkv
, .avi
) that maybe be in there. Sometimes, the video files are nested in a sub-directory which is why I am checking if the item in the FOR
loop is a folder or not. If it is, then the batch script was intended to go into the sub-directory and check whether there are any wanted video files in there or not and extract them.
I added the option that if the script comes across an unwanted file, it is deleted. This is not a requirement though.
The intent is also is that when the directory (supplied in the command line arguments) is cleared of all video files recursively, it is deleted.
Due to unwanted video sample files sometimes being present, I have put in a check as well to check the size of the file in MB, if it is GTR
then the %minimumSize%
then it is a wanted file and can be extracted
My full code is below:
@ECHO off
SET "dirWhereKept=%1"
SET mp4=".mp4"
SET mkv=".mkv"
SET avi=".avi"
SET needCompressingDir="E:\Need_compressing"
SET minimumSize=200
CD %needCompressingDir%
CD %dirWhereKept%
FOR /f "delims=" %%i IN ('DIR /B') DO (
IF EXIST "%%~i\" (
rem do nothing
) ELSE (
GOTO :EOF
CALL :checkIfWantedFile "%%i" "%%~xi" "%%~zi"
)
)
:checkIfWantedFile
SET file=%~1
SET fileExtension=%~2
SET fileSizeInBytes=%~3
IF "%fileExtension%" == %mp4% (
CALL :checkFileSize %fileSizeInBytes%
) ELSE (
IF "%fileExtension%" == %mkv% (
CALL :checkFileSize %fileSizeInBytes%
) ELSE (
IF "%fileExtension%" == %avi% (
CALL :checkFileSize %fileSizeInBytes%
) ELSE (
rem this is not required!
CALL :deleteFile
)
)
)
:checkFileSize
SET /a fileSizeInMB=%~1/1024/1024
IF %fileSizeInMB% GTR %minimumSize% (
CALL :moveFileToCompress
)
:deleteFile
ECHO "Delete called!"
:moveFileToCompress
MOVE %file% %needCompressingDir%