Environment variable value not set, but why? - batch-file

Why is batch variable TESTTYPE not passed through to the calling batch file (Windows 7 machine)?
I trigger a test system via SVN commit message. Given e.g. this SVN message:
This should trigger my test system for a long test.
testing RunTest#longtest
Problem: The command set at the end of the inner batch file properly outputs longtest but the outer batch file outputs smoketest unexpectedly.
This is the calling batch file test.bat:
call %~dp0gettype.bat trunk 12345
echo %TESTTYPE%
This is the called batch file gettype.bat doing the work:
set TESTTYPE=smoketest
::find all lines in the string which contain the keyword "RunTest#"
setlocal EnableDelayedExpansion
set "cmd=svn log -r %2 -v | findstr /r "RunTest#""
::for all words in that found line call the subroutine "handleElement"
for /F "tokens=*" %%a in ('!cmd!') do (
if "%%a" NEQ "" for %%b in (%%a) do call:handleElement %%b
)
goto ende
::split on hashtag char and handle the keyword after the hashtag (and find the type)
:handleElement
set TMPSTR=%1
set TAGNAME=%TMPSTR:~0,8%
if "%TAGNAME%"=="RunTest#" call:getSpecialType %TMPSTR:~8%
goto:eof
:getSpecialType
set TESTTYPE=%1
goto:eof
:ende
set

change
call %~dp0gettype.bat
echo %TESTTYPE%
to
for /F "usebackq" %%i in (`call %~dp0gettype.bat`) do set "TESTTYPE=%%i"
echo %TESTTYPE%
and in gettype.bat
change
:ende
set
to
:ende
echo %TESTTYPE%

Related

Unset variable value assigned within if statement inside a nested for loop

I am trying to make one batch file to read parameters from the file and pass it to ant for deployment.
I have made the below batch file:
setlocal enabledelayedexpansion
echo on
IF EXIST "D:\testfile1.txt" (
echo Do one thing
set string=
set /p string=< D:\testfile1.txt
echo value taken from file is %string
for /f "tokens=2,4,6 delims==:" %%G IN ("%string%") DO (
echo %%G %%H %%I
set env=%%G
set dom=%%H
set com=%%I
echo ENV !env!
echo DOM !dom!
echo COM !com!
cd D:\kpn_eai\EAI_FIXED\branches\kpn_eai_fixed\fixed\build\scripts
%ANT_HOME%\bin\xanteai deploy %%G %%H %%I
)
) ELSE (
echo Do another thing
)
endlocal
In testfile1.txt I have parameters in below format:
Environment=Env_Name1:Domain=Domain_Name1:Component=Component_name1
Parameters are different deployments. When I am running the above code it is giving below output
D:\>echo off
Do one thing Ant home is C:\tibco\ant\apache-ant-1.9.13
value taken from file is Environment=Env_name1:Domain=Domain_Name1:Component=Component_name1
Env_name1 Domain_Name1 Component_name1
ENV Env_name1
DOM Domain_Name1
COM Component_name1
Deployments starts after this.
The issue I am facing is when I run this code for different parameters (in testfile1.txt) the values of ENV, DOM and COM remains same regardless of any parameters read from testfile1.txt.
Can anyone help me to correct this code and let me know how can I just assign the values read from file to a variable and pass it to ant for deployment?
NOTE:- This batch file will be placed in scheduler which will check for testfile1.txt after every 5 minutes, when it finds that file deployment process gets triggered. Thus I have included if condition to check availability of file.
There really shouldn't be any need to be setting or echoing everything as you go, so why not just do something like this instead:
#Echo Off
If Exist "testfile1.txt" (Set /P "string="<"testfile1.txt"
SetLocal EnableDelayedExpansion
Echo value taken from file is !string!
For /F "Tokens=2,4,6 Delims==:" %%A In ("!string!") Do (
CD /D "D:\kpn_eai\EAI_FIXED\branches\kpn_eai_fixed\fixed\build\scripts"
"%ANT_HOME%\bin\xanteai" deploy %%A %%B %%C)
EndLocal) Else Echo Do another thing
You could first split the read line at the colons with a simple for and
obtain the variable names env,dom,com from the content.
Here preceeded with an underscore for easier output.
:: Q:\Test\2019\03\08\SO_55061545_.cmd
#echo off
setlocal enabledelayedexpansion
set "FileIn=D:\testfile1.txt"
IF not EXIST "%FileIn%" (
echo Do another thing
Goto :somewhere
)
echo Do one thing
set "string="
set /p "string="<"%FileIn%"
echo value taken from file is %string%
for %%A in ("%string::=" "%") do for /f "tokens=1* delims==" %%B IN ("%%~A") DO (
echo %%A %%B %%C
set var=%%B
set "_!var:~0,3!=%%C"
)
set _
:: cd D:\kpn_eai\EAI_FIXED\branches\kpn_eai_fixed\fixed\build\scripts
:: %ANT_HOME%\bin\xanteai deploy %_env% %_dom% %_com%
endlocal
:somewhere
> SO_55061545_.cmd
Do one thing
value taken from file is Environment=Env_Name1:Domain=Domain_Name1:Component=Component_name1
"Environment=Env_Name1" Environment Env_Name1
"Domain=Domain_Name1" Domain Domain_Name1
"Component=Component_name1" Component Component_name1
_Com=Component_name1
_Dom=Domain_Name1
_Env=Env_Name1

Batch .txt reader

So, basically I want a Batch file to read a .txt. The problem is that the Batch file needs to update everytime a new line gets written to the .txt
#echo off
set "pc=%1"
FOR /F "delims=:" %%A IN ('findstr /N .* "%pc%"') DO set "zeilen=%%A"
type %pc%
set /A zeilen1=%zeilen%
:loop
if not %zeilen% == %zeilen1% (
set "line="
set zeilen2=%zeilen% - 1
for /f %%a in ('more/e +%zeilen2% ^< %pc%') do (
if not defined line set "line=%%a"
)
echo %line%
set /A zeilen+=1
)
FOR /F "delims=:" %%A IN ('findstr /N .* "%pc%"') DO set "zeilen1=%%A
goto loop
I also can't use the type command (line 9-13) because I don't want to refresh the whole .txt only the last line.
sry for my poor english
Thanks
To start the Batch you need to do something like this call batch.cmd txtname.txt
A basic tail command can be written like so. Credit to #dbenham for his initial solution on DosTips.com
#echo off
call :Loop <"tailme.txt"
exit
:Loop
set "line="
set /p "line="
if defined line (
echo %line%
) else (
pathping -q 1 -p 300 localhost >nul
)
goto :loop
If you don't wish to use third party options and wish to keep it pure batch, it is very possible. From your question, it sounds like you wish to read the last line of a text file and have it update that text each time the text file is edited. Further more, this batch file much be call'ed to when it needs to be used.
To do this, we can compare the date it was last modified using forfiles in an for loop. The reason for this is that if we use the file properties EX: ECHO Last-Modified Date : %%~ta we will not get the properties down to seconds. Thus the file will only compare down to the minutes.
Now that we can grab the last modified properties we can use an IF statement to look for when the file get a new time stamp. From there we can use a modified script that reads only the last line of a text file (Configurable by set /a LINES=LINES+1 LINES+1 - Infin) made by #Patrick Cuff
To call this batch file you will want to use call ReadFile.bat txtname.txt
Call - Command
ReadFile.bat - Name of batch script
txtname.txt - Name of textfile to read
Bellow is the full script.
ReadFile.bat
#ECHO OFF
#GOTO READ
:LOOP
Rem | Look for changes
FOR /f %%a in ('forfiles /M %1 /C "cmd /c echo #fdate-#ftime"') DO (set FileTimeCurrent=%%a)
IF "%FileTimeLoad%"=="%FileTimeCurrent%" (goto LOOP) else (goto READ)
:READ
cls
Rem | Get current date
FOR /f %%a in ('forfiles /M %1 /C "cmd /c echo #fdate-#ftime"') DO (set FileTimeLoad=%%a)
Rem | Get the number of lines in the file
set LINES=0
for /f "delims==" %%I in (%1) do (
set /a LINES=LINES+1
)
Rem | Print the last line
set /a LINES=LINES-1
more +%LINES% < %1
goto LOOP
For help on any of the commands do the following:
call /?
set /?
for /?
if /?
So on.

Im passing a multi line text as argument which will be saved in a variable and then the file created has only 1 Lline

Set a=%~1
echo %a%>>"C:\file.txt"
Im calling the script via cmd
"C:\batch.bat" "$Multi Line String$"
The output file is only having the text till the first line break.
Please Help
Obviously, there is a way to to pass a value with multi-line text in a Batch file parameter.
But it is a little bit complex and currently there isn't a way to get the content out of the parameter in a safe way for any possible content.
A simple demonstration
Producer.bat - puts "Line1<line feed>Line2" into %1
#echo off
setlocal EnableDelayedExpansion
(set LF=^
%=empty=%
)
set ^"pVar=^^^^^"Line1^^^^!LF!!LF!line2""
call Receiver.bat %%pVar%% #
Receiver.bat
#echo off
prompt :
#echo on
(
#exit /b
REM # %~1 #
)
Output
:(
REM # Line1
line2 #
)
To fetch this, the solution from How to receive even the strangest command line parameters? can be extended.
The problem is, that it only works with simple content, but there exists content which can't be received without producing parser errors.
Like: "Line1<line feed>Line2"
Therefore I would recommend Aacini's solution
Edit: Improved samples
A batch file to call another batch file with multi line variables.
#echo off
setlocal EnableDelayedExpansion
(set \n=^
%=empty=%
)
set "var=Line1!\n!line2!\n!line3!\n!line4"
call :callBatch var
exit /b
:callBatch
setlocal EnableDelayedExpansion
set "prepVar=!%~1!"
for %%L in ("!\n!") DO (
for %%b in ("^=^^" "&=^&" "<=^<" ">=^>" "|=^|") do set "prepVar=!prepVar:%%~b!"
set "prepVar=^^"!prepVar:%%~L=^^%%~L%%~L!""
)
rem echo !prepVar!-
call receiver.bat %%prepVar%%
exit /b
And a receiver.bat that accepts and parses a multi line value from %1.
#echo off
setlocal EnableExtensions DisableDelayedExpansion
prompt :
(
#echo on
for %%a in (4) do (
#goto :next
rem # %~1#
)
) > param.tmp
:next
#echo off
endlocal
setlocal DisableDelayedExpansion
set lineNo=0
(
for /F "skip=3 delims=" %%L in (param.tmp) do (
set /a lineNo+=1
set "line=%%L"
setlocal EnableDelayedExpansion
if !lineNo! EQU 1 (
set "line=!line:*# =!"
set "line=!line:~,-2!"
) ELSE IF "!line!"==") " (
goto :exit
) ELSE (
set "line=!line:* =!"
set "line=!line:~,-1!"
)
(echo(!line!)
endlocal
)
) > file.txt
:exit
type file.txt
Edit2: Unsolved problems
It's possible to fetch any content in line1, but all other lines have restrictions for their content.
The content has to be "parse conform", that means that a multi line parameter with this content fails
Line1
If a=
It's because the first line can be secured with REM, but all other lines have to be valid lines.
Carets are only visible in line1 (with the REM technic).
And lines beginning with # are empty and : are removed completly.
As I said in a comment, there is no way to pass a value with multi-line text in a Batch file parameter. You should pass the name of the multi-line text variable instead and expand it using DelayedExpansion. This is an example of such a method:
#echo off
setlocal EnableDelayedExpansion
rem Create a variable with just LineFeed
set LF=^
%Don't remove%
%these two lines%
rem Create the variable with multi-line text
set "MultiLineString=Line1: Test.!LF!Line2: Testing.!LF!Line3: Tested."
rem Call the Batch script passing *THE NAME* of the variable
cmd /C batch.bat MultiLineString
And this is your same batch.bat script with the changes I suggested:
#echo off
setlocal EnableDelayedExpansion
Set a=%~1
echo !%a%!> file.txt
echo File created:
type file.txt
Output:
File created:
Line1: Test.
Line2: Testing.
Line3: Tested.
This method works as long as the calling program have stored the multi-line string in an environment variable, that is the only type of variables that can be accessed by a Batch file. For this reason, it is important for us to know how you created the multi-line string; otherwise, we have no means to solve this problem...
EDIT: New method added
After I read the improvements to jeb's answer I must admit that I was wrong: it is possible to pass the value of a multi-line variable in a Batch file parameter. This is my own version of such a method:
#echo off
setlocal EnableDelayedExpansion
rem Create a variable with just LineFeed
(set LF=^
%empty line%
)
rem Create the variable with multi-line text
set "MultiLineString=Line1: <Test>.!LF!Line2: |Testing|.!LF!Line3: &Tested&."
rem Prepare the multi-line variable and call the receiver Batch file
call :callBatch
echo Back to caller...
goto :EOF
:callBatch
rem Prepare the multi-line variable
set "lines="
for /F "delims=" %%a in ("!MultiLineString!") do (
if not defined lines (
set "lines=%%a "
) else (
set "line=%%a"
for %%b in ("&=^&" "<=^<" ">=^>" "|=^|") do set "line=!line:%%~b!"
set "lines=!lines!!LF!!line!"
)
)
REM echo [!lines!]
rem Call the receiver Batch file
(batch.bat "!lines!")
exit /B
And this is the new version of the companion batch.bat file:
#echo off
setlocal EnableDelayedExpansion
(
prompt $S
echo on
for %%a in (_) do if a==b (
%1
)
) > param.tmp
#echo off
(for /F "skip=2 delims=" %%a in (param.tmp) do if "%%a" neq ")" (
set "line=%%a"
echo(!line:~1,-2!
)) > file.txt
del param.tmp
echo File created:
echo/
type file.txt
This version correctly manage most special Batch characters, excepting exclamation-mark. This is the output:
File created:
Line1: <Test>.
Line2: |Testing|.
Line3: &Tested&.
Back to caller...
PS - This method will not work if the receiver batch.bat file is executed from outside another batch file, that is, via the standard cmd /C batch.bat "$Multi Line String$" method.

Issue with environment variable in batch script

So, I have probably a simple question but I cannot seem to find an easy answer.
Issue: I have a file that contains a set of lines such as:
%windir%\file.exe
%windir%\file2.dll
and so forth...
What I am trying to do is echo the actual file path to a second file such that the resulting output would be something like:
C:\Windows\file.exe
C:\Windows\file2.dll
and so forth...
The actual source file could have other variables such as %programfiles% but all of them have a resulting actual path.
I am currently using a for /f loop but when I echo the variable, I just get the environment variable returned rather than the actual path to the file.
Is there a solution out there for batch scripting?
The actual script is below. Note I am all for making this more efficient as time to get the information is important.
#echo off
setlocal enabledelayedexpansion
reg.exe query "HKLM\System\CurrentControlSet\Services" >> registry_hklm_installed_services_tmp.txt 2>nul
reg.exe query "HKLM\System\ControlSet001\Services" >> registry_hklm_installed_services_tmp.txt 2>nul
reg.exe query "HKLM\System\ControlSet002\Services" >> %temp_outpath%\registry_hklm_installed_services_tmp.txt 2>nul
reg.exe query "HKLM\System\ControlSet002\Services" >> registry_hklm_installed_services_tmp.txt 2>nul
for /f "delims=?" %%a in (registry_hklm_installed_services_tmp.txt) do (
set regkey=%%a
call :getvalue
)
goto :parsereg
:getvalue
reg.exe query "!regkey!\Parameters" /v ServiceDll > nul 2>&1 && goto regkeyexist
goto :eof
:regkeyexist
for /f "tokens=2*" %%b in ('reg.exe query "!regkey!\Parameters" /v ServiceDll') do set ImagePath=%%c
call :regag
goto :eof
:regag
echo !ImagePath! >> registry_hklm_installed_services_tmp2.txt
goto :eof
:parsereg
for /f "delims=?" %%a in (registry_hklm_installed_services_tmp2.txt) do echo %%a >> registry_hklm_installed_services_tmp3.txt
You can use the for /f command to cycle through the lines in the file like you are doing, and pass the line from the file to a subroutine inside the batch file, which will resolve it while it is being passed. Give the following:
Test.txt
%windir%\test.txt
%programfiles%\Test2.txt
This batch file will resolve the environment variables:
#echo off
setlocal ENABLEDELAYEDEXPANSION
for /f "delims=" %%i in (Test.txt) do call :Expand "%%i"
endlocal
goto TheEnd
:Expand
set _var=%1
echo !_var:"=!
:TheEnd
This is how the output looks when you run it:
c:\>Test.bat
C:\Windows\test.txt
C:\Program Files (x86)\Test2.txt
You can redirect the result to a new text file like this:
Test.bat > NewFile.txt
Or you can modify the original Test.bat to output the modified filename under Expand instead of echoing it to the console. It is important to include the quotes around %%i ("%%i") or spaces in the resolved paths will break into multiple variables when calling Expand (e.g., %1, %2, %3, etc.). The !_var:"=! removes the quotes.
This will also expand the variables.
#echo off
for /f "delims=" %%a in (Test.txt) do call echo %%a
pause
Test.txt
%windir%\test.txt
%programfiles%\Test2.txt
#ECHO OFF
SETLOCAL
FOR /f "delims=" %%a IN (q22726616.txt) DO (
FOR /f "delims=" %%b IN ('echo %%a') DO (
ECHO %%b
)
)
GOTO :EOF
I used a file named q22726616.txt containing your data for my testing.
[fixed following response - %%b line]

Batch File Loop Skip File if name contains

I am creating this batch file, that works with handbrakecli, to batch convert avi to mp4.
However I am stuck in how to continue the loop and skip the current file inside a loop.
FOR /R "%somepath%" %%G in (*.avi) DO (
rem skip if filename contains word trailer
rem skip if file name contains word sample
rem do conversion
)
This currently doesn't work in skipping the files that contain trailer or sample
I have tried using find or findstr and both fail to skip.
echo "%%G" | c:\windows\system32\findstr /i "trailer" > NUL
If %ERRORLEVEL% EQU 1 set skip Yes
Here is for sample.
echo "%%G" | c:\windows\system32\findstr /i "sample" > NUL
If %ERRORLEVEL% EQU 1 set skip Yes
If a file contains either trailer or sample, I do not want to do any handbrakecli conversions, but to just skip it.
I do echo's to display which files get converted, and it does include files with Sample or sample in the name.
I have tried using find or findstr and both fail to set skip to yes
if skip == No do ( rem do conversion )
I only want to convert non-trailer/sample avi files.
Thank you for your time.
try this, put your conversion commands in the loop and remove the word echo before handbrakecli if the output is OK:
#echo off &setlocal
FOR /R "%somepath%" %%G in (*.avi) DO (
set "fpath=%%G"
set "fname=%%~nG"
setlocal enabledelayedexpansion
if "!fname!"=="!fname:trailer=!" if "!fname!"=="!fname:sample=!" (
echo handbrakecli.exe "!fpath!" &rem put your conversion command here
>>"logfile.log" echo !fname!
)
endlocal
)
The file name+file path is in the variable "!fpath!".
Added some code concerning the needs of the OP:
#echo off &setlocal
rem replace avi with mp4 files in my movie folder
rem grab 4 random folders with avi in them and no mp4
rem Settings for this Batch File
set "moviepath=H:\Movies"
set "logfile=C:\Documents and Settings\%USERNAME%\LogFiles\avi_converter.log"
rem check if log file exists
if not exist "%logfile%" echo(>"%logfile%"
rem create empty convert file
copy nul "convert_movies.bat" >nul 2>&1
rem add echo off
echo #echo off >>"convert_movies.bat"
rem set counter
SET /A COUNT=1
FOR /R "%moviepath%" %%G in (*.avi) DO (
set "fpath=%%~fG"
set "fname=%%~nG"
setlocal enabledelayedexpansion
rem check if count greater than 4
if !COUNT! gtr 4 goto:eof
if "!fname!"=="!fname:trailer=!" if "!fname!"=="!fname:sample=!" (
rem echo handbrakecli.exe "!fpath!" &rem put your conversion command here
rem Send File To HandBrakeCLI
CALL :DOHandBrakeCLI "!fpath!"
rem Delete File
CALL :DeleteOldFile "!fpath!"
rem Add Log Entry
CALL :LogEntry "!fpath!"
rem add line break space
echo( >>"convert_movies.bat"
endlocal
rem increment counter
SET /A COUNT+=1
) else endlocal
)
rem end main program, to close cmd window replace it with EXIT
goto:eof
:DOHandBrakeCLI
rem skip if the parameter is empty
IF "%~1"=="" goto:eof
For %%A in ("%~1") do (
Set "Folder=%%~dpA"
Set "Name=%%~nxA"
)
rem echo %Folder%%Name%
echo start /b "" "c:\handbrakecli\HandBrakeCLI.exe" -i "%~1" -o "%Folder%%~n1.mp4" --preset="High Profile">>"convert_movies.bat"
exit /b
:DeleteOldFile
rem skip if the parameter is empty
IF "%~1"=="" goto:eof
For %%A in ("%~1") do (
Set "Folder=%%~dpA"
Set "Name=%%~nxA"
)
rem sends parameters to deletefile which will make sure new file exists before deleting old one
echo c:\projects\deletefile.bat "%~1" "%Folder%%~n1.mp4">>"convert_movies.bat"
exit /b
:LogEntry
rem skip if the parameter is empty
IF "%~1"=="" goto:eof
echo "%~1">>"%logfile%"
exit /b
This should work:
#echo off
FOR /R "%somepath%" %%G in (*.avi) DO (
echo "%%~nG" |findstr /i "trailer sample">nul || (
rem do conversion
)
)
It's difficult to see where your post is pseudocode and where actual code.
The first sample contains only REM statements, so it's not surprising it apparently does nothing.
Your second and third sample are effectively identical - the only difference is the target string. It's not surprising that the variable skip isn't set to Yes since the correct syntax is
if %errorlevel% equ 0 set skip=Yes
The syntax you've posted will REPORT that skip is not defined - it ignores the Yes
HOWEVER this syntax is only usable OUTSIDE of a "block statement" - that is, a multiple-instruction statement (enclosed in parentheses) or cascaded&by&ampersands. Batch first PARSES a complete statement - from the FOR or if through to the appropriate closing-parenthesis and THEN executes it. As part of the PARSING phase, any %var% - including %errorlevel% is replaced by its value as it stands at the time the entire statement is parsed - not as it changes due to the operation of the for.
In order to use the value as it changes, you need to use
if errorlevel 1 (do_something) else (do_something_else)
where do_something and do_something_else) may themselves be compound statements.
OR
if defined variable (do_something) else (do_something_else)
where the variable either is defined or not
OR
setlocal enabledelayedexpansion
....
if !errorlevel! equ x (do_something) else (do_something_else)
OR
if !var! neq something (do_something) else (do_something_else)
But it's quite possible that
FOR /R "%somepath%" %%G in (*.avi) DO (
echo(%%G|findstr /i "sample trailer" >nul
if errorlevel 1 echo %%G
)
will give you an appropriate skeleton.
Echo the filename through FINDSTR and look for "sample" or "trailer" /i case-insensitive. Findstr sets errorlevel 0 if either target string is found, 1 otherwise - and the if errorlevel x syntax works on the dynamic value of errorlevel within a loop.
#ECHO on &SETLOCAL
REM This script was inspired by Endoro's expanded script
(https://stackoverflow.com/a/16891696/10572786).
REM This batch script will recursively search for all .mp4 files that don't
have (x265) in the file name. Any valid results will be encoded with x265
using FFmpeg. The original .mp4 file will remain unchanged in it's original
folder with the new x265 version.
REM Example: %PATH%\A.mp4 > %PATH%\A(x265).mp4
REM If you don't have ffmpeg.exe on your PC you must download or build it
with Microsoft Visual Studios. I recommend you download and run media
autobuild suite on GitHub: (https://github.com/jb-alvarado/media-
autobuild_suite).
REM Once ffmpeg is compiled/downloaded make sure to set it's folder path as
an environmental variable in Windows before running the script. Change the
script's working directory to your .mp4 files root folder using the "cd"
command.
REM !!BEGIN SCRIPT!!
cd /d %USERPROFILE%\Desktop\Vids\
REM or perhaps use [cd /d %OneDrive%\Desktop\Vids]
REM Set mp4PATH to the root folder you wish to recursively search.
SET "mp4PATH=%USERPROFILE%\Desktop\Vids\"
REM Create empty convert file.
COPY NUL "convert_movies.bat" >NUL 2>&1
REM Add ECHO off.
ECHO #ECHO off >>"convert_movies.bat"
REM Recursively search root folder.
FOR /R "%mp4PATH%" %%G IN (*.mp4) DO (
SET "fpath=%%~fG"
SET "fname=%%~nG"
SETLOCAL enabledelayedexpansion
REM Ignore all files that have "(x265)" in the file name.
IF "!fname!"=="!fname:*(x265)=!" (
CALL :DO_FFmpeg_CLI "!fpath!"
ECHO(>>"convert_movies.bat"
) ELSE ENDLOCAL
)
)
GOTO:EOF
REM CALL variables for use in FFmpeg's command line.
:DO_FFmpeg_CLI
IF "%~1"=="" GOTO:EOF
FOR %%I IN ("%~1") DO (
SET "Folder=%%~dpI"
SET "Name=%%~nxI"
)
REM Export info to "convert_movies.bat and run ffmpeg.exe's command line in the cmd.exe window.
ECHO ffmpeg -y -i "%~1" -c:v libx265 -preset slow -crf
18 -c:a aac "%Folder%%~n1(x265).mp4">>"convert_movies.bat" && ffmpeg |
ffmpeg -y -i "%~1" -c:v libx265 -preset slow
-crf 18 -c:a aac "%Folder%%~n1(x265).mp4"
EXIT /B
PAUSE
The Batch file below solve the original question AND limit the number of converted files to a given number (that does not appear in the original question):
#echo off
setlocal EnableDelayedExpansion
rem Insert in the next line the list of files to skip
set skip=/trailer/sample/
set count=0
FOR /R "%somepath%" %%G in (*.avi) DO (
if /I "!skip:/%%~nG/=!" equ "%skip%" (
echo Current file name is not in skip variable
echo Do conversion on: %%G
set /A count+=1
if !count! equ 20 goto :endLoop
)
)
:endLoop
echo Converted files: %count%

Resources