I have a config file named config.conf with the content of:
[Config]
password=1234
usertries=0
allowterminate=0
I want to just edit the usertries value to 5, are there any batch script code that can help me do that
Thanks
This gets a little messy, but should work:
#echo off
set ConfigFile=config.conf
setlocal enabledelayedexpansion
(for /f "delims=" %%L in (%ConfigFile%) do (
set "LINE=%%L"
if "!LINE:~0,10!"=="usertries=" (echo usertries=5) else (echo !LINE!)
)) > %ConfigFile%.new
move /y %ConfigFile%.new %ConfigFile% > nul
Basically we're writing each line to a new file, unless it starts with usertries=. In which case we just insert our replacement line. Afterwards we move the new file on top of the old one to replace it.
#ECHO OFF
setlocal
::
:: set %1 to value %2 in config.conf
::
SET target=%2&IF NOT DEFINED target ECHO syntax is %0 keyname newvalue&GOTO :EOF
SET target=Config.conf
MOVE /y %target% %target%.old >NUL
FOR /f "tokens=1*delims==" %%i IN (%target%.old) DO (
IF /i %%i==%1 (ECHO %%i=%2) ELSE (
IF "%%j"=="" (ECHO %%i) ELSE (ECHO %%i=%%j)
)
) >>%target%
With this version, you can change an existing key to a value using
thisbatchname existingkey newvalue
I know I'm a little late to the party, but I decided to write a general purpose ini file utility batch script to address this question.
The script will let you retrieve or modify values in an ini-style file. Its searches are case-insensitive, and it preserves blank lines in the ini file. In essence, it allows you to interact with an ini file as a sort of very rudimentary database.
:: --------------------
:: ini.bat
:: ini.bat /? for usage
:: --------------------
#echo off
setlocal enabledelayedexpansion
goto begin
:usage
echo Usage: %~nx0 /i item [/v value] [/s section] inifile
echo;
echo Take the following ini file for example:
echo;
echo [Config]
echo password=1234
echo usertries=0
echo allowterminate=0
echo;
echo To read the "password" value:
echo %~nx0 /s Config /i password inifile
echo;
echo To change the "usertries" value to 5:
echo %~nx0 /s Config /i usertries /v 5 inifile
echo;
echo In the above examples, "/s Config" is optional, but will allow the selection of
echo a specific item where the ini file contains similar items in multiple sections.
goto :EOF
:begin
if "%~1"=="" goto usage
for %%I in (item value section found) do set %%I=
for %%I in (%*) do (
if defined next (
if !next!==/i set item=%%I
if !next!==/v set value=%%I
if !next!==/s set section=%%I
set next=
) else (
for %%x in (/i /v /s) do if "%%~I"=="%%x" set "next=%%~I"
if not defined next (
set "arg=%%~I"
if "!arg:~0,1!"=="/" (
1>&2 echo Error: Unrecognized option "%%~I"
1>&2 echo;
1>&2 call :usage
exit /b 1
) else set "inifile=%%~I"
)
)
)
for %%I in (item inifile) do if not defined %%I goto usage
if not exist "%inifile%" (
1>&2 echo Error: %inifile% not found.
exit /b 1
)
if not defined section (
if not defined value (
for /f "usebackq tokens=2 delims==" %%I in (`findstr /i "^%item%\=" "%inifile%"`) do (
echo(%%I
)
) else (
for /f "usebackq delims=" %%I in (`findstr /n "^" "%inifile%"`) do (
set "line=%%I" && set "line=!line:*:=!"
echo(!line! | findstr /i "^%item%\=" >NUL && (
1>>"%inifile%.1" echo(%item%=%value%
echo(%value%
) || 1>>"%inifile%.1" echo(!line!
)
)
) else (
for /f "usebackq delims=" %%I in (`findstr /n "^" "%inifile%"`) do (
set "line=%%I" && set "line=!line:*:=!"
if defined found (
if defined value (
echo(!line! | findstr /i "^%item%\=" >NUL && (
1>>"%inifile%.1" echo(%item%=%value%
echo(%value%
set found=
) || 1>>"%inifile%.1" echo(!line!
) else echo(!line! | findstr /i "^%item%\=" >NUL && (
for /f "tokens=2 delims==" %%x in ("!line!") do (
echo(%%x
exit /b 0
)
)
) else (
if defined value (1>>"%inifile%.1" echo(!line!)
echo(!line! | find /i "[%section%]" >NUL && set found=1
)
)
)
if exist "%inifile%.1" move /y "%inifile%.1" "%inifile%">NUL
Example:
C:\Users\me\Desktop>type config.conf
[Config]
password=1234
usertries=0
allowterminate=0
[Other]
password=foo
C:\Users\me\Desktop>ini /i usertries /v 5 config.conf
5
C:\Users\me\Desktop>ini /i usertries config.conf
5
C:\Users\me\Desktop>ini /s config /i password /v bar config.conf
bar
C:\Users\me\Desktop>ini /s other /i password /v baz config.conf
baz
C:\Users\me\Desktop>type config.conf
[Config]
password=bar
usertries=5
allowterminate=0
[Other]
password=baz
Related
I have and batch file script shows me output of adb utility.
FOR /F "skip=1 delims=~" %%x IN ('adb devices -l') DO echo %%x
2 output lines
0123456789ABCDEF device product:java_joyplus_qb7 model:TM702B4_3G device:java_joyplus_qb7 transport_id:12
F9NPFP084096 device product:WW_P023 model:P023 device:P023_1 transport_id:11
I want parse non-space substring after "transport_id:".
For the first line i want get 12, for second 11. How can I do that?
If I understand correctly what you've asked, the following batch-file should output the information you require.
#Echo Off
For /F "Skip=1 Tokens=1*" %%G In ('adb.exe devices -l') Do (Set "DI=%%H"
SetLocal EnableDelayedExpansion
For /F %%I In ("!DI:*transport_id:=!") Do EndLocal & Echo %%I)
Pause
And, if you wanted to do that from cmd:
For /F "Skip=1Tokens=1*" %G In ('adb.exe devices -l')Do #Set "DI=%H"&For /F %I In ('Cmd /D/V/C Echo "!DI:*transport_id:=!"')Do #Echo %~I
You may easily extract all variables no matter their positions and show what you want:
#echo off
setlocal EnableDelayedExpansion
FOR /F "skip=1 delims=" %%a IN ('adb devices -l') DO (
for %%b in (%%a) do for /F "tokens=1,2 delims=:" %%x in ("%%b") do set "%%x=%%y"
echo Product=!product!
echo Transport=!transport_id!
)
For example:
Product=java_joyplus_qb7
Transport=12
Product=WW_P023
Transport=11
SETLOCAL EnableDelayedExpansion
REM …
FOR /F "skip=1 delims=~" %%x IN ('adb devices -l') DO (
set "_ID="&set "_transport_id="
call :parse "" "%%~x"
echo transport_id !_transport_id! (!_ID!^)
)
REM … remaining part of your script here …
goto :eof
:parse
if "%~2"=="" goto :eof
for /F "tokens=1,*" %%G in ("%~2") do (
if "%~1"=="" (
set "_ID=%%~G"
call :parse "%%~G" "%%~H"
) else (
for /F "tokens=1,2 delims=: " %%g in ("%~2") do (
if /I "%%~g"=="transport_id" (
set "_transport_id=%%~h"
) else (
call :parse "%%~G" "%%~H"
)
)
)
)
goto :eof
Tested using the following script with hard-coded hypothetical adb output (not having adb installed):
#ECHO OFF
SETLOCAL EnableExtensions EnableDelayedExpansion
(
REM last token
set "_ID="&set "_transport_id="
call :parse "" "0123456789ABCDEF device product:java_joyplus_qb7 model:TM702B4_3G device:java_joyplus_qb7 transport_id:12"
echo transport_id !_transport_id! (!_ID!^)
REM penult token
set "_ID="&set "_transport_id="
call :parse "" "F9NPFP084096 device product:WW_P023 model:P023 transport_id:11 device:P023_1"
echo transport_id !_transport_id! (!_ID!^)
REM elsewhere token
set "_ID="&set "_transport_id="
call :parse "" "abc123def567 transport_id:10 device product:XX_P023 model:P023 device:P023_2"
echo transport_id !_transport_id! (!_ID!^)
REM nowhere token
set "_ID="&set "_transport_id="
call :parse "" "noTransportId sport_id:10 device product:XX_P023 model:P023 device:P023_2"
echo transport_id !_transport_id! (!_ID!^)
)
goto :eof
:parse
if "%~2"=="" goto :eof
for /F "tokens=1,*" %%G in ("%~2") do (
if "%~1"=="" (
set "_ID=%%~G"
call :parse "%%~G" "%%~H"
) else (
for /F "tokens=1,2 delims=: " %%g in ("%~2") do (
if /I "%%~g"=="transport_id" (
set "_transport_id=%%~h"
) else (
call :parse "%%~G" "%%~H"
)
)
)
)
goto :eof
Output: D:\bat\SO\61588773.bat
transport_id 12 (0123456789ABCDEF)
transport_id 11 (F9NPFP084096)
transport_id 10 (abc123def567)
transport_id (noTransportId)
I am trying call Run_sqlldr_process.bat from within batch file run_process.bat.
This is how the call is invoked in run_process.bat:
call %FWK_DIR%\run_sqlldr_process.bat !ldr!.ctl !log!.log !bad!.bad !data!.dat %BATCHUSER% %BATCHPWD% %ORACLE_SID%
echo %ERRORLEVEL%
echo %RC%
There is a failure inside run_sqlldr_process.bat after which run_sqlldr_process.bat exits with return code 1 as assigned to environment variable RC. But run_process.bat does not capture the error return code from run_sqlldr_process.bat. Both echo statements output 0 and run_process.bat completes successfully.
File Run_sqlldr_process.bat:
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
REM ##########################################################################
REM # Program Name: run_process.bat
REM # Description:
REM # This script is a part of the batch framework. It contains the common
REM # functions for running batch jobs.
REM #
REM # Usage:
REM # run_process.bat <processname>
REM #
REM # History:
REM # Date Who Description
REM # ---------- ------------------ ----------------------------------------
REM # 01-Jun-2014 Ilavarasan Sekar Created
REM ##########################################################################
SET NLS_DATE_FORMAT=RRRR-MM-DD-HH24:MI
FOR /F "TOKENS=1* DELIMS= " %%A IN ('DATE/T') DO SET CDATE=%%B
FOR /F "TOKENS=1,2 eol=/ DELIMS=/ " %%A IN ('DATE/T') DO SET mm=%%B
FOR /F "TOKENS=1,2 DELIMS=/ eol=/" %%A IN ('echo %CDATE%') DO SET dd=%%B
FOR /F "TOKENS=2,3 DELIMS=/ " %%A IN ('echo %CDATE%') DO SET yyyy=%%B
SET todaysdate=%yyyy%%mm%%dd%
SET WORKFLOWLOGMONTH=%mm%%yyyy%
SET DATETIMESTAMP=%todaysdate%
for /f "tokens=1-3 delims=:." %%a in ("%time%") do SET timestamp=%%a%%b%%c
for /f "tokens=1-3 delims= " %%a in ("%timestamp%") do SET trimmed_timestamp=%%a%%b%%c
SET TIMESTAMP=%DATETIMESTAMP%_%trimmed_timestamp%
rem --- Initialize --------------------------------------------
rem Install error handler.
rem basename=$(basename $0)
SET dirname=D:/JDA/Batch_FWK/fwk
echo off
SET oldpwd=%~dp0
SET suffix=%date%
SET process_id=%1
SET job_id=
SET process_type=
SET preprocess_type=
SET postprocess_type=
SET job_status=CREATED
SET job_status_message=
SET log_level=INFO
SET pre=
if "%1" == "" goto show_usage
rem --- Include environment variables -------------------------
SET batchenv=%dirname%\batchenv.bat
if exist %batchenv% (
call %batchenv%
) else (
echo "Environment file missing.\n"
goto end
)
set LOG_FILE=%LOG_DIR%\%process_id%_%TIMESTAMP%.log
set TMP_FILE=%TMP_DIR%\%process_id%_%TIMESTAMP%.tmp
rem --- Start job ---------------------------------------------
call:start_job
IF "%preprocess_type%"=="SQL" (
call:log_info "SQL."
SET pre=%SQL_DIR%\%process_id%_pre
call %FWK_DIR%\run_sql_process.bat !pre!.sql
) ELSE IF "%preprocess_type%"=="CMD" (
call:log_info "CMD."
SET pre=%CMD_DIR%\%process_id%_pre
call %FWK_DIR%\run_cmd_process.bat !pre!.bat
) ELSE (
call:log_info "No postprocess_type."
)
IF %ERRORLEVEL% GTR 0 (
set job_status=FAILED
call:end_job
call:cleanup
set RC=1
goto end
)
rem --- Execute main process ----------------------------------
IF "%process_type%"=="SQL" (
call:log_info "SQL."
SET main=%SQL_DIR%\%process_id%
call %FWK_DIR%\run_sql_process.bat !main!.sql
) ELSE IF "%process_type%"=="CMD" (
call:log_info "CMD."
SET main=%CMD_DIR%\%process_id%
call %FWK_DIR%\run_cmd_process.bat !main!.bat
) ELSE IF "%process_type%"=="SRE" (
call:log_info "SRE."
SET main=%XML_DIR%\%process_id%
call %FWK_DIR%\run_sre_process.bat !main!.xml
) ELSE IF "%process_type%"=="PERL" (
call:log_info "PERL."
SET main=%PRL_DIR%\%process_id%
call %FWK_DIR%\run_perl_process.bat !main!.pl
) ELSE IF "%process_type%"=="SQLLDR" (
call:log_info "SQLLDR."
SET ldr=%CTL_DIR%\%process_id%
SET log=%LOG_DIR%\%process_id%_%TIMESTAMP%
SET bad=%BAD_DIR%\%process_id%_%TIMESTAMP%
SET data=%IN_DIR%\%process_id%
call %FWK_DIR%\run_sqlldr_process.bat !ldr!.ctl !log!.log !bad!.bad !data!.dat %BATCHUSER% %BATCHPWD% %ORACLE_SID%
echo Return Code ERRORLEVEL inside run_process is %ERRORLEVEL%
echo Return Code inside run_process.bat is %RC%
IF %RC% GTR 0 (
ECHO INSIDE FAILED BLOCK
set job_status=FAILED
set RC=1
call:end_job
call:cleanup
goto end
)
) ELSE (
call:log_info "Unsupported process type %process_type%."
set job_status=FAILED
set RC=1
call:end_job
call:cleanup
goto end
)
IF %ERRORLEVEL% GTR 0 (
set job_status=FAILED
set RC=1
call:end_job
call:cleanup
goto end
)
IF "%postprocess_type%"=="SQL" (
call:log_info "SQL."
SET post=%SQL_DIR%\%process_id%_post
call %FWK_DIR%\run_sql_process.bat !post!.sql
) ELSE IF "%postprocess_type%"=="CMD" (
call:log_info "CMD."
SET post=%CMD_DIR%\%process_id%_post
call %FWK_DIR%\run_cmd_process.bat !post!.bat
) ELSE (
call:log_info "No postprocess_type."
)
IF %ERRORLEVEL% GTR 0 (
set job_status=FAILED
set RC=1
call:end_job
call:cleanup
goto end
)
rem --- End job -------------------------------------------------
call:end_job
call:cleanup
goto end
:show_usage
#echo Usage: Usage is run_process.bat ProcessName
goto end
rem --- Function definitions ----------------------------------
rem -----------------------------------------------------------
rem Function Name: start_job
rem Description :
rem Retrieve process properties and start a new job for
rem the specified process.
rem Input : process_id - Process to start.
rem Output : job_id - New job id set.
rem -----------------------------------------------------------
:start_job
call:log_info "--- Starting -----------------------------------"
call:log_info "Process : %process_id%"
call:log_info "Environment : %ENV_NAME%"
call:log_info "Batch root : %ENV_HOME%"
call:log_info "Log file : %LOG_FILE%"
call:log_info "Tmp file : %TMP_FILE%"
sqlplus -s /nolog #%dirname%/sqlFile.sql '%BATCHUSER%' '%BATCHPWD%' '%ORACLE_SID%' '%process_id%'
for /f "tokens=2 delims=|" %%a in ('findstr /R /I "^JOB_ID" %tmp_file%') do set JOB_ID=%%a
for /f "tokens=1,*" %%a in ("%JOB_ID%") do set JOB_ID=%%a
for /f "tokens=2 delims=|" %%a in ('findstr /R /I "^PROCESS_TYPE" %tmp_file%') do set PROCESS_TYPE=%%a
for /f "tokens=1,*" %%a in ("%PROCESS_TYPE%") do set PROCESS_TYPE=%%a
for /f "tokens=2 delims=|" %%a in ('findstr /R /I "^PREPROCESS_TYPE" %tmp_file%') do set PREPROCESS_TYPE=%%a
for /f "tokens=1,*" %%a in ("%PREPROCESS_TYPE%") do set PREPROCESS_TYPE=%%a
for /f "tokens=2 delims=|" %%a in ('findstr /R /I "^POSTPROCESS_TYPE" %tmp_file%') do set POSTPROCESS_TYPE=%%a
for /f "tokens=1,*" %%a in ("%POSTPROCESS_TYPE%") do set POSTPROCESS_TYPE=%%a
for /f "tokens=2 delims=|" %%a in ('findstr /R /I "^LOG_LEVEL" %tmp_file%') do set LOG_LEVEL=%%a
for /f "tokens=1,*" %%a in ("%LOG_LEVEL%") do set LOG_LEVEL=%%a
set job_status=RUNNING
del /q %TMP_FILE%
IF "%job_id%" == "" (
set job_status=FAILED
call:cleanup
set RC=1
goto end
)
call:log_info "Job id : %job_id%"
call:log_info "Process type : %process_type%"
call:log_info "Pre-process type : %preprocess_type%"
call:log_info "Post-process type: %postprocess_type%"
call:log_info "--- Running ------------------------------------"
goto:eof
rem -----------------------------------------------------------
rem Function Name: end_job
rem Description :
rem End the current job.
rem Input : job_id
rem Output : N/A
rem -----------------------------------------------------------
:end_job
call:log_info "--- Ending -------------------------------------"
sqlplus -s /nolog #%dirname%/sqlFileEnd.sql '%BATCHUSER%' '%BATCHPWD%' '%ORACLE_SID%' '%job_id%' '%job_status%'
for /f "tokens=2 delims=|" %%a in ('findstr /R /I "^JOB_STATUS|" %tmp_file%') do set JOB_STATUS=%%a
for /f "tokens=1,*" %%a in ("%JOB_STATUS%") do set JOB_STATUS=%%a
for /f "tokens=2 delims=|" %%a in ('findstr /R /I "^JOB_STATUS_MESSAGE" %tmp_file%') do set JOB_STATUS_MESSAGE=%%a
for /f "tokens=1,*" %%a in ("%JOB_STATUS_MESSAGE%") do set JOB_STATUS_MESSAGE=%%a
for /f "tokens=2 delims=|" %%a in ('findstr /R /I "^JOB_VOLUME" %tmp_file%') do set JOB_VOLUME=%%a
for /f "tokens=1,*" %%a in ("%JOB_VOLUME%") do set JOB_VOLUME=%%a
for /f "tokens=2 delims=|" %%a in ('findstr /R /I "^JOB_ERROR_COUNT" %tmp_file%') do set JOB_ERROR_COUNT=%%a
for /f "tokens=1,*" %%a in ("%JOB_ERROR_COUNT%") do set JOB_ERROR_COUNT=%%a
for /f "tokens=2 delims=|" %%a in ('findstr /R /I "^JOB_WARNING_COUNT" %tmp_file%') do set JOB_WARNING_COUNT=%%a
for /f "tokens=1,*" %%a in ("%JOB_WARNING_COUNT%") do set JOB_WARNING_COUNT=%%a
del /q %TMP_FILE%
call:log_info "Job status : %job_status%"
call:log_info "Job status msg : %job_status_message%"
rem call:log_info "Volume : %job_volume%"
call:log_info "Error count : %job_error_count%"
call:log_info "Warning count : %job_warning_count%"
goto:eof
rem -----------------------------------------------------------
rem Function Name: fatal
rem Description :
rem Error handler called called by trap on error etc.
rem Handle clean up and exit with error.
rem Input : Return code from failed command in ?.
rem Output : N/A
rem -----------------------------------------------------------
rem -----------------------------------------------------------
rem Function Name: clear
rem Description :
rem Handle clean up.
rem Input : N/A
rem Output : N/A
rem -----------------------------------------------------------
:cleanup
if not '%job_status%' == 'COMPLETED' (
call:log_info "--------- Ended with failure -------------------------"
) else (
call:log_info "----------------------- End --------------------------"
)
goto:eof
rem -----------------------------------------------------------
rem Function Name: log_info
rem Description :
rem Write information to log file and stdout.
rem Input : 1 - Message to log.
rem Output : N/A
rem -----------------------------------------------------------
:log_info
set stamp=%TIMESTAMP%
rem echo [INFO.][%stamp%] %1
rem echo.
rem echo %LOG_FILE%
echo [INFO.][%stamp%] %1 >> %LOG_FILE%
echo. >> %LOG_FILE%
goto:eof
:end
exit /b %RC%
endlocal
Why does error handling after calling a batch file not work as expected in my batch file?
I have re-written your CMD script to be MUCh tighter, and I Put in a variable to set the FWK variable, and there are other variables that are being set that are not being used I removed many of them from here, and there are additional script variables that are not being set by you from anywhere That I ca find, perhaps they are coming from the other .bat file?>
Lets go through this in chat in more detail as there are several unknowns and I obviously can;t test this myself as it depends so heavily on the custom scripts you have.
:: ##########################################################################
:: # Program Name: run_process.bat
:: # Description:
:: # This script is a part of the batch framework. It contains the common
:: # functions for running batch jobs.
:: #
:: # Usage:
:: # run_process.bat <processname>
:: #
:: # History:
:: # Date Who Description
:: # ---------- ------------------ ----------------------------------------
:: # 01-Jun-2014 Ilavarasan Sekar Created
:: ##########################################################################
::
#(
echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET "_eLvl=0"
IF /I "%1" EQU "" (
CALL :Show_Usage
Exit /B 1
)
REM ENTER YOUR DIRECTORIES
SET "dirname=D:\JDA\Batch_FWK\fwk"
SET "FWK_DIR=C:\SOME\DIRECTORY"
REM If Batch Environment Bat File is missing Quit! Otherwise load it and continue
SET "batchenv=!dirname!\batchenv.bat"
IF NOT EXIST "!batchenv!" (
ECHO Environment file !batchenv!" is missing.
Exit /B 1
)
CALL "!batchenv!"
SET "_Write-Info=call :log_info "
)
CALL :Main
(
ENDLOCAL
EXIT /B %_eLvl%
)
:Main
CALL :Initialize_Variables
rem --- Start job ---------------------------------------------
call :start_job
FOR %A IN (
Step1
Step2
Step3
) DO (
IF /I !_eLvl! LEQ 0 (
ECHO. Running :%%A
CALL :%%A
) ELSE (
ECHO Error Occured! Skipping :%%A
)
)
IF !_eLvl! GTR 0 (
SET "job_status=FAILED"
)
rem --- End job -------------------------------------------------
call :end_job
GOTO :EOF
:Initialize_Variables
rem --- Initialize --------------------------------------------
REM SET "NLS_DATE_FORMAT=RRRR-MM-DD-HH24:MI"
REM --- Set up the Date Variable
FOR /F "Tokens=1-7 delims=MTWFSmtwfsouehrandit:-\/. " %%A IN ("%DATE% %TIME: =0%") DO (
FOR /F "Tokens=2-4 Skip=1 Delims=(-)" %%a IN ('ECHO.^| DATE') DO (
SET "_%%~a=%%~A"
SET "_%%~b=%%~B"
SET "_%%~c=%%~C"
SET "TIMESTAMP=!_YY!!_mm!!_dd!_%%~D%%~E%%~F"
)
)
REM Setup Logs:
SET "LOG_FILE=%LOG_DIR%\%process_id%_%TIMESTAMP%.log"
SET "TMP_FILE=%TMP_DIR%\%process_id%_%TIMESTAMP%.tmp"
rem Install error handler.
rem basename=$(basename $0)
REM Setup the Process Info and default variable States
SET "process_id=%1"
SET "job_id="
SET "process_type="
SET "preprocess_type="
SET "postprocess_type="
SET "job_status=CREATED"
SET "job_status_message="
SET "log_level=INFO"
SET "pre="
GOTO :EOF
:show_usage
#echo Usage: Usage is run_process.bat ProcessName
GOTO :EOF
:start_job
rem --- Function definitions ----------------------------------
rem -----------------------------------------------------------
rem Function Name: start_job
rem Description :
rem Retrieve process properties and start a new job for
rem the specified process.
rem Input : process_id - Process to start.
rem Output : job_id - New job id set.
rem -----------------------------------------------------------
%_Write-Info%"--- Starting -----------------------------------"
%_Write-Info%"Process : %process_id%"
%_Write-Info%"Environment : %ENV_NAME%"
%_Write-Info%"Batch root : %ENV_HOME%"
%_Write-Info%"Log file : "%LOG_FILE%""
%_Write-Info%"Tmp file : "%TMP_FILE%" "
sqlplus -s /nolog #"%dirname%/sqlFile.sql" '%BATCHUSER%' '%BATCHPWD%' '%ORACLE_SID%' '%process_id%'
for /f "tokens=2 delims=|" %%a in ('findstr /R /I "^JOB_ID" "%TMP_FILE%" ') do SET "JOB_ID=%%a"
for /f "tokens=1,*" %%a in ("%JOB_ID%") do SET "JOB_ID=%%a"
for /f "tokens=2 delims=|" %%a in ('findstr /R /I "^PROCESS_TYPE" "%TMP_FILE%" ') do SET "PROCESS_TYPE=%%a"
for /f "tokens=1,*" %%a in ("%PROCESS_TYPE%") do SET "PROCESS_TYPE=%%a"
for /f "tokens=2 delims=|" %%a in ('findstr /R /I "^PREPROCESS_TYPE" "%TMP_FILE%" ') do SET "PREPROCESS_TYPE=%%a"
for /f "tokens=1,*" %%a in ("%PREPROCESS_TYPE%") do SET "PREPROCESS_TYPE=%%a"
for /f "tokens=2 delims=|" %%a in ('findstr /R /I "^POSTPROCESS_TYPE" "%TMP_FILE%" ') do SET "POSTPROCESS_TYPE=%%a"
for /f "tokens=1,*" %%a in ("%POSTPROCESS_TYPE%") do SET "POSTPROCESS_TYPE=%%a"
for /f "tokens=2 delims=|" %%a in ('findstr /R /I "^LOG_LEVEL" "%TMP_FILE%" ') do SET "LOG_LEVEL=%%a"
for /f "tokens=1,*" %%a in ("%LOG_LEVEL%") do SET "LOG_LEVEL=%%a"
SET "job_status=RUNNING"
del /q "%TMP_FILE% ""
IF "%job_id%" == "" (
SET "job_status=FAILED"
SET "_eLvl=1"
)
%_Write-Info%"Job id : %job_id%"
%_Write-Info%"Process type : %process_type%"
%_Write-Info%"Pre-process type : %preprocess_type%"
%_Write-Info%"Post-process type: %postprocess_type%"
%_Write-Info%"--- Running ------------------------------------"
goto :eof
:Step1
(
ECHO."SQL CMD" | FIND /I /V "%preprocess_type%" (
) && (
%_Write-Info%"No Prerocess_type."
)
) || (
CALL SET "pre=!%preprocess_type%_DIR!\%process_id%_pre"
CALL "%FWK_DIR%\run_sql_process.bat" "!pre!.%preprocess_type%"
)
SET "_eLvl=%ERRORLEVEL%"
GOTO :EOF
:Step2
rem --- Execute main process ----------------------------------
SET "_Valid_Procs=SQL CMD SRE PERL SQLLDR"
SET "_SQL.Ext=SQL"
SET "_CMD.Ext=CMD"
SET "_Perl.Ext=pl"
SET "_SRE.Ext=xml"
rem Check is valid Process Type
ECHO."%_Valid_Procs%" | FIND /I /V "%process_type%" >NUL && (
%_Write-Info%"Unsupported process type %process_type%."
SET "job_status=FAILED"
SET "_eLvl=1"
GOTO :EOF
)
IF /I "%process_type%" EQU "SQLLDR" (
%_Write-Info%"SQLLDR."
SET "ldr=%CTL_DIR%\%process_id%"
SET "log=%LOG_DIR%\%process_id%_%TIMESTAMP%"
SET "bad=%BAD_DIR%\%process_id%_%TIMESTAMP%"
SET "data=%IN_DIR%\%process_id%"
CALL SET "FWK_CMD=%FWK_DIR%\run_SQLLDR_process.bat" !ldr!.ctl !log!.log !bad!.bad !data!.dat %BATCHUSER% %BATCHPWD% %ORACLE_SID%"
) ELSE (
FOR %%A IN (
%_Valid_Procs%"
) DO (
%_Write-Info%"%%A."
CALL SET "FWK_CMD="%FWK_DIR%\run_%%A_process.bat" "Main=!%%A_DIR!\%process_id%.!_%%A.ext!""
)
)
%_Write-Info% Running: %FWK_CMD%
call "%FWK_CMD%"
SET "_eLvl=%ERRORLEVEL%"
GOTO :EOF
:Step3
(
ECHO."SQL CMD" | FIND /I /V "%postprocess_type%" (
) && (
%_Write-Info%"No Prerocess_type."
)
) || (
CALL SET "pre=!%preprocess_type%_DIR!\%process_id%_post"
CALL "%FWK_DIR%\run_%preprocess_type%_process.bat" "!pre!.%preprocess_type%"
)
SET "_eLvl=%ERRORLEVEL%"
GOTO :EOF
:end_job
rem -----------------------------------------------------------
rem Function Name: end_job
rem Description :
rem End the current job.
rem Input : job_id
rem Output : N/A
rem -----------------------------------------------------------
%_Write-Info%"--- Ending -------------------------------------"
sqlplus -s /nolog #%dirname%/sqlFileEnd.sql '%BATCHUSER%' '%BATCHPWD%' '%ORACLE_SID%' '%job_id%' '%job_status%'
for /f "tokens=2 delims=|" %%a in ('findstr /R /I "^JOB_STATUS|" "%TMP_FILE%" ') do SET "JOB_STATUS=%%a"
for /f "tokens=1,*" %%a in ("%JOB_STATUS%") do SET "JOB_STATUS=%%a"
for /f "tokens=2 delims=|" %%a in ('findstr /R /I "^JOB_STATUS_MESSAGE" "%TMP_FILE%" ') do SET "JOB_STATUS_MESSAGE=%%a"
for /f "tokens=1,*" %%a in ("%JOB_STATUS_MESSAGE%") do SET "JOB_STATUS_MESSAGE=%%a"
for /f "tokens=2 delims=|" %%a in ('findstr /R /I "^JOB_VOLUME" "%TMP_FILE%" ') do SET "JOB_VOLUME=%%a"
for /f "tokens=1,*" %%a in ("%JOB_VOLUME%") do SET "JOB_VOLUME=%%a"
for /f "tokens=2 delims=|" %%a in ('findstr /R /I "^JOB_ERROR_COUNT" "%TMP_FILE%" ') do SET "JOB_ERROR_COUNT=%%a"
for /f "tokens=1,*" %%a in ("%JOB_ERROR_COUNT%") do SET "JOB_ERROR_COUNT=%%a"
for /f "tokens=2 delims=|" %%a in ('findstr /R /I "^JOB_WARNING_COUNT" "%TMP_FILE%" ') do SET "JOB_WARNING_COUNT=%%a"
for /f "tokens=1,*" %%a in ("%JOB_WARNING_COUNT%") do SET "JOB_WARNING_COUNT=%%a"
del /q "%TMP_FILE%"
%_Write-Info%"Job status : %job_status%"
%_Write-Info%"Job status msg : %job_status_message%"
rem %_Write-Info%"Volume : %job_volume%"
%_Write-Info%"Error count : %job_error_count%"
%_Write-Info%"Warning count : %job_warning_count%"
CALL :Cleanup
goto :eof
:cleanup
rem -----------------------------------------------------------
rem Function Name: clear
rem Description :
rem Handle clean up.
rem Input : N/A
rem Output : N/A
rem -----------------------------------------------------------
if not '%job_status%' == 'COMPLETED' (
%_Write-Info%"--------- Ended with failure -------------------------"
) else (
%_Write-Info%"----------------------- End --------------------------"
)
goto :eof
:log_info
rem -----------------------------------------------------------
rem Function Name: log_info
rem Description :
rem Write information to log file and stdout.
rem Input : 1 - Message to log.
rem Output : N/A
rem -----------------------------------------------------------
SET "stamp=%TIMESTAMP%"
rem echo [INFO.][%stamp%] %1
rem echo.
rem echo "%LOG_FILE%"
echo.[INFO.][%stamp%] %*>> "%LOG_FILE%"
echo.>> "%LOG_FILE%"
goto :eof
My batch file is silently ending execution on the first loop (for /d "delims= eol=|" %%d in (*.*) do () and I can't tell why.
I'm not calling any other batch files
My subfolder names DO contain spaces, which I tried to handle with "delims= eol=|"
I never see !parent!!folder! echoed and it doesn't pause at the end. What am I doing wrong?
#Echo off
setlocal enabledelayedexpansion
pushd "%~dp0"
set "parent=%~dp0"
echo !parent!
set "destination=\\(test destination folder)\"
rem SET "destination=\\(prod destination folder)\"
set "FileCount=0"
for /d "delims= eol=|" %%d in (*.*) do (
set "folder=%%d"
echo !parent!!folder!
pause
for %%f in ("!parent!!folder!\(file identifying-pattern)*.DAT") do (
echo "File Type 1"
pause
if !FileCount! lss 200 (
set /a !FileCount!+=1
ECHO !FileCount!
ECHO !parent!!folder!%%f
ECHO !destination!%%f
MOVE "%%f" "!destination!"
)
(
for %%f in ("!parent!!folder!\(file identifying-pattern)*.DAT") do (
echo "File Type 2"
pause
if !FileCount! lss 200 (
set /a !FileCount!+=1
ECHO !FileCount!
ECHO !parent!!folder!%%f
ECHO !destination!%%f
MOVE "%%f" "!destination!"
)
)
)
for /d %%d in (*.*) do (
set "folder=%%d"
if not %%d==Archive (
if not %%d==Hold (
dir /b "!folder!"|find /v "">nul && ECHO "!folder! NOT EMPTY"||rmdir "!parent!!folder!"
)
)
)
pause
popd
I've took a guess at what you were doing, trying not to change the structure too much!
You'll need to add your destinations on lines 4 and 5 and your file patterns to lines 8 and 9 as necessary. (Please make sure you do not end your destination paths with back slashes):
#Echo Off
CD /D "%~dp0" 2>Nul || Exit /B
Set "Destination=\\(test destination folder)"
Rem Set "Destination=\\(prod destination folder)"
If Not Exist "%destination%\" Exit /B
Set "Pattern1=(file identifying-pattern)"
Set "Pattern2=(file identifying-pattern)"
Set /A Type1Count=Type2Count=0
SetLocal EnableDelayedExpansion
For /D %%A In (*) Do (
For %%B In ("%%A\%Pattern1%*.dat") Do (
Echo "File Type 1"
Set /A Type1Count +=1
If !Type1Count! Lss 200 (
Echo !Type1Count!. "%CD%\%%A\%%B" --^> "%Destination%\%%B"
If Not Exist "%Destination%\%%B\" MD "%Destination%\%%B"
Move /Y "%%B" "%Destination%\%%B">Nul
)
)
For %%C In ("%%A\%Pattern2%*.dat") Do (
Echo "File Type 2"
Set /A Type2Count +=1
If !Type2Count! Lss 200 (
Echo !Type2Count!. "%CD%\%%B\%%C" --^> "%Destination%\%%C"
If Not Exist "%Destination%\%%C\" MD "%Destination%\%%C"
Move /Y "%%C" "%Destination%\%%C">Nul
)
)
If /I Not "%%A"=="Archive" (
If /I Not "%%A"=="Hold" (
Set "file="
For %%D In (*) Do Set "file=1"
If Defined file (Echo %%A NOT EMPTY) Else RD "%%A" 2>Nul
)
)
)
Pause
If you're happy with it in your test you can Remark line 4 and unRemark line 5.
I have a program to search drives for a specific file. The only problem is is that i wont know what drive it is on. The problem occurs when it searches a drive for a file that isn't there is there a way to go on to the next drive after say like 10 seconds. Heres my code so far
#echo off
setlocal EnableDelayedExpansion
set count=1
for /f "skip=1" %%a in ('wmic logicaldisk get caption') do (
set drive!count!=%%a
set /a count+=1
)
set "gh=!drive1!"
::Change this to do whatever with the variables
set "fh=!drive2!"
set "hh=!drive3!"
set "jh=!drive4!"
For /R %gh%\ %%G IN (*.ut2) do set jk="%%~dpG"
if defined jk (
echo %jk%
) else (
goto next
)
for /r %jk% %%a in (*) do if "%%~nxa"=="CTF-Hydro-16-2k3.ut2" set k=%%~dpnxa
if defined k (
echo %k% found
pause
cls
echo You have it
goto end
) else (
echo Map not found
copy %CD:~0,3%\Unrealmap\CTF-Hydro-16-2k3.ut2 %jk%
goto end
)
:next
For /R %fh%\ %%G IN (*.ut2) do set ht="%%~dpG"
if defined ht (
echo %ht%
) else (
goto there
)
for /r %ht% %%a in (*) do if "%%~nxa"=="CTF-Hydro-16-2k3.ut2" set m=%%~dpnxa
if defined k (
echo %m% found
pause
cls
echo You have it
goto end
) else (
echo Map not found
copy %CD:~0,3%\Unrealmap\CTF-Hydro-16-2k3.ut2 %jk%
goto end
:there
:end
cls
echo done
pause
#echo off
for /f "skip=1" %%D in ('wmic logicaldisk get caption') do (
for /r "%%D\\" %%G IN (*.ut2) do (
echo %%~dpG
if exist "%%~dpGCTF-Hydro-16-2k3.ut2" (
echo Found the map.
goto end
) else (
echo Map not found, copying
copy %CD:~0,3%\Unrealmap\CTF-Hydro-16-2k3.ut2 "%%~dpG"
goto end
)
)
)
:end
pause
Try next approach:
#ECHO OFF >NUL
SETLOCAL enableextensions
set /A "ii=0"
for /f "skip=1" %%D in ('
wmic logicaldisk get caption
') do for /F %%d in ("%%D") do (
echo searching %%d
for /F "delims=" %%G IN ('dir /B /S "%%d\CTF-Hydro-16-2k3.ut2" 2^>nul') do (
set /A "ii+=1"
set "k=%%G"
rem remove 'rem' from next line to discontinue searching
rem goto :testfound
)
TIMEOUT /T 10 /NOBREAK >NUL
)
:testfound
if %ii% EQU 0 (
echo Map not found
) else (
echo Map found %ii% times: last one "%k%"
)
Here the for loops against wmic command are
%%D to retrieve the caption value;
%%d to remove the ending carriage return in the value returned: wmic behaviour: each output line ends with 0x0D0D0A (<CR><CR><LF>) instead of common 0x0D0A (<CR><LF>).
See Dave Benham's WMIC and FOR /F: A fix for the trailing <CR> problem
Another eventuality: parse
wmic datafile where "Extension='ut2' and FileName='CTF-Hydro-16-2k3'" get Name 2>NUL
The following is part of a batch script I'm testing, but the 2nd IF-statement at the bottom just won't work. I'm confident I need ENABLEDELAYEDEXPANSION, but I just don't know where and how.
FOR /F "delims==" %%B IN ("Filename_example.m4v") DO (
FOR /F "tokens=1 delims=." %%C IN ("%%B") DO SET name=%%C
FOR /F "tokens=2 delims=." %%D IN ("%%B") DO (
IF "%%D" EQU "m4v" SET ext=mp4
IF "%%D" EQU "m3u8" SET ext=mp4
IF "%%D" EQU "wmv" SET ext=wmv
)
)
ECHO Filename: %name%.%ext%
SET /P name1=Rename file (Y/N) {Y}?
IF /I "%name1%" EQU "N" SET filename=%name%.%ext%
IF /I "%name1%" NEQ "N" (
ECHO New filename:
SET /P name2=
SET filename=%name2%.%ext%
)
ECHO %filename%
You can use delayed expansion (see npocmaka's answer) or, keeping the same behaviour
for %%B in ("Filename_example.m4v") do (
set "name=%%~nB"
set "ext=%%~xB"
if /i "%%~xB"==".m4v" set "ext=.mp4"
if /i "%%~xB"==".m3u8" set "ext=.mp4"
)
echo Filename: %name%%ext%
set /p "rename=Renamefile (Y/N) {Y}?"
if /i not "%rename%"=="N" (
set /p "filename=New filename"
) else (
set "filename=%name%%ext%
)
echo %filename%
To turn on delayed expansion, use the setlocal command.
setlocal enableDelayedExpansion
FOR /F "delims==" %%B IN ("Filename_example.m4v") DO (
FOR /F "tokens=1 delims=." %%C IN ("%%B") DO SET name=%%C
FOR /F "tokens=2 delims=." %%D IN ("%%B") DO (
IF "%%D" EQU "m4v" SET ext=mp4
IF "%%D" EQU "m3u8" SET ext=mp4
IF "%%D" EQU "wmv" SET ext=wmv
)
)
ECHO Filename: %name%.%ext%
SET /P name1=Rename file (Y/N) {Y}?
IF /I "%name1%" EQU "N" SET filename=%name%.%ext%
IF /I "%name1%" NEQ "N" (
ECHO New filename:
SET /P name2=
SET filename=!name2!.!ext!
)
ECHO %filename%
From what I see you need to use delayedExpansion only on the last SET.To use the delayed expansion you need ! instead of %
Next script shows
where (minimal use, but could be extended to whole script, see npocmaka's answer) and
how to use EnableDelayedExpansion (SETLOCAL).
See also %%~nB, %%~xB and other ~ modifiers in Command Line arguments (Parameters)
Moreover, minimizes user's input to the only set /P.
#ECHO OFF >NUL
SETLOCAL enableextensions
FOR %%B IN (
"FN_example.m4v"
"fn_example2.WMV"
"fn_example3.xyz"
) DO (
set "name=%%~nB"
set "ext=%%~xB"
IF /I "%%~xB" EQU ".m4v" SET "ext=.mp4"
IF /I "%%~xB" EQU ".m3u8" SET "ext=.mp4"
IF /I "%%~xB" EQU ".wmv" SET "ext=.wmv"
SETLOCAL enabledelayedexpansion
ECHO orig. filename: !name!!ext!
SET /P "name=Type new filename (or hit <Enter> to keep '!name!'): "
SET "filename=!name!!ext!"
IF /I "!name!" EQU "%%~nB" (
ECHO name unchanged: !filename!
) else (
ECHO new file name: !filename!
)
ENDLOCAL
echo(
)
Thanks for all your help thus far. I have a working script. I'm sure things could be more efficient, but at least it's working! :) Sorry for the Dutch-language usage, the code may still be useful:
#ECHO off
CLS
REM BatchGemist geschreven door Reino Wijnsma, 2015 (reino#degeelebosch.nl)
SET batchname=BatchGemist
SET version=1.0
TITLE %batchname% %version%
IF NOT EXIST youtube-dl.exe (
ECHO 'youtube-dl.exe' niet gevonden.
ECHO.
GOTO Help
)
IF NOT EXIST ffmpeg.exe (
ECHO 'ffmpeg.exe' niet gevonden.
ECHO.
GOTO Help
)
FOR /F %%A IN ('youtube-dl.exe --version') DO ECHO (youtube-dl %%A)
ECHO Voer "?" in voor Hulp
ECHO.
:Input
ECHO Voer npo.nl, rtlxl.nl, kijk.nl, of youtube programma-link in:
SET url=
SET /P url=
IF "%url%"=="?" GOTO Help
IF "%url%"=="U" GOTO Update
:: stop script bij geen Input (enter-toets)
IF "%url%"=="" GOTO:eof
:: spaties in Input niet toestaan
IF NOT "%url: =%"=="%url%" (
ECHO.
ECHO Spaties in programma-link niet toegestaan.
ECHO.
GOTO Input
)
:: alleen bepaalde websites als input toestaan
IF NOT "%url:http://www.npo.nl=%"=="%url%" GOTO Input2
IF NOT "%url:http://www.rtlxl.nl=%"=="%url%" GOTO Input2
IF NOT "%url:http://www.kijk.nl=%"=="%url%" GOTO Input2
IF NOT "%url:http://www.youtube.com=%"=="%url%" GOTO Input2
IF NOT "%url:https://www.youtube.com=%"=="%url%" GOTO Input2
IF NOT "%url:http://youtu.be=%"=="%url%" GOTO Input2
IF NOT "%url:https://youtu.be=%"=="%url%" GOTO Input2
ECHO.
ECHO Ongeldige programma-link.
ECHO.
GOTO Input
:Input2
ECHO.
youtube-dl.exe --no-warnings -F "%url%"
ECHO.
SET "format="
SET /P "format=Voer gewenst formaat in {best}: "
IF "%format%"=="" SET "format=best"
ECHO.
SET /P "task=Video-link achterhalen (J), of Downloaden (N) {J}? "
IF /I "%task%"=="N" GOTO :Download
:: voer youtube-dl uit en kopiëer video-link zonder eol-character naar het klembord
FOR /F %%B IN ('youtube-dl.exe --no-warnings -gf "%format%" "%url%"') DO (
ECHO.
ECHO Video-link:
ECHO %%B
ECHO|SET /P ="%%B"|clip.exe
ECHO.
ECHO Video-link gekopi‰erd naar het klembord.
ECHO.
ECHO.
)
:: ‰ (Alt+0137, ANSI CP-1252) wordt in cmd.exe als ë (Alt+137, OEM CP-437 en CP-850) weergegeven
:: https://en.wikipedia.org/wiki/Code_page_437
:: https://en.wikipedia.org/wiki/Code_page_850
:: https://en.wikipedia.org/wiki/Windows-1252
GOTO Input
:Download
ECHO.
ECHO Doelmap: %~dp0
SET /P "remap=Wijzigen (J/N) {J}? "
IF /I "%remap%"=="N" (
SET "map=%~dp0"
) ELSE (
SET /P "map=Opslaan in: "
)
IF NOT "%map:~-1%"=="\" SET "map=%map%\"
FOR /F "delims==" %%C IN ('youtube-dl.exe --no-warnings --get-filename -f "%format%" "%url%"') DO (
SET "name=%%~nC"
IF /I "%%~xC"==".m4a" SET "ext=.m4a"
IF /I "%%~xC"==".m4v" SET "ext=.mp4"
IF /I "%%~xC"==".m3u8" SET "ext=.mp4"
IF /I "%%~xC"==".wmv" SET "ext=.wmv"
)
ECHO.
ECHO Bestandsnaam: %name%%ext%
SETLOCAL ENABLEDELAYEDEXPANSION
SET /P "rename=Wijzigen (J/N) {J}? "
IF /I "%rename%"=="N" (
SET "filename=%name%%ext%"
) ELSE (
SET /P "rename2=Nieuwe bestandsnaam: "
SET "filename=!rename2!%ext%
)
ECHO.
SET /P "part=Fragment downloaden (J/N) {N}? "
IF /I "%part%"=="J" (
SET /P "ss=Voer begintijd in (in seconden, of in uu:mm:ss[.xxx] vorm): "
SET /P "t=Voer tijdsduur in (in seconden, of in uu:mm:ss[.xxx] vorm): "
ECHO.
FOR /F %%D IN ('youtube-dl.exe --no-warnings -gf "%format%" "%url%"') DO (
ffmpeg.exe -hide_banner -ss "!ss!" -i "%%D" -c copy -bsf:a aac_adtstoasc -t "!t!" "%map%%filename%"
)
) ELSE (
ECHO.
FOR /F %%E IN ('youtube-dl.exe --no-warnings -gf "%format%" "%url%"') DO (
ffmpeg.exe -hide_banner -i "%%E" -c copy -bsf:a aac_adtstoasc "%map%%filename%"
)
)
ENDLOCAL
ECHO.
ECHO.
GOTO Input
:Update
youtube-dl.exe -U
ECHO.
ECHO.
GOTO Input
:Help
ECHO.
ECHO H U L P
ECHO.
GOTO Input