How do I make a dynamic config maker? batch - windows

I am trying to make a batch script that makes a config file for each file i a specified folder.
After it has created the config files then it executes a program that uses a selected config file then converts the specified file in the config.
here is my current code:
:start
#echo off
cls
cd /d %~dp0
goto source
rem This sets up the working folder. I usually do this because I need to but I am
rem not sure about it this time...
:source
echo Please select the folder with the ARF files in it.
set /p source=E.G. C:\Users\Elliot\Desktop\toconvert:
goto dest
rem This sets the folder location for the ARF files.
rem I am going to add a menu for simplicity sake later.
:dest
echo Please select where the mp4 files should appear.
set /p dest=E.G. C:\Users\Elliot\Desktop\Converted:
goto cfgname
rem This sets the output folder for the converted files.
rem Again I will make a menu in the future.
:cfgname
goto createcfg
rem this sets the name of the CFG that will be created for the ARF file
:createcfg
echo [Console] >> %MP4%.cfg
echo inputfile=%source%\1.arf >> MP4.cfg
echo media=MP4 >> MP4.cfg
echo showui=1 >> MP4.cfg
echo [UI] >> MP4.cfg
echo chat=1 >> MP4.cfg
echo qa=0 >> MP4.cfg
echo largeroutline=1 >> MP4.cfg
echo [MP4] >> MP4.cfg
echo outputfile=%dest%\1.mp4 >> MP4.cfg
echo width=1024 >> MP4.cfg
echo height=768 >> MP4.cfg
echo framerate=10 >> MP4.cfg
rem This makes the config file that is required by the player to convert
:convert
cd C:\programdata\webex\webex\500
nbrplay.exe -Convert "%cfg%"
:end
echo Thanks for using Elliot Labs Auto Converter.
echo for feature requests please email: elliot-labs#live.com
pause | echo Press any key to exit...
exit
:preloop
set num=0
goto loop
:loop
cls
echo %num%
pause
set /a num+=1
if %num%==6 (goto end) ELSE goto loop
This only works for one file. How do I make it apply to a whole folder.

#echo off &setlocal
for %%a in ("%cd%\*.arf") do call:MakeCFG "%%~a"
goto:eof
:MakeCFG
setlocal
set "MP4=%~n1"
set "source=%~dp1"
set "filename=%~n1"
set "dest=X:\destination"
(
ECHO([Console]
ECHO(inputfile=%source%%filename%
ECHO(media=MP4
ECHO(showui=1
ECHO([UI]
ECHO(chat=1
ECHO(qa=0
ECHO(largeroutline=1
ECHO([MP4]
ECHO(outputfile=%dest%\%filename%.mp4
ECHO(width=1024
ECHO(height=768
ECHO(framerate=10
)>"%MP4%.cfg"
exit /b 0

Related

"ECHO is on" is setting in variable

I have a batch file which takes three parameters [log:path], [logok:path], [logerr:path]. The values of the respective parameter is log:c:\logs\install.log, logok:c:\logs\installok.log,
logerr:c:\logs\installerr.log.
I need to process these 3 parameters and set it on respective variable log=c:\logs\install.log, logok=c:\logs\installok.log, logerr=c:\logs\installerr.log so that I can use them in next step to create a file in those paths.
I have written below script but somehow each variable is printing "ECHO is on". It should actually print the location path. Any idea how to achieve this?
REM PARAMS ARE [LOG:PATH] [LOGOK:PATH] [LOGERR:PATH]
REM ACCESS WITH '%LOG%', '%LOGOK%' AND '%LOGERR%'
REM SETUP LOCALIZED ENVIRONMENT VARIABLES FOR THE LOG, LOGOK AND LOGERR PARAMS
SETLOCAL ENABLEDELAYEDEXPANSION
FOR %%A IN (%*) DO (
ECHO %%A >> C:\LOGS\TEST1.TXT
FOR /F "TOKENS=1,2,3 DELIMS=:" %%G IN ("%%A") DO (
SET %%G=%%H:%%I
)
)
ENDLOCAL
ECHO %LOG% >> C:\LOGS\TEST2.TXT
ECHO %LOGOK% >> C:\LOGS\TEST3.TXT
ECHO %LOGERR% >> C:\LOGS\TEST4.TXT
START /WAIT MSIEXEC /I "%~DP0SETUP.MSI" /QN
SET EXIT_CODE=%ERRORLEVEL%
REM If the instalaltion is successful it should a create installok.log file in 'c:\logs'
IF %EXIT_CODE% EQU 0 ECHO INSTALLED >> %LOGOK%
REM If it fails then it should a create installerr.log file 'c:\logs')
IF NOT EXIST %LOGOK% ECHO FAILED >> %LOGERR%
Output of TEST1.TXT:
log:c:\logs\install.log
logok:c:\logs\installok.log
logerr:c:\logs\installerr.log
Output of TEST1.TXT,TEST2.TXT,TEST3.TXT:
ECHO is on.
Try this:
REM PARAMS ARE [LOG:PATH] [LOGOK:PATH] [LOGERR:PATH]
REM ACCESS WITH '%LOG%', '%LOGOK%' AND '%LOGERR%'
REM SETUP LOCALIZED ENVIRONMENT VARIABLES FOR THE LOG, LOGOK AND LOGERR PARAMS
SETLOCAL ENABLEDELAYEDEXPANSION
FOR %%A IN (%*) DO (
ECHO %%A >> c:\Logs\TEST1.TXT
FOR /F "TOKENS=1,2,3 DELIMS=:" %%G IN ("%%A") DO (
SET %%G=%%H:%%I
)
)
ECHO !LOG! >> c:\LOGS\TEST2.TXT
ECHO !LOGOK! >> c:\LOGS\TEST3.TXT
ECHO !LOGERR! >> c:\LOGS\TEST4.TXT
START /WAIT MSIEXEC /I "%~DP0SETUP.MSI" /QN
SET EXIT_CODE=%ERRORLEVEL%
REM If the instalaltion is successful it should a create installok.log file in 'c:\logs'
IF %EXIT_CODE% EQU 0 ECHO INSTALLED >> !LOGOK!
REM If it fails then it should a create installerr.log file 'c:\logs')
IF NOT EXIST !LOGOK! ECHO FAILED >> !LOGERR!
ENDLOCAL
Basically this:
Extends the local scope so that the variables which were assigned inside the FOR loop will be available in the rest of the script
Moves variables to the !delayed! variable syntax in order to cause them to be evaluated as late as possible. See the documentation for "EnableDelayedExpansion" for more details
Note that "ECHO is on." is the output that you get when calling echo with no parameters:
C:\>type echo_test.bat
echo
echo %no_such_variable%
C:\>echo_test.bat
C:\>echo
ECHO is on.
C:\>echo
ECHO is on.

Unable to load a .cmd from a batch file

I recently played the game "Dictator" and wanted to recreate it in a batch script. I just began creating it when I ran into an issue. I have made programs which allow the user to save their progress and load it. I copied this code from a different program and put it in mine. Saving does seem to work but when i edit the save file (.cmd) and edit one of the saved variables, once I load that file nothing seems to happen. For example I saved one file called key.cmd and edited it to make the "pol" variable equivalent to 100. Once I loaded that file it did say loaded but the pol variable was still set to the default (10). I dont understand as I have made a folder called save and key.cmd file is inside it. I have used my save and load code multiple times in the past and have never had any issues. Please help! Here is my code:
:Setup
#echo off
title Dictator
color 0a
:Save Variables
set pol=10
set bui=10
set low=10
set cor=10
set peo=10
set cri=10
:Main
cls
echo 1 - Save
echo 2 - Load
echo 3 - Police - %pol%
echo 4 - Buissnes Men - %bui%
echo 5 - Lower Government - %low%
echo 6 - Corruption - %cor%
echo 7 - People - %peo%
echo 8 - Criminals - %cri%
choice /c 12345678 /n /m ">>> "
if %errorlevel% equ 1 goto Save
if %errorlevel% equ 2 goto load
:Save
cls
set /p pin="PIN: "
(
echo set pol=%pol%
echo set bui=%bui%
echo set low=%low%
echo set cor=%cor%
echo set peo=%peo%
echo set cri=%cri%
) >> saves\%pin%.cmd
echo SAVED
pause >nul
goto main
:Load
cls
set /p pin="PIN: "
if exist saves\%pin%.cmd (
call saves\%pin%.cmd
echo %pol%
echo LOADED
pause >nul
) else (
echo INCORRECT PIN
pause >nul
)
goto main
At :Load you need to allow spaces in the file name. Most users won't know of this weakness, but in order to allow spaces (and a few other 'special' characters) to be read in the user input without breaking it, you need to add setlocal enableextensions enabledelayedexpansion to the beginning of your batch file and make the set /p pin="PIN: " statement into set /p "pin=PIN: " and replace the %'s with !'s where it calls for %pin%.
In the end, your code should look like:
:Setup
setlocal enableextensions enabledelayedexpansion
#echo off
title Dictator
color 0a
:Save Variables
set pol=10
set bui=10
set low=10
set cor=10
set peo=10
set cri=10
:Main
cls
echo 1 - Save
echo 2 - Load
echo 3 - Police - %pol%
echo 4 - Buissnes Men - %bui%
echo 5 - Lower Government - %low%
echo 6 - Corruption - %cor%
echo 7 - People - %peo%
echo 8 - Criminals - %cri%
choice /c 12345678 /n /m ">>> "
if %errorlevel% equ 1 goto Save
if %errorlevel% equ 2 goto load
:Save
cls
set /p "pin=PIN: "
(
echo set pol=%pol%
echo set bui=%bui%
echo set low=%low%
echo set cor=%cor%
echo set peo=%peo%
echo set cri=%cri%
) >> saves\!pin!.cmd
echo SAVED
pause >nul
goto main
:Load
cls
set /p "pin=PIN: "
if exist saves\!pin!.cmd (
call saves\!pin!.cmd
echo %pol%
echo LOADED
pause >nul
) else (
echo INCORRECT PIN
pause >nul
)
goto main

BATCH / connect to FTP and put list of directories in an array

I have tried to do this in BAT however I think it cannot be done.
Asked already - Link: batch-ftp-list-directories-and-put-in-array
Objectives:
Connect to FTP
Go to a specific folder /var/www/xxx.com/image/go
List full path of directories and sub directories in that folder
put the list in an array
I am very new to VBS and it is really not my thing. However BATCH seems to be limited with FTP functions.
echo user xxx> c:\cmd.dat
echo 1234>> c:\cmd.dat
echo cd /var/www/xxx/html/TEST>> c:\cmd.dat
echo ls * c:\list.txt>> c:\cmd.dat
echo bye>> c:\cmd.dat
ftp -n -s:c:\cmd.dat xxx xxx xxx
REM This reads a file called list.txt and makes and array
REM - Removes blank lines
For /F "tokens=* delims=" %%A in (c:\list.txt) Do (
Echo %%A >> c:\list_clean.txt )
pause
set "file=c:\list.txt"
set /A i=0
for /F "usebackq delims=" %%a in ("%file%") do (
set /A i+=1
call echo %%i%%
call set array[%%i%%]=%%a
call set n=%%i%%
)
for /L %%i in (1,1,%n%) do call echo %%array[%%i]%%
pause
del c:\cmd.dat
del c:\wfslist.txt
I need to add these fictions:
This an example in batch just for testing that can list only files from a folder located on a public ftp server like ftp.microsoft.com in order to create a list.txt file to download it after, so give a try and tell us the result.
NB : Please don't forget to add the tag Batch
#echo off
mode con cols=85 lines=22 & Color A
::***********************************
Set FTPSERVER=ftp.microsoft.com
Title Lister les fichiers et les dossiers sur un serveur FTP (%FTPSERVER%) by Hackoo
Set USER=anonymous
Set Password=anonymous#anonymous.com
Set DossierFTP=/bussys/winsock/winsock2/
Set DownloadFolder=winsock2
::*******************************************************
Goto Lister
:Lister
> ft.do echo Open %FTPSERVER%
>> ft.do echo %USER%
>> ft.do echo %Password%
>> ft.do echo prompt n
>> ft.do echo bin
>> ft.do echo cd %DossierFTP%
>> ft.do echo ls -h TLIST.txt
>> ft.do echo bye
ftp -s:ft.do
del ft.do
CLS
Color 9B
echo Download la liste
pause
Goto Download
::*********************************************************
:Download
> ft.do echo Open %FTPSERVER%
>> ft.do echo %USER%
>> ft.do echo %Password%
>> ft.do echo prompt n
>> ft.do echo bin
>> ft.do echo cd %DossierFTP%
for /F %%f in (TLIST.txt) do ( >> ft.do echo get %%f)
>> ft.do echo bye
ftp -s:ft.do
del ft.do
CLS
Color 9A
pause
echo Deplacer la liste
Goto Deplacer
::*********************************************************
:Deplacer
Set Source=%~dp0
Set Destination=%Source%%DownloadFolder%
if not exist %DownloadFolder% MD %DownloadFolder%
for /F %%f in (TLIST.txt) do (move "%Source%%%f" "%Destination%")
pause
Here is my attempt at it.
This is a utility script that lists a remote directory (full paths, nothing else). It's designed to be reusable (i.e. it works with parameters) and to be called from other scripts.
Save as ftpls.bat.
#echo off
setlocal
rem USAGE: call with 4 parameters
rem ftpls hostname USER PASS path/on/remote/machine
set "script=%TEMP%\ftpscript.txt"
set "output=%TEMP%\ftplist.txt"
set "host=%1"
set "user=%2"
set "pass=%3"
set "dir=%4"
> "%script%" echo open %host%
>> "%script%" echo user %user%
>> "%script%" echo %pass%
>> "%script%" echo cd %dir%
>> "%script%" echo ls * %output%
>> "%script%" echo bye
ftp -n -s:"%script%"
rem modify "skip" and "tokens" to select the right lines and columns
for /f "skip=1 tokens=6,*" %%a in (%output%) do (
echo %dir%/%%b
)
del "%script%"
del "%output%"
The for loop cuts out the filename (which is the last part on every line of the listing). Choose the correct tokens parameter if 6,* doesn't work for your FTP server. 6,* means that column 6 will become %%a and the rest of the line (*) will become %%b. 6 should be the column in front of the filename.
Call it on its own like
ftpls xxx.xxx.xxx.xxx USER PASS /var/www/xxx/html/TEST
Use it in another batch file like
set cmd=ftpls xxx.xxx.xxx.xxx USER PASS /var/www/xxx/html/TEST
for /f "delims=" %%a in ('%cmd%') do (
echo %%a
)

Why is the variable disapearing?

I made a names.bat file
that will make a folder called profiles then add a batch in the profiles folder to view the profiles and I'm having a problem with the creation of program veiwer.bat
the names.bat contains this code:
#echo off
TITLE Profiles
SET /a YEAR=%DATE:~6,4%
if not exist Profiles mkdir Profiles
CD Profiles
ECHO #echo off>"Profile Viewer.bat"
ECHO :profile>>"Profile Viewer.bat"
ECHO title Profile Viewer>>"Profile Viewer.bat"
ECHO SET /P NAME=Search names:>>"Profile Viewer.bat"
ECHO call %NAME%.bat>>"Profile Viewer.bat"
ECHO goto profile>>"Profile Viewer.bat"
GOTO RES
:RES
SET /P NAME=State your name:
ECHO #echo off>%NAME%.txt
ECHO title %NAME%>>%NAME%.txt
ECHO echo Name:%NAME%>>%NAME%.txt
SET /P AGE=State your Age:
ECHO echo Age:%AGE%>>%NAME%.txt
SET /a YOB=%YEAR%-%AGE%
SET /P A=Where you born in %YOB%(Y/N)
IF %A%==Y goto YES
IF %A%==y goto YES
IF %A%==N goto NO
IF %A%==n goto NO
:NO
SET /P YOB=State your Year of birth:
ECHO echo Birth Date:%YOB%>>%NAME%.txt
goto CONTINUE
:YES
ECHO echo Birth Date:%YOB%>>%NAME%.txt
goto CONTINUE
:CONTINUE
ECHO pause>>%NAME%.txt
rename *.txt *.bat
start %NAME%.bat
%SystemRoot%\explorer.exe "C:\Users\%username%\Desktop\Profiles"
echo Here is your profile
pause
so I edit "Profile Viewer.bat" to see if it works it has this
#echo off
:profile
title Profile Viewer
SET /P NAME=Search names:
call .bat
goto profile
Where did the %NAME% variable go?
I want it to be in the profile viewer.bat
i know its an empty variable i want it to literally say in the code this;
#echo off
:profile
title Profile Viewer
SET /P NAME=Search names:
call %NAME%.bat
goto profile
I know I could make it myself but I want name.bat to do it for me
ECHO call %NAME%.bat>>"Profile Viewer.bat"
When this line is executed, the %NAME% variable is expanded. Since it hasn't been defined yet, it expands to nothing. Thus, we get the erroneous line in Profile Viewer.bat.
To prevent %NAME% from being expanded, we need to tell the command interpreter that we want an actual % instead of starting a variable. So, we need to escape the % like so:
ECHO call %%NAME%%.bat>>"Profile Viewer.bat"

Execute SQL from batch file

I'm new to batch files scripting.
All I want is creating a batch file that calling SQL file and store results in text file .
Can anyone help me, your help is highly appreciated,
This is the first time I need to create such files.
using a batch file:
save and run this:
#echo off
sqlplus -s -l user/pass#yourdb #yoursql.sql>your_log.log
p.s. be sure to have the last line of your sql script as exit; or the batch file will hang.
sqlcmd -S sqlservername -i yoursqlfile.sql -U username -P password -o outputfile.txt
I created a more advanced launcher, try it out (you can find the latest version at http://www.unix.com/windows-and-dos-issues-and-discussions/256021-windowss-batch-launcher-oracle-sql-linux-sh-scripts-available-here.html)
Here's the code anyway:
launcher.cmd
#ECHO OFF
rem Script Launcher by Fr3dY v1.4
rem ##############################
rem Version History:
rem 1.4 - Misc. fixes
rem 1.3 - Merged with 'server launcher', now accepts both SQL and SHELL SCRIPTS
rem 1.2 - Interactive prompt to show the file on screen
rem 1.1 - No need to add 'quit;' or 'exit;' in the .sql file anymore
rem Fixed sqlplus waiting for username/password if the first attempt was unsuccessful
rem Log file is generated automatically, including date and time in name
rem Misc. fixes
rem 1.0 - Initial Version
:MAIN
::Path of PLINK
set PLINK="C:\Program Files (x86)\PuTTY\plink.exe"
::List with TNS NAMES
set dbservers=launcher-databases.txt
::List with LINUX SERVERS
set linuxservers=launcher-servers.txt
set dt=%DATE:~6,4%_%DATE:~3,2%_%DATE:~0,2%__%TIME:~0,2%_%TIME:~3,2%_%TIME:~6,2%
set dt=%dt: =0%
echo Choose launcher mode:
echo 1) Database scripts (.sql files)
echo 2) Shell scripts (.sh files)
set /p launchermode="Insert value: "
echo.
if %launchermode%==1 (
set extension=sql
set servers=%dbservers%
set mode=DB
) else (
if %launchermode%==2 (
set extension=sh
set servers=%linuxservers%
set mode=OS
) else (echo "Incorrect value, exiting..." & goto :END)
)
if exist %servers% (
goto :LISTFILES
) else echo FILE %servers% NOT FOUND, ABORTING & goto :END
:LISTFILES
echo Listing *.%extension% files...
echo.
dir /b *.%extension%
echo.
set /p file=Name of the file to be launched (without extension)?
if exist %file%.%extension% (
goto :CONFIRMSHOW
) else echo FILE %file%.%extension% NOT FOUND, ABORTING & goto :END
:CONFIRMSHOW
echo.
set /p confirm=Show the script %file%.%extension% on screen now?
if %confirm%==y (
goto :SHOWSCRIPT
) else goto :CONFIRMEXEC
echo.
:SHOWSCRIPT
echo.
echo Content of %file%.%extension%
echo ######################
type %file%.%extension%
echo.
echo ######################
echo.
:CONFIRMEXEC
set /p confirm=Are you sure you want to execute this script?
if %confirm%==y (
set /p user=%mode% username?
goto :HInput
) else echo ABORTED & goto :END
echo.
echo Output saved to %file%_%dt%.log
echo.
Goto :END
:HInput
::Hidden.cmd
::Tom Lavedas, 02/05/2013, 02/20/2013
::Carlos, 02/22/2013
::https://groups.google.com/forum/#!topic/alt.msdos.batch.nt/f7mb_f99lYI
::Version 3.0
SetLocal DisableDelayedExpansion
echo.
Echo Enter password:
Set "Line="
Rem Save 0x08 character in BS variable
For /F %%# In (
'"Prompt;$H&For %%# in (1) Do Rem"'
) Do Set "BS=%%#"
:HILoop
Set "Key="
For /F "delims=" %%# In (
'Xcopy /L /W "%~f0" "%~f0" 2^>Nul'
) Do If Not Defined Key Set "Key=%%#"
Set "Key=%Key:~-1%"
SetLocal EnableDelayedExpansion
If Not Defined Key echo. & Goto :HIEnd
If %BS%==^%Key% (Set /P "=%BS% %BS%" <Nul
Set "Key="
If Defined Line Set "Line=!Line:~0,-1!"
) Else Set /P "=*" <Nul
If Not Defined Line (EndLocal &Set "Line=%Key%"
) Else For /F delims^=^ eol^= %%# In (
"!Line!") Do EndLocal &Set "Line=%%#%Key%"
Goto :HILoop
:HIEnd
if %launchermode%==1 (
goto :EXECDB
) else goto :EXECLINUX
:EXECDB
FOR /f %%A IN (%servers%) DO CALL ECHO DATABASE: %%A & ECHO DATABASE: %%A >> %file%_%dt%.log & sqlplus -S -L %user%/!Line!#%%A < %file%.%extension% >> %file%_%dt%.log
goto :END
:EXECLINUX
FOR /f %%A IN (%servers%) DO CALL ECHO SERVER: %%A & ECHO SERVER: %%A >> %file%_%dt%.log & echo y | %PLINK% %user%#%%A -pw !Line! "exit" & %PLINK% %user%#%%A -pw !Line! -batch -m %file%.%extension% >> %file%_%dt%.log & echo. >> %file%_%dt%.log
goto :END
:END
pause

Resources