log file of script batch - windows

#echo off
call :checkFTP1 %* > all_log_all_log_%date:~10,4%%date:~4,2%%date:~7,2%.log 2>&1
call :checkFTP2 %* >> all_log_all_log_%date:~10,4%%date:~4,2%%date:~7,2%.log 2>&1
call :checkFTP3 %* >> all_log_all_log_%date:~10,4%%date:~4,2%%date:~7,2%.log 2>&1
call :doCommands1 %* >> all_log_all_log_%date:~10,4%%date:~4,2%%date:~7,2%.log 2>&1
call :doCommands2 %* >> all_log_all_log_%date:~10,4%%date:~4,2%%date:~7,2%.log 2>&1
call :doCommands3 %* >> all_log_all_log_%date:~10,4%%date:~4,2%%date:~7,2%.log 2>&1
call :doCommands4 %* >> all_log_all_log_%date:~10,4%%date:~4,2%%date:~7,2%.log 2>&1
exit /b
:checkFTP1
#echo off
Setlocal
:: Is folder empty
set _TMP=
for /f "delims=" %%a in ('dir /b "C:\test\folder1"') do set _TMP="%%a"
IF {%_TMP%}=={} (
goto :Exit1
) ELSE (
goto :checkFTP2
)
Endlocal
:checkFTP2
#echo off
Setlocal
:: Is folder empty
set _TMP=
for /f "delims=" %%a in ('dir /b "C:\test\folder2"') do set _TMP="%%a"
IF {%_TMP%}=={} (
goto :Exit2
) ELSE (
goto :checkFTP3
)
Endlocal
:checkFTP3
#echo off
Setlocal
:: Is folder empty
set _TMP=
for /f "delims=" %%a in ('dir /b "C:\test\folder3"') do set _TMP="%%a"
IF {%_TMP%}=={} (
goto :Exit3
) ELSE (
goto :doCommands1
)
Endlocal
:doCommands1
call script1.bat
if %errorlevel% EQU 0 (goto :doCommands2 ) Else ( ECHO error on script 1 ,2,3,4)
exit
:doCommands2
call script2.bat
if %errorlevel% EQU 0 (goto :doCommands3 ) Else ( ECHO Script 1 Completed Successfully , ERRORS on 2,3,4)
exit
:doCommands3
call script3.bat
if %errorlevel% EQU 0 (goto :doCommands4) Else ( ECHO Script 2 Completed Successfully , ERRORS on 3,4)
exit
:doCommands4
call script4.bat
if %errorlevel% EQU 0 (goto :completed1) Else ( ECHO Script 3 Completed Successfully , ERRORS on 4)
exit
:Exit1
Echo Today Date %DATE% at %Time%
Echo ###################FTP-1 FILES MISSING #########################
Exit
:Exit2
Echo Today Date %DATE% at %Time%
Echo ###################FTP-2 FILES MISSING (#########################
Exit
:Exit3
Echo Today Date %DATE% at %Time%
Echo ###################FTP-3 FILES MISSING #########################
Exit
:completed1
Echo Today Date %DATE% at %Time%
Echo ###################all scripts Completed Successfully#########################
Exit
I have above batch file which calls multiple bat files. I have tested the script and it worked fine.
My only issue is that the log file generated contains all information, and it's a large file.
Is it possible to just log comments and echo, and exclude what executed in screen?
For example I don't want 1 file moved to be showing in log file.

I've taken your code and re-written it to use generic functions which deduplicates the code into an a more easily managed form and allows you to add or remove any steps to FTP and Script sections as needed by editing the list of variables.
I also only return output that does not include the "file(s) moved".
Alternatively if you really only want the status lines to print you could change this to just have those print to the log and not have all info print to the log (this is being done because you are putting the redirection to the log on the calls to other steps.
Also the way this was written before it was actually going through all the goto commands and not needing the calls to each at the top.
Here is the refactored code, I haven't tested it but it should be fine I have to step out for a few hours and you can ask any questions and I'll be happy to answer when I have time.
#( Setlocal EnableDelayedExpansion
echo off
SET "_FolderList="C:\test\folder1" "C:\test\folder1" "C:\test\folder1" "
SET "_ScriptList="c:\test\script1.bat" "E:\script2.bat" "C:\Path\To\script3.bat" "C:\Path\To\script4.bat" "
SET "_Scripts_Failed=1,2,3,4"
SET "_Scripts_Completed="
SET "_Continue=0"
CALL :GetDateTime
SET "MasterLog=all_log_all_log_!IsoDate!_!IsoTime!.log"
)
CALL :Main
( Endlocal
EXIT /B
)
:Main
SET /A "_Counter=0"
FOR %%_ IN (%_FolderList%) DO (
IF DEFINED _Continue (
SET /A "_Counter+=1"
CALL :CheckFTP_List %%~_
)
)>> "%MasterLog%"
SET /A "_Counter=0"
FOR %%_ IN (%_FolderList%) DO (
IF DEFINED _Continue (
SET /A "_Counter+=1"
CALL :DoCommands_List %%~_
)
)>> "%MasterLog%"
IF DEFINED _Continue (
Echo Today Date %DATE% at %Time%
Echo ###################all scripts Completed Successfully#########################
)
GOTO :EOF
:CheckFTP_List
REM Is folder empty
for /f "delims=" %%a in ('dir /b /A-D "%*') do (
set "_Continue=%%a" )
IF NOT Defined _Continue (
Echo.Today Date on %DATE% at %Time%
Echo.###################FTP-%_Counter% FILES MISSING #########################
)
GOTO :EOF
:DoCommands_List
call "*%" | FIND /I /V "file(s) moved" &REM only outputs lines that don't contain files moved.
if %errorlevel% NEQ 0 (
SET "_Continue="
SET "_Scripts_Completed=%_Scripts_Completed:,1=1%"
SET "_Scripts_Failed=!_Scripts_Failed:%_Scripts_Completed%=!"
Echo Today Date %DATE% at %Time%
ECHO. Error encountered on Script %Counter%! -- Completed Scripts: !_Scripts_Completed! -- Failed Scripts: !_Scripts_Failed!
) ELSE (
SET "_Scripts_Completed=%_Scripts_Completed:,1=1%,%Counter%"
)
GOTO :EOF
:GetDateTime
FOR /F "Tokens=1-7 delims=MTWFSmtwfsouehrandit:-\/. " %%A IN ("%DATE% %TIME: =0%") DO (
FOR /F "Tokens=2-4 Delims=(-)" %%a IN ('ECHO.^| DATE') DO (
SET "%%~a=%%~A"
SET "%%~b=%%~B"
SET "%%~c=%%~C"
SET "HH=%%~D"
SET "Mn=%%~E"
SET "SS=%%~F"
SET "Ms=%%~G"
)
)
SET "IsoTime=%HH%.%Mn%.%SS%.%Ms%"
SET "IsoDate=%yy%-%mm%-%dd%"
GOTO :EOF

Related

Looping through %1 as a directory in batch?

I've searched the web for answers but can't seem to find an answer. I want the user to provide a directory and to be able to loop through it. I'm able to loop through the current directory like so:
#Echo off
for /r %%f in (*.*) do (
echo %%f
)
But then when I try to do the same by looping through %1, I can't get the result I'm looking for. What am I doing wrong? Here's where I'm at in the batch file:
#Echo off
if exist %1 (
for /r %%f in (%1) do (
echo %%f
)
) else (
echo "That directory does not exist."
)
I've tried using /D but all that did was echo the directory I provided like this:
FileCount C:\Users\Me\Desktop
> C:\Users\Me\Desktop
Edit: My goal for this program is to eventually count the number of files within the given directory. I expect the directory to be provided as it's absolute path and I'll be executing this file through cmd. Here's an example of the input I'm expecting.
FileCount C:\Users\Me\Desktop
And the desired output would be something like:
> Hello world.txt
> Cat.png
> There are 2 files within this directory.
Side-note: I don't want to filter out the output of the dir command, I want to do this with a for loop.
Here's what you asked for, plus the optional recurs feature. Note that this will miss hidden files and directories.
#setlocal EnableExtensions
#prompt=$G
#set _Error_Success=0
#set _Error_PathNotFound=3
#if "%~1" equ "/?" goto :Usage
#if "%~1" equ "" goto :Usage
#if "%~1" equ "/r" (#set _recurs=/r & #set _root=%~2) else (#set _root=%~1)
#if not exist "%_root%" goto :Oops
#set count=0
#pushd "%_root%"
#for %_recurs% %%f in (*) do #call :Counter "%%f"
#popd
#echo There are %count% entries within this directory.
#exit /b %_Error_Success%
:Counter
#set /a count+=1
#echo %~1
#exit /b %_Error_Success%
:Oops
#echo "That directory does not exist."
#exit /b %_Error_PathNotFound%
:Usage
#echo Usage: FileCount [/r] path
And this uses the dir command, without resorting to invoking findstr:
#setlocal EnableExtensions
#prompt=$G
#set _Error_Success=0
#set _Error_PathNotFound=3
#set _Error_InvalidParameter=87
#set _attributes=
#set _recurs=
#set _count=0
#if "%~1" equ "/?" #goto :Usage & #exit
#if "%~1" equ "" goto :Usage
#set _root=%~1
#if not exist "%_root%" goto :Oops
#shift
#pushd "%_root%"
for /f %%G in ('dir /B /A-d %1 %2 %_root%') do #call :Counter "%%G"
#popd
#echo There are %_count% entries within this directory.
#exit /b %_Error_Success%
:Counter
#set /a _count+=1
#echo %~1
#exit /b %_Error_Success%
:HandleOptions
:Oops
#echo "That directory does not exist."
#exit /b %_Error_PathNotFound%
:Usage
#echo Usage: FileCount [/A<Attributes>] [/S] rootPath
#echo Where <Attributes> corresponds to 'dir /A' optiions (see 'help dir')
#echo and /S will cause recursion into subdirectories of rootPath.
This example, uses a for loop, and does not use the dir command, as per your inexplicable request, but it does use xcopy to list and count the files within it instead:
#For %%G In ("%~1") Do #If "%%~aG" Lss "d" (If "%%~aG" GEq "-" (
Echo Error: File argument given, expected a directory.
%SystemRoot%\System32\timeout.exe /T 3 /NoBreak 1> NUL
Exit /B 1) Else (
Echo Error: Invalid argument, directory path expected.
%SystemRoot%\System32\timeout.exe /T 3 /NoBreak 1> NUL
Exit /B 1)
) Else Echo File content of "%~1":& For /F Delims^= %%H In (
'%SystemRoot%\System32\xcopy.exe "%~1" : /HIL') Do #Echo %%~nxH
#%SystemRoot%\System32\timeout.exe /T -1 & Exit /B 0
If you want it to recurse the input directory, then I'd suggest this very small modification:
#For %%G In ("%~1") Do #If "%%~aG" Lss "d" (If "%%~aG" GEq "-" (
Echo Error: File argument given, expected a directory.
%SystemRoot%\System32\timeout.exe /T 3 /NoBreak 1> NUL
Exit /B 1) Else (
Echo Error: Invalid argument, directory path expected.
%SystemRoot%\System32\timeout.exe /T 3 /NoBreak 1> NUL
Exit /B 1)
) Else Echo File content of "%~1":& For /F Delims^= %%H In (
'%SystemRoot%\System32\xcopy.exe "%~1" : /HILS') Do #Echo %%H
#%SystemRoot%\System32\timeout.exe /T -1 & Exit /B 0
Or with a little more work, outputting relative paths instead:
#For %%G In ("%~1") Do #If "%%~aG" Lss "d" (If "%%~aG" GEq "-" (
Echo Error: File argument given, expected a directory.
%SystemRoot%\System32\timeout.exe /T 3 /NoBreak 1> NUL
Exit /B 1) Else (
Echo Error: Invalid argument, directory path expected.
%SystemRoot%\System32\timeout.exe /T 3 /NoBreak 1> NUL
Exit /B 1)
) Else Echo File content of "%~1":& For /F Delims^= %%H In (
'%SystemRoot%\System32\xcopy.exe "%~1" : /HILS') Do #(
Set "}=%%H" & SetLocal EnableDelayedExpansion
For %%I In ("!}:*%~1=.!") Do #EndLocal & Echo %%~I)
#%SystemRoot%\System32\timeout.exe /T -1 & Exit /B 0
setlocal enabledelayedexpansion
rem Put it into a folder that is in ENV PATH variable.
rem So, you can call it from wherever you want.
if not "%~1"=="" (
if exist "%~dpn1" (
set /a "filecount=0"
for /f "tokens=* delims=" %%f in ('dir /b "%~dpn1"') do (
echo %%f
set /a "filecount+=1"
)
echo:
echo In "%~dpn1" found !filecount! file^(s^).
) else (
echo:
echo [ERROR] Invalid input. Please specify:
echo 1. A full path.
echo 2. A folder in cwd.
echo:
echo If input is empty, current working directory (cwd)
echo will be considered.
exit /b 1
)
) else (
set /a "filecount=0"
for /f "tokens=* delims=" %%f in ('dir /b "%~dp0"') do (
echo %%f
set /a "filecount+=1"
)
echo:
echo In "%~dp0" found !filecount! file^(s^).
)
endlocal
If you want it recursive call it as progname.bat /r [foldname]:
setlocal enabledelayedexpansion
rem Put it into a folder that is in ENV PATH variable.
rem So, you can call it from wherever you want.
if "%1"=="/r" shift
if not "%1"=="" (
if exist "%~dpn1" (
set /a "filecount=0"
set /a "totalcount=0"
set "oldroot=%~dpn1"
for /f "tokens=* delims=" %%f in ('dir /b /s "!cd!"') do (
if not "%%~dpf"=="!oldroot!" (
echo:
echo In "!oldroot!" found !filecount! file^(s^).
echo:
set /a "filecount=0"
set "oldroot=%%~dpf"
)
echo --- %%f
set /a "filecount+=1"
set /a "totalcount+=1"
)
echo:
echo In "%~dpn1" found !totalcount! file^(s^).
) else (
echo:
echo [ERROR] Invalid input. Please specify:
echo 1. A full path.
echo 2. A folder in cwd.
echo:
echo If input is empty, current working directory (cwd)
echo will be considered.
exit /b 1
)
) else (
set /a "filecount=0"
set /a "totalcount=0"
set "oldroot=!cd!"
for /f "tokens=* delims=" %%f in ('dir /b /s "!cd!"') do (
if not "%%~dpf"=="!oldroot!" (
echo:
echo In "!oldroot!" found !filecount! file^(s^).
echo:
set /a "filecount=0"
set "oldroot=%%~dpf"
)
echo --- %%f
set /a "filecount+=1"
set /a "totalcount+=1"
)
echo:
echo In "%~dp0" found !totalcount! file^(s^).
)
endlocal
Try it and see if it is what you expected.
Thank you for the help everybody, I've found the solution to the problem and it was very simple. All I needed to do was loop through %1\*.* instead of %1 itself.
#Echo off
if exist %1 (
for %%f in (%1\*.*) do (
echo %%f
)
) else (
echo "That directory does not exist."
)

Batch Script Iterative form

I have a batch script which :
check the files in a directory and check if it exists in another
directory and it should not exists there
count each file with a specific format, there should be just one of each
if both of the above statement is true then generate a success file.
Below is my code which is working fine:
SET /A file1=file2=Val=0
SET /A FileE=1
set /a flagname=1
for %%A in (*ABC*.txt) do set /a file1+=1
for %%A in (*XYZ*.txt) do set /a file2+=1
for %%i in ("*") do if exist "Processed\%%~nxi" SET /A FileE=0
SET /A Val=%file1%*%file2%*%FileE%
if %Val% EQU 1 (
echo SUCESS>Sucess.txt
SET Flag=Sucess
echo %Flag%) else (
if %file1% EQU 0 ( echo Missing ABC.txt files >> Error.txt)
if %file1% GTR 1 ( echo More than 1 ABC.txt files >> Error.txt)
if %file2% EQU 0 ( echo Missing XYZ.txt files >> Error.txt)
if %file2% GTR 1 ( echo More than 1 XYZ.txt files >> Error.txt)
(for %%i in ("*") do if exist "Processed\%%~nxi" echo(File Exists in
Processed
Folder %%~i)>>Error.txt
SET Flag=FAILURE
echo %Flag%)
My problem is how to transform above code to iterate over a list of number of files like 100 ? Below is the code which I tried :
#echo off
setlocal enable delayed expansion
Set Filen[0]=ABC*.txt
Set Filen[1]=XYZ*.txt
SET /A Val=1
SET /A File1=0
FOR /l %%G in (0,1,1) Do (
echo !Filen[%%G]! hi
set File1=0
echo %file1% Count
for %%A in (!Filen[%%G]! ) do (
set File1=!File1!+1
echo %%A %file1%)
)
Put your search words in a string and iterate over it:
Set "Search=ABC DEF XYZ"
For %%A in (%Search%) do (
Or in a file and read one by one
For /f "usebackq" %%A in ("Search.txt") Do (
With this file Search.txt
ABC
DEF
XYZ
In the environment
> tree a:\ /F
Auflistung der Ordnerpfade für Volume RamDisk
A:\
└───Test
│ ABC_123.txt
│ DEF_456.txt
│ Search.txt
│
└───Processed
The following batch
:: Q:\Test\2018\07\01\SO_51120147.cmd
#Echo off & Setlocal EnableDelayedExpansion
Set "BaseDir=A:\Test"
Set Err=^>^> "Error.txt" Call Echo=[%%date%% %%time%%]
PushD "%BaseDir%" || (%Err% can't locate %BaseDir% & Pause &Goto :Eof)
%Err% Job %~f0 started by %USERNAME%
Set " Flag=Sucess"
:: Uncomment for string variant
:: Set "Search=ABC DEF XYZ"
:: For %%A in (%Search%) do (
:: use the file variant
For /f "usebackq" %%A in ("Search.txt") Do (
Set Cnt=0
For %%B in (%%A*.txt) Do Set /A Cnt=1
if !Cnt! NEQ 1 (
%Err% Count of %%A*.txt file=[!Cnt!]
SET Flag=FAILURE
)
)
For %%A in (*) do if exist "Processed\%%~nxA" (
%Err% File %%A does exist in Processed Folder
Set Flag=FAILURE
)
%Err% Job %~f0 terminated with %Flag%
Yields this output:
> type A:\Test\Error.txt
[2018-07-01 13:10:34,43] Job Q:\Test\2018\07\01\SO_51120147.cmd started by LotPings
[2018-07-01 13:10:34,45] Count of XYZ*.txt file=[0]
[2018-07-01 13:10:34,47] Job Q:\Test\2018\07\01\SO_51120147.cmd terminated with FAILURE
EDIT: Explanation of the somehow tricky line:
Set Err=^>^> "Error.txt" Call Echo=[%%date%% %%time%%]
To not have to pre/append every line which should go to the error log with
the redirection and a (fixed) file name I put these together with the
echo command and a [date time] stamp into a variable.
To avoid immediate interpretation when setting the variable, the '>' have to
be esaped with a caret, and to delay the expansion of the %-signs these have
to be doubled. (otherwise each log entry had the same date time)
To force expansion of date time when echoing the (pseudo) call is neccessary.

How to use % or ! in batch script

I write script like this:
#ECHO OFF
setlocal EnableDelayedExpansion
set "remove=ABC"
echo. %remove%
Set FILENAME="456_789_ABC00011092_789_EFGHIK_56893.mpg"
for %%a in (%FILENAME:_=" "%) do (
set TEN=%%a
echo. %AB%
set "remove_1=ABC"
echo. %remove_1%
Set _TEN=!TEN:%remove%=!
echo. %_TEN%
Set i=0
IF !_TEN! NEQ !TEN! (
set /A i+=1
set "String[!i!]=%%~a"
)
)
pause
exit
Why echo. %AB% echo. %remove_1% result is
I replace % by !. It's work fine but command Set _TEN=!TEN:!remove_1!=! not run
Edit - (from the additional question currently posted as an answer)
When I use FindStr command like this:
for %%a in (%FILENAME:_=" "%) do (
echo %%a | findstr /I /R /C:"ABC" >nul
ECHO %errorlevel%
if "%errorlevel%" equ "0" (
set /A i+=1
set "String[!i!]=%%~a"
)
)
Why errorlevel always = 0
%AB% has not been defined within your posted script, so as it has no value will not be echoed, you will just get an empty line due to the . after echo. Because remove_1 is being set within the loop, (code block), you should be using the delayed expansion syntax, Echo !remove_1!. It is the same for echo. %_TEN%, i.e. Echo !_TEN!, and would have been Echo !AB! had it previously been defined. In order to get the double expansion needed to Set your _TEN variable, you could use a pseudo Call:
#Echo Off
SetLocal EnableDelayedExpansion
Set "FILENAME=456_789_ABC00011092_789_EFGHIK_56893.mpg"
For %%A In ("%FILENAME:_=" "%") Do (
Set "TEN=%%A"
Echo. !AB!
Set "remove_1=ABC"
Echo !remove_1!
Call Set "_TEN=!TEN:%%remove_1%%=!"
Echo !_TEN!
Set "i=0"
If "!_TEN!" NEq "!TEN!" (
Set /A i+=1
Set "String[!i!]=%%~A"
)
)
Pause
Exit /B
In your second related question, initially posted as an answer and now added as an edit to your original question; because the error level is being set within the loop, (code block), you should be using the delayed expansion syntax, !errorlevel!
#Echo Off
SetLocal EnableDelayedExpansion
Set "FILENAME=456_789_ABC00011092_789_EFGHIK_56893.mpg"
For %%A In ("%FILENAME:_=" "%") Do (
Echo %%A | FindStr /IRC:"ABC" >Nul
Echo !errorlevel!
If "!errorlevel!"=="0" (
Set /A i+=1
Set "String[!i!]=%%~A"
)
)
Set String[
Pause
Exit /B
Or if you don't need to Echo each error level to the screen, you can use a conditional statement &&:
#Echo Off
SetLocal EnableDelayedExpansion
Set "FILENAME=456_789_ABC00011092_789_EFGHIK_56893.mpg"
For %%A In ("%FILENAME:_=" "%") Do (
Echo %%A | FindStr /IRC:"ABC" >Nul && (
Set /A i+=1
Set "String[!i!]=%%~A"
)
)
Set String[
Pause
Exit /B

Variable not setting in nested for loop - Windows batch

Below is a simplified snippet of my code:
#echo off
setlocal enabledelayedexpansion
for /f %%f in ('%pomFiles%') do (
findstr "var" %%f > nul
if errorlevel 0 if not errorlevel 1 (
cd "%%~dpf"
for /f "usebackq" %%i in ('%%~dpftree.out') do ( set size=%%~zi && echo !size!)
if !size! gtr 0 (
//do stuff
)
)
)
The !size! variable does not seem to be being set. If I do echo !size!, it prints !size!. How can I make sure !size! evaluates?
It's because you are using for /f, which is to read the contents of a file, hence the problem is you are trying to get the size of the variable %%i, which is set as the first line in the file.
You just need a regular for loop
for %%i in ("%%~dpftree.out") do ( set size=%%~zi && echo !size!)
if !size! gtr 0 (
//do stuff
)

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