Why does Windows command interpreter output the error message: ) was unexpected - batch-file

I'm making a Minecraft modding tool using a batch file. But on execution of the batch file the Windows command interpreter outputs the syntax error message:
) was unexpected
I can't figure out why.
This is my code:
#echo off
cd mods
setlocal enabledelayedexpansion
set "selected=1"
call:print 1
call:print 2
:menu
choice /c wse>nul
if "%errorlevel%"=="2" (
if not !selected! GEQ !a! (
set /a "selected+=1"
cls
call:print 1
call:print 2
)
)
if "%errorlevel%"=="1" (
if not !selected!==1 (
set /a "selected-=1"
cls
call:print 1
call:print 2
)
)
if "%errorlevel%"=="3" (
)
goto menu
:print
if "%1"=="1"set a=0
echo.
if "%1"=="1" (
echo Uninstalled:
) else (
echo Installed:
)
echo.
for %%f in (*.jar) do (
if "%1"=="1" (
if NOT EXIST
"C:/Users/Coornhert/AppData/Roaming/.minecraft/mods/%%~nf.jar" (
set /a "a+=1"
if "!a!"=="!selected!" (
echo -%%~nf
) else (
echo %%~nf
)
set "b=!a!"
)
) else (
if EXIST "C:/Users/Coornhert/AppData/Roaming/.minecraft/mods/%%~nf.jar" (
set /a "a+=1"
if "!a!"=="!selected!" (
echo -%%~nf
) else (
echo %%~nf
)
set "b=!a!"
)
)
)
goto :eof
And it works, but when I hit s, execution terminates with the error message.
Folder structure of folder containing the batch file:
mods
Foo.jar
Foo2.jar
Folder structure of target folder:
C:\Users\Coornhert\AppData\Roaming\.minecraft\mods
Foo.jar

I partly do not understand what this batch file should do, but here is the batch file rewritten with several improvements.
#echo off
setlocal EnableExtensions EnableDelayedExpansion
rem cd /D "%~dp0mods"
pushd "%~dp0mods"
set "a=0"
set "selected=1"
call :PrintIt 1
call :PrintIt 2
:Menu
choice /C wse /N
if errorlevel 3 popd & endlocal & goto :EOF
if errorlevel 2 goto AddOne
if %selected% == 1 goto Menu
set /A selected-=1
cls
call :PrintIt 1
call :PrintIt 2
goto Menu
:AddOne
if %selected% GEQ %a% goto Menu
set /A selected+=1
cls
call :PrintIt 1
call :PrintIt 2
goto Menu
:PrintIt
if %1 == 1 set "a=0"
echo/
if %1 == 1 (echo Uninstalled:) else echo Installed:
echo/
for %%I in (*.jar) do (
if %1 == 1 (
if not exist "%APPDATA%\.minecraft\mods\%%~nI.jar" (
set /A a+=1
if !a! == %selected% (echo -%%~nI) else echo %%~nI
set "b=!a!"
)
) else (
if exist "%APPDATA%\.minecraft\mods\%%~nI.jar" (
set /A a+=1
if !a! == %selected% (echo -%%~nI) else echo %%~nI
set "b=!a!"
)
)
)
goto :EOF
It does nothing useful as is, but batch code in question is also not useful at all.
The applied improvements are:
The command SETLOCAL is moved to top of file. The reason is:
It pushes path of current directory on stack.
It pushes state of command extensions on stack.
It pushes state of delayed expansion on stack.
It pushes the memory address of the current environment variables table on stack.
It creates a copy of the current environment variables table in memory and makes this new environment variables table active.
It sets command extensions and delayed expansion according to the specified parameters if the command is called with parameters at all.
The command ENDLOCAL is executed before leaving batch file. The reason is:
It deletes the current environment table which means no environment variable defined in this batch file exists anymore after ENDLOCAL except it existed already before execution of command SETLOCAL.
It pops memory address of previous environment table from stack and uses this address resulting in restoring initial environment variables.
It pops state of delayed expansion from stack and disables/enables delayed expansion accordingly.
It pops state of command extensions from stack and disables/enables command extensions accordingly.
It pops previous current directory path from stack and sets current directory to this path to restore the current directory.
So the entire command process environment is restored on exit of this batch file to exactly the same environment as it was on starting the batch file.
This makes it possible to call this batch file from within another batch file or from within a command prompt window with no impact on calling batch file or command process.
The command CD could be extended to include drive and path of argument 0 which is the full path of the batch file ending with a backslash because the subdirectory mods is most likely always expected in directory of the batch file and it should not matter what is the current directory on running the batch file.
But cd /D "%~dp0mods" could fail if the batch file is located on a network share accessed using UNC path and therefore command PUSHD is used instead working with enabled command extensions also for UNC paths.
In all programming and scripting languages it is required that variables are defined and initialized with a value before being used the first time. For that reason the environment variables a and selected are defined at top of the batch file with default values. By the way: a is a very bad name for a variable. Why? Search for a in batch file. It is quite often found on not using special find features like whole word only, isn't it.
PRINT is a command as it can be seen on running in a command prompt window print /?. While it is possible to use command names as labels or as names for subroutines, it is not advisable to do so as it could be confusing for readers of the batch file.
The command CHOICE has the option /N to hide the list of choices in the prompt. It is better to use this option than redirecting the output of CHOICE to device NUL.
The very old but still valid Microsoft support article Testing for a Specific Error Level in Batch Files explains that if errorlevel X means that the condition is true if the exit code of previous command or application is greater or equal X. The command CHOICE with 3 choices exits always with 1, 2 or 3 as exit code. So it is best to use:
if errorlevel 3 rem Do something on third choice avoiding fall through to next line.
if errorlevel 2 rem Do something on second choice avoiding fall through to next line.
Do something on first choice.
The advantage of using this method is that it even works with CHOICE within a command block on which if %ERRORLEVEL% == X fails because of delayed expansion would be required and usage of if !ERRORLEVEL! == X.
The integer comparison if %selected% GEQ %a% would not work if the two arguments would be enclosed in double quotes as the double quotes are also interpreted as part of the arguments to compare. For that reason using if "%selected%" GEQ "%a%" would result in running a string comparison instead of an integer comparison. For more information about comparing values with IF look on answer on Exit a for loop in batch.
It is safe here to omit the double quotes also on the other IF conditions with == operator running string comparisons because the environment variables selected and a must be both defined before running this IF condition and therefore both variables are defined at top of the batch file.
The answer on Why is no string output with 'echo %var%' after using 'set var = text' on command line? explains why set "variable=value" should be always used to assign a value to an environment variable or delete an environment variable on omitting the value. And this answer also explains why on set /A variable=expression the double quotes can be usually omitted as whitespace characters are interpreted completely different within an arithmetic expression. The exception is usage of set /A with 1 or more commands on same command line on which double quotes around variable=expression would be also needed.
The batch file should be exited when the batch file user enters e or E to take third choice. This could be done with just goto :EOF, or with exit /B which is an alias for goto :EOF, or with just exit which always exits entire command process independent on calling hierarchy which is not recommended. Windows command interpreter would implicitly restore the initial stack before finishing batch file execution. But it is nevertheless good coding practice to pop from stack with code which was pushed on stack before with code. For that reason there is used popd & endlocal & goto :EOF. See answer on Single line with multiple commands using Windows batch file for more information about usage of multiple commands on one command line.
The list of predefined environment variables of used user account is output on running in a command prompt window the command set. One predefined Windows environment variable is APPDATA with path to application data of current user account. This environment variable should be used instead of a fixed path to application data directory of user account.
And the directory separator on Windows is the backslash character \ and not slash character / as on Unix and Mac.
The usage of f as loop variable is not recommended as this is also a loop variable modifier. %%~f can be interpreted by Windows command interpreter as value of loop variable f without surrounding double quotes or as incomplete loop variable reference because of missing loop variable after %%~f which could be also interpreted as full file name of ?. So it is better to use # or $ as loop variable or upper case letters to avoid such a confusion on interpreting the loop variable reference. Loop variables are case-sensitive.
I prefer for IF conditions with an ELSE branch the coding style
if condition (
command
) else (
command
)
But here in this batch file with command being just a short ECHO command the code is better readable on being more compact with using:
if condition (echo short message) else echo other short message
Delayed expansion for an environment variable referenced within a command block started with ( and ending with matching ) is only needed if the environment variable is also modified in same command block. Therefore environment variable a must be referenced in body of FOR with usage of delayed expansion while environment variable selected can be referenced as usual because of not modified within this command block at all.
It is better to use echo/ to output an empty line instead of echo.. For the reason read the DosTips forum topic: ECHO. FAILS to give text or blank line - Instead use ECHO/
For a basic understanding of the used commands, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
choice /?
cls /?
echo /?
endlocal /?
for /?
goto /?
if /?
popd /?
pushd /?
rem /?
set /?
setlocal /?

Related

How can i call resgen in a bat file if the file is edited?

I need to create a resource file using resgen.exe only if the file is edited.
I've found a way to do it, and i need to loop through all the Language available.
This is my script.
#echo on
echo ------------------------------
echo -- Starting a run of resgen --
echo ------------------------------
Set resourcesPath=%~1Resources\
Set configuration=%~2
Set platform=%~3
set landingPath=%~1bin\%configuration%\Resources\
echo %landingPath%
IF exist %landingPath% ( echo %landingPath% exists ) ELSE ( mkdir %landingPath% && echo %landingPath% created)
set obj[0].Resource="%landingPath%Strings.en-GB.resources"
set obj[0].Text="%resourcesPath%Strings.en-GB.txt"
set obj[1].Resource="%landingPath%Strings.ru-RU.resources"
set obj[1].Text="%resourcesPath%Strings.ru-RU.txt"
set obj[2].Resource="%landingPath%Strings.es-ES.resources"
set obj[2].Text="%resourcesPath%Strings.es-ES.txt"
FOR /L %%i IN (0 1 2) DO (
for %%x in ("%%obj[%%i].Text%%") do set date_of_filenameTxt=%%~tx
for %%x in ("%%obj[%%i].Resource%%") do set date_of_filenameRes=%%~tx
ECHO %date_of_filenameTxt:~0, 16%
ECHO %date_of_filenameRes:~0, 16%
IF "%date_of_filenameTxt:~0, 16%" == "%date_of_filenameRes:~0, 16%" call :same
call :notsame
:same
(ECHO "No Copy for the :" %%obj[%%i].Text%% )
call :end
:notsame
"%resourcesPath%resgen.exe" %%obj[%%i].Text%% %%obj[%%i].Resource%%
:end
)
The problem is on getting the string from the obj[], how should be the sintax?
i've found if i do as below, it works.
call echo resource = %%obj[0].Resource%%
The task could be done much easier with the following code:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
echo ------------------------------
echo -- Starting a run of resgen --
echo ------------------------------
set "resourcesPath=%~1Resources\"
set "configuration=%~2"
set "platform=%~3"
set "landingPath=%~1bin\%configuration%\Resources\"
if exist "%landingPath%" echo "%landingPath%" exists.& goto ProcessFiles
mkdir "%landingPath%"
if exist "%landingPath%" echo "%landingPath%" created.& goto ProcessFiles
echo ERROR: "%landingPath%" could not be created!& goto EndBatch
:ProcessFiles
for %%I in ("%resourcesPath%Strings.*-*.txt") do (
set "RunResgen=1"
for %%J in ("%landingPath%%%~nI.resources") do if "%%~tJ" == "%%~tI" set "RunResgen="
if defined RunResgen "%resourcesPath%resgen.exe" "%%I" "%landingPath%%%~nI.resources"
)
:EndBatch
endlocal
The outer FOR loop searches in the directory Resources for non-hidden files matching the wildcard pattern Strings.*-*.txt. There could be used also the wildcard pattern Strings.*.txt or the pattern Strings.??-??.txt.
For a found text file there is first defined the environment variable RunResgen with string value 1 whereby the value does not matter.
Next is executed one more FOR processing the resource file in landing Resources directory. If that file does not exist at all, %%~tJ expands to an empty string which means the IF condition compares "" with something like "10.11.2021 19:00" and so the condition is not true. If the appropriate .resources file exists, its last modification time is compared with the last modification time of the .txt file. If the two file time stamps are equal, the environment variable RunResgen is undefined for the next IF condition because of no need to run resgen.exe for this pair of text and resources files.
The second IF condition checks the existence of the environment variable RunResgen and if this variable still exists because of .resources file does not exist at all or has not the same last modification time as the .txt file, the executable resgen.exe is executed with the two file names.
Please note that the file date/time format depends on the country setting of the used account. On my Windows machine the date/time format is dd.MM.yyyy hh:mm and for that reason the simple string comparison works with these sixteen characters plus the two surrounding quotes taken also into account on comparing the two strings.
resgen.exe must create the .resources file with last modification date/time explicitly set to last modification date/time of the .txt file or this code does not work at all.
There is usually used on Windows the archive file attribute do determine if a source file was modified since last processing it. But I suppose this is not possible here as one .txt file could be used for multiple .resources files for multiple configurations and platforms (wherever the environment variable platform is used in real batch file).
Well, the main code could be even more optimized by using just following single line:
for %%I in ("%resourcesPath%Strings.*-*.txt") do for %%J in ("%landingPath%%%~nI.resources") do if not "%%~tJ" == "%%~tI" "%resourcesPath%resgen.exe" "%%I" "%landingPath%%%~nI.resources"
The executable resgen.exe is executed on .resources file not existing at all or its last modification date/time is different to the last modification date/time of the .txt file.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
echo /?
endlocal /?
for /?
goto /?
if /?
mkdir /?
set /?
setlocal /?
See also single line with multiple commands using Windows batch file for an explanation of the operator & to execute always a GOTO after an ECHO.

Get directory name from array in Batch [duplicate]

This question already has answers here:
Arrays, linked lists and other data structures in cmd.exe (batch) script
(11 answers)
Closed 3 years ago.
I have a list of paths from which I want to extract folder name
I wrote:
#echo off
set paths[0]="C:\p\test1"
set paths[1]="C:\p\test2"
set paths[2]="C:\p\test3"
(for %%p in (%paths%) do (
for %%F in (%%p) do echo Processing %%~nxF
))
but seems that nothing is shown.
I expected to see:
Processing test1
Processing test2
Processing test3
It makes a big difference if first " is specified on a set command line left to variable name or left to variable value. In most cases it is better to specify it left to the variable name, especially if a variable value holding a path should be concatenated later with a file name to a full qualified file name.
See also: Why is no string output with 'echo %var%' after using 'set var = text' on command line?
The solution for this task is:
#echo off
set "paths[0]=C:\p\test1"
set "paths[1]=C:\p\test2"
set "paths[2]=C:\p\test3"
for /F "tokens=1* delims==" %%I in ('set paths[ 2^>nul') do echo Processing %%~nxJ
The command FOR with option /F and a set enclosed in ' results in starting one more command process running in background with %ComSpec% /c and the command line specified between the two ' appended as further arguments. So executed is in this case with Windows installed to C:\Windows:
C:\Windows\System32\cmd.exe /c set paths[ 2>nul
The command SET outputs all environment variables of which name starts with paths[ line by line using the format VariableName=VariableValue to handle STDOUT of started background command process.
It could be that there is no environment variable of which name starts with paths[ which would result in an error message output to handle STDERR by command SET which would be redirected from background command process to handle STDERR of the command process which is processing the batch file and for that reason would be displayed in console window. For that reason a possible error message is redirected by the background command process to device NUL to suppress it with using 2>nul.
Read the Microsoft article about Using command redirection operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded set command line with using a separate command process started in background.
FOR captures in this case everything written to handle STDOUT of started background command process and process this output line by line after started cmd.exe terminated itself.
Empty lines are ignored by FOR which does not matter here as there are no empty lines to process.
FOR would split up a non-empty line into substrings using normal space and horizontal tab as string delimiters and would assign just first space/tab separated string to specified loop variable, if it does not start with default end of line character ;. This default line splitting behavior is not wanted here. For that reason the option delims== defines the equal sign as string delimiter.
The option tokens=1* instructs FOR to assign in this case the variable name to specified loop variable I and assign everything after the equal sign(s) after variable name without any further string splitting on equal signs to next loop variable according to ASCII table which is in this case J. That is the reason why loop variables are interpreted case-sensitive while environment variables are handled case-insensitive by the Windows command processor.
In this case only the variable value is of interest in the body of the FOR loop. For that reason just loop variable J is used on ECHO command line while I is not used at all.
The modifier %~nxJ results in removing surrounding double quotes from string value assigned to loop variable J and next get the string after last backslash or beginning of string in case of the string value does not contain a backslash at all. This is the name of the last folder in folder path string.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
echo /?
for /?
set /?
UPDATE:
There is a big advantage of this solution in comparison to the other two solutions posted up to now here:
There is not used delayed environment variable expansion which is always problematic on working with file or folder names on not being 100% sure that no folder and no file contains ever an exclamation mark in its name.
Let us compare the three solutions with unusual folder names containing !.
#echo off
rem Make sure there is no environment variable defined of which name starts with
rem paths[ as suggested by Compo which is a very valuable addition on my code.
for /F "delims==" %%I in ('set paths[ 2^>nul') do set "%%I="
set "paths[0]=C:\p\test1!"
set "paths[1]=C:\p\!test2"
set "paths[2]=C:\p\!test!3"
echo/
echo Results of solution 1:
echo/
for /F "tokens=1* delims==" %%I in ('set paths[ 2^>nul') do echo Processing %%~nxJ
echo/
echo Results of solution 2:
echo/
SetLocal EnableDelayedExpansion
for /L %%i in (0,1,2) do (
for %%j in (!paths[%%i]!) do echo Processing %%~nxj
)
endLocal
echo/
echo Results of solution 3:
echo/
Setlocal EnableDelayedExpansion
Call :process paths "!paths[0]!" "!paths[1]!" "!paths[2]!"
Endlocal
echo/
pause
goto :EOF
:process
Set P_C=0
Set /a P_C-=1
For %%a in (%*) DO (
CALL :populate %1 "%%~a"
)
Set /a P_C-=1
For /L %%b in (0,1,!P_C!) DO (
ECHO Processing %1[%%b] = "!%1[%%b]!"
)
GOTO :EOF
:populate
Set "%1[!P_C!]=%~2"
Set /a P_C+=1
GOTO :EOF
The output on running this batch file is:
Results of solution 1:
Processing test1!
Processing !test2
Processing !test!3
Results of solution 2:
Processing test1
Processing test2
Processing 3
Results of solution 3:
Processing paths[0] = "C:\p\test1\p\\p\3"
Solution 1 as posted here works for all three folder names correct.
Solution 2 omits for first and second folder name the exclamation mark which will most likely cause errors on further processing. The third folder name is modified to something completely different. Enabled delayed expansion results in parsing a second time echo Processing %%~nxj after %~nxj being replaced by !test!3 with interpreting test in folder name now as environment variable name of which value is referenced delayed. There was no environment variable test defined on running this batch file and so !test!3 became just 3 before echo was executed by Windows command processor.
Solution 3 produces garbage on any folder name contains an exclamation mark, even on full qualified folder name defined before enabling delayed expansion and referenced with delayed expansion on calling the subroutine process.
Well, folder and file names with an exclamation mark in name are fortunately rare which makes the usage of delayed expansion usually no problem. But I want to mention here nevertheless the potential problems which could occur on any folder name containing one or more !.
Something like that should work :
#echo off
set paths[0]="C:\p\test1"
set paths[1]="C:\p\test2"
set paths[2]="C:\p\test3"
SetLocal EnableDelayedExpansion
for /L %%i in (0,1,2) do (
for %%j in (!paths[%%i]!) do echo Processing %%~nxj
)
pause
Define the Array within the function.
This approach can be used to define multiplay Arrays.
#ECHO OFF
Setlocal EnableDelayedExpansion
:: REM P_C is used to define the range of the Array. The -1 operations on P_C is to shift the paths parameter out of the Arrays working Index.
::REM the first parameter passed is used as the Arrays Name. all other parameters are assigned to index values 0 +
Call :process paths "C:\p\test1" "C:\p\test2" "C:\p\test3"
pause
:process
Set P_C=0
Set /a P_C-=1
For %%a in (%*) DO (
CALL :populate %1 "%%~a"
)
Set /a P_C-=1
For /L %%b in (0,1,!P_C!) DO (
ECHO Processing %1[%%b] = "!%1[%%b]!"
)
GOTO :EOF
:populate
Set "%1[!P_C!]=%~2"
Set /a P_C+=1
GOTO :EOF

Batch "if" statement not working

I seem to have a problem with my "if" statements. Currently after the pause in "start" the file just closes and nothing else happens. Chances are, I have no idea what I'm doing.
#echo off
set startnum=0
goto start
:start
pause
set startnum="!startnum!"+1
if "%startnum%"="%0%" goto fail
if "%startnum%"="%1%" goto success
goto start
:success
cls
echo It worked!
echo "%startnum%"
pause.
exit
:fail
cls
echo Failure
pause.
exit
First problem:
set startnum="!startnum!"+1
Evidently, you wish to add 1 to startnum.
Your set command would set startnum to "!startnum!"+1. Literally. To perform arithmetic, you need set /a.
set /A startnum="!startnum!"+1
well, this won't work as "!startnum! isn't numeric. Had you invoked delayedexpansion beforehand, then the value of startnum would have been substituted for !startnum! yielding set /A startnum="0"+1 which makes more, but still not much sense.
set /A startnum=startnum+1
adds 1 to startnum - see set /? from the prompt for documentation.
set /A startnum+=1
would also add 1 to startnum.
Next problem.
if "%startnum%"="%0%" goto fail
Well, you appear to have found lss and all that gang. Problem is that the simple comparison operator is ==, not =.
if "%startnum%"=="%0%" goto fail
Now - what will that do? It will compare "thecontentsofstartnum" to "thecontentsof0". Since both of these arguments are quoted, batch will perform a string comparison. With a string comparison, 123 is less than 89 because 1 is less than 8.
But - you are attempting an equivalence comparison (equ as the operator may be used instead of ==) so the preceding point is simply AAMOI.
The difficulty is %0% which you may believe attempts to extract the value of the variable 0 but actually it replaces %0 with the value of the 0th parameter to the batchfile, which is the batchfile name itself, so you get "batchfilename%" - probably not what you actually wanted.
if "%startnum%"=="0" goto fail
is the way to implement that test.
The first IF statement is preprocessed by cmd.exe to
if ""!startnum!"+1"="test.bat" goto fail
which is a completely invalid IF condition.
cmd.exe outputs a syntax error message because of "="test.bat"" and exits batch file processing. This can be seen by debugging the batch file.
The solution is using right syntax for
assigning a value to an environment variable,
an arithmetic expression,
and last but not least the IF condition itself.
The batch file code fixed:
#echo off
set "startnum=0"
goto Begin
:Begin
set /A startnum+=1
if "%startnum%" == "0" goto Fail
if "%startnum%" == "1" goto Success
goto Begin
:Success
cls
echo It worked!
echo "%startnum%"
pause
exit /B
:Fail
cls
echo Failure
pause
exit /B
It would be safe here to remove all double quotes on both IF conditions.
One more hint: Don't use the name of a command like START as label. It works, but it should be avoided in case of ever adding to batch file the command START and searching for either command or label.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /? ... explains how to reference batch file arguments.
cls /?
echo /?
exit /?
goto /?
if /?
pause /?
set /?
Further read the answers on following questions:
Batch file comparison of variable with constant fails
Why is no string output with 'echo %var%' after using 'set var = text' on command line?
Where does GOTO :EOF return to?
Symbol equivalent to NEQ, LSS, GTR, etc. in Windows batch files

The system cannot find the path specified (Batch file) - Using path with white spaces - PENTAHO spoon.bat (pdi-ce-7.1.0.0-12)

I'm trying to execute a batch file "spoon.bat" from PENTAHO (pdi-ce-7.1.0.0-12), but there is an error.
Part of batch file where the error is found:
if "%SPOON_CONSOLE%"=="1" set PENTAHO_JAVA=C:\Program Files (x86)\Java\jre1.8.0_121\bin\java.exe
if not "%SPOON_CONSOLE%"=="1" set PENTAHO_JAVA=C:\Program Files (x86)\Java\jre1.8.0_121\bin\javaw.exe
set IS64BITJAVA=0
call "%~dp0set-pentaho-env.bat"
But I receive next below error:
The system cannot find the path specified
The error is when I'm trying to assign the path where java.exe or javaw.exe are found to "PENTAHO_JAVA".
I've modified the path with double quote mark, but doesn't work; and also I've modified as:
if "%SPOON_CONSOLE%"=="1" set "PENTAHO_JAVA=C:\<Program Files (x86)>\Java\jre1.8.0_121\bin\java.exe"
Any idea to how declare it to fix it?
Where is the environment variable PENTAHO_JAVA referenced?
It must be referenced with "%PENTAHO_JAVA%" because the string assigned to this environment variable contains characters like a space or &()[]{}^=;!'+,`~. This is explained in help of Windows command interpreter output on running in a command prompt window cmd /? in last paragraph on last help page.
It is of course also possible to define the environment variable with the necessary double quotes already added, i.e. use:
if "%SPOON_CONSOLE%"=="1" set "PENTAHO_JAVA="%ProgramFiles(x86)%\Java\jre1.8.0_121\bin\java.exe""
if not "%SPOON_CONSOLE%"=="1" set "PENTAHO_JAVA="%ProgramFiles(x86)%\Java\jre1.8.0_121\bin\javaw.exe""
set "IS64BITJAVA=0"
call "%~dp0set-pentaho-env.bat"
But this is not recommended. Better would be to use
if "%SPOON_CONSOLE%"=="1" set "PENTAHO_JAVA=%ProgramFiles(x86)%\Java\jre1.8.0_121\bin\java.exe"
if not "%SPOON_CONSOLE%"=="1" set "PENTAHO_JAVA=%ProgramFiles(x86)%\Java\jre1.8.0_121\bin\javaw.exe"
set "IS64BITJAVA=0"
call "%~dp0set-pentaho-env.bat"
and reference environment variable PENTAHO_JAVA enclosed in double quotes where it is necessary to specify its value enclosed in double quotes.
Example:
#echo off
rem Get path of latest installed Java directly from Windows registry.
for /F "skip=1 tokens=1,2*" %%N in ('%SystemRoot%\System32\reg.exe QUERY "HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths\javaws.exe" /v Path 2^>nul') do if /I "%%N" == "Path" set "PENTAHO_JAVA=%%P" & goto JavaPathFound
rem Path of Java not found in registry, search for 32-bit Java in the default
rem program files folders of 64-bit and 32-bit Windows and take first found.
if "%ProgramFiles(x86)%" == "" goto Windows_x86
for /R "%ProgramFiles(x86)%" %%I in (java*.exe) do set "PENTAHO_JAVA=%%~dpI" & goto JavaPathFound
:Windows_x86
for /R "%ProgramFiles%" %%I in (java*.exe) do set "PENTAHO_JAVA=%%~dpI" & goto JavaPathFound
echo Error: Java binary directory not found.
echo/
pause
goto :EOF
:ErrorJavaEXE
echo Error: File %PENTAHO_JAVA% not found.
echo/
pause
goto :EOF
:JavaPathFound
if not "%PENTAHO_JAVA:~-1%" == "\" set "PENTAHO_JAVA=%PENTAHO_JAVA%\"
if "%SPOON_CONSOLE%" == "1" (
set "PENTAHO_JAVA=%PENTAHO_JAVA%java.exe"
) else (
set "PENTAHO_JAVA=%PENTAHO_JAVA%javaw.exe"
)
rem Check existence of Java executable to run.
if not exist "%PENTAHO_JAVA%" goto ErrorJavaEXE
"%PENTAHO_JAVA%" -version
call "%~dp0set-pentaho-env.bat"
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
echo /?
for /?
goto /?
if /?
pause /?
reg /?
reg query /?
rem /?
Read also the Microsoft article about Using Command Redirection Operators for an explanation of 2>nul whereby redirection operator must be escaped in this batch code on FOR command line with caret character ^. And read answer on Single line with multiple commands using Windows batch file for an explanation of & operator.
Generally, don't modify the shell scripts coming with Kettle. You're supposed to set certain environment variables to adapt the scripts to your runtime environment. Look at the top comment section in script set-pentaho-env to learn what's best for your system.
BTW: The current Java 8 security baseline 8u131 was released in April 2017 - you're way behind that. Also, are you aware of the fact that you are using a 32bit JVM with limited RAM support?

How to ask user of batch file for a folder name/path?

I was able to create this batch file to move certain files from one folder to another. But I want to be able to use it also on different folders. For instance here I'm only moving files from UTS16. I want to use this batch file also for other folders like UTS15, UTS14, UTS13, UTS12, etc.
What do I need to change in code to ask the batch user on which folder to run? What am I missing?
#echo off
SET /P letter=Please give your drive letter and press ENTER:
ECHO %letter%
PAUSE
SET Datefolder="%date:~10,4%_%date:~4,2%_%date:~7,2%_%time:~0,2%%time:~3,2%"
MD "%Datefolder%"
mkdir %letter%:\UTS16\Database\"RTBackup%Datefolder%"
move /-y "%letter%:\UTS16\Database\*.dbf" "%letter%:\UTS16\Database\RTBackup%Datefolder%"
move /-y "%letter%:\UTS16\Database\*.cdx" "%letter%:\UTS16\Database\RTBackup%Datefolder%"
move /-y "%letter%:\UTS16\Database\*.~cd" "%letter%:\UTS16\Database\RTBackup%Datefolder%"
move /-y "%letter%:\UTS16\Database\*.~db" "%letter%:\UTS16\Database\RTBackup%Datefolder%"
move /-y "%letter%:\UTS16\Database\*.fpt" "%letter%:\UTS16\Database\RTBackup%Datefolder%"
move /-y "%letter%:\UTS16\Database\RTBackup%Datefolder%\zipdata.dbf" "%letter%:\UTS16\Database\"
pause
start "" %letter%:\UTS16/dbrepair.exe
I suggest for your task following commented batch file:
#echo off
setlocal EnableExtensions EnableDelayedExpansion
goto UserPrompt
rem Define environment variable BaseFolder with a double quote character as
rem value in case of the user enters nothing on prompt in which case the
rem variable BaseFolder is still defined with the double quote as value.
rem Then let the user enter the folder path or drag and drop
rem the folder over the console window to enter the path.
rem Next remove all double quotes from folder path and test
rem if the variable BaseFolder still exists with a value.
rem Last replace forward slashes by backslashes in case of user entered
rem the path with forward slashes, make sure the folder path does not
rem end with a backslash and test if the folder really exists in case of
rem user has made a typing mistake on entering manually the folder path.
rem Run the backup and repair operation if entered folder exists as expected.
:UserPrompt
cls
echo/
echo Please type the database base folder path and press ENTER.
echo/
echo Or alternatively drag ^& drop the folder from Windows
echo Explorer on this console window and press ENTER.
echo/
set "BaseFolder=""
set /P "BaseFolder=Path: "
set "BaseFolder=!BaseFolder:"=!"
if "!BaseFolder!" == "" goto UserPrompt
set "BaseFolder=!BaseFolder:/=\!"
if "!BaseFolder:~-1!" == "\" set "BaseFolder=!BaseFolder:~0,-1!"
if "!BaseFolder!" == "" goto UserPrompt
echo/
if not exist "!BaseFolder!\Database\*" (
echo There is no folder "!BaseFolder!\Database".
echo/
choice "Do you want to enter the path once again "
if errorlevel 2 goto ExitBatch
goto UserPrompt
)
set "BackupFolder=%BaseFolder%\Database\RTBackup%DATE:~10,4%_%DATE:~4,2%_%DATE:~7,2%_%TIME:~0,2%%TIME:~3,2%"
rem For German date/time format which is for DATE TIME: dd.mm.yyy hh:mm:ss,xx
rem set "BackupFolder=%BaseFolder%\Database\RTBackup%DATE:~-4%_%DATE:~-7,2%_%DATE:~-10,2%_%TIME:~0,2%%TIME:~3,2%"
if exist "%BackupFolder%\*" goto MakeBackup
md "%BackupFolder%"
if errorlevel 1 (
echo/
echo Error: Failed to create backup folder !BackupFolder!
echo/
choice "Repair without making a backup "
if errorlevel 2 goto ExitBatch
goto RunRepair
)
:MakeBackup
echo Making a backup to folder !BackupFolder! ...
move /-y "%BaseFolder%\Database\*.dbf" "%BackupFolder%" 2>nul
move /-y "%BaseFolder%\Database\*.cdx" "%BackupFolder%" 2>nul
move /-y "%BaseFolder%\Database\*.~cd" "%BackupFolder%" 2>nul
move /-y "%BaseFolder%\Database\*.~db" "%BackupFolder%" 2>nul
move /-y "%BaseFolder%\Database\*.fpt" "%BackupFolder%" 2>nul
move /-y "%BackupFolder%\zipdata.dbf" "%BaseFolder%\Database\" 2>nul
:RunRepair
echo/
echo Running database repair ...
"%BaseFolder%\dbrepair.exe"
:ExitBatch
endlocal
Please read first answer on Why is no string output with 'echo %var%' after using 'set var = text' on command line? explaining the difference between set variable="value" and set "variable=value".
The environment variable Datefolder was created in batch file in question with double quotes included in environment variable value resulting in expanding
"%letter%:\UTS16\Database\RTBackup%Datefolder%"
for example to
"C:\UTS16\Database\RTBackup"2017_01_13_1250""
which of course is not good. Double quotes inside a double quoted string is in general not correct.
And the command line
mkdir %letter%:\UTS16\Database\"RTBackup%Datefolder%"
expanded for example to
mkdir C:\UTS16\Database\"RTBackup"2017_01_13_1250""
Error correction of Windows must do overtime to fix the folder paths.
The date/time format of the environment variables DATE and TIME depends on Windows region and language settings of current user. I needed a different line to define the backup folder with date and time in name for my German Windows machine. There are region independent solutions posted for example at How to get current datetime on Windows command line, in a suitable format for using in a filename? However, if the faster command line using the environment variable DATE and TIME work on the computers where this batch file is used, there is no need to replace that line with a region independent solution.
The batch file in this answer uses delayed expansion of environment variables mainly to prevent an exit of batch processing in case of user enters by mistake a path string which results without usage of delayed expansion in a syntax error.
The user can drag & drop the folder also for example from Windows Explorer over the console window to enter the folder path on prompt.
echo/ is used to output a blank line which is better than echo. as explained by DosTips forum topic ECHO. FAILS to give text or blank line - Instead use ECHO/
The ampersand & is interpreted by Windows command interpreter as operator for executing multiple commands in one command line in an unquoted string. For that reason it is necessary to escape this character with the caret character ^ to be interpreted as literal character to echo into the console window. See Single line with multiple commands using Windows batch file for details on meaning of &, && and || in a command line in an unquoted string and not escaped with ^.
The command choice is used on asking if the user wants to proceed on error or exit the batch file. This command appends to prompt text in square brackets the keys to press for Yes or No in language of Windows and a question mark, i.e. [Y,N]? on English Windows or [J,N]? on German Windows. choice does not allow any other key before exiting. The exit code assigned to errorlevel is 2 on No and 1 on Yes.
The Microsoft support article Testing for a Specific Error Level in Batch Files explains the usage of if errorlevel to test on exit code of previous command or application. In this case it is enough to test on errorlevel being greater or equal 2 to exit batch processing on an error in case of user chooses No.
The batch file does not check if "%BaseFolder%\dbrepair.exe" really exists before it tries to execute this application. It would be good if that additional check with appropriate error message for the user is added, best before creating the backup folder.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
choice /?
cls /?
echo /?
endlocal /?
goto /?
if /?
md /?
move /?
set /?
setlocal /?
And read also the Microsoft TechNet article Using command redirection operators for an explanation of 2>nul.

Resources