Batch script terminating unexpectedly before IF command - batch-file

I have written the below code in a batch script.
set /p timestamp=Enter timestamp:
cd "C:\temp\%timestamp%"
for %%a in (*.rmt) do (bldtool -c COMMAND -a SPLIT -n %%a -l C:\temp\%timestamp%)
if exist "C:\temp\%timestamp%\XCLES01A.c" (xcopy /Y "C:\Program Files\CA\AllFusion Gen\GEN\extrn\src\XCLES01A.c" "C:\temp\%timestamp%")
for %%a in (*.icm) do (bldtool -c COMMAND -a BUILD -n %%a -l C:\temp\%timestamp% -f CodeMgr)
pause
When i run the above script line-by-line in cmd, it runs as expected. However, when I run it in the form of a script, it terminates before the if command is executed i.e. the window just disappears.
I have spent hours on trying to resolve this and am still stuck. Please help ! I am very new to batch scripting.
Thanks in advance.
Update: BLDTOOL is an executable software.

Try this (I haven't though):
set /p timestamp=Enter timestamp:
cd /D "C:\temp\%timestamp%"
for %%a in (*.rmt) do (
bldtool -c COMMAND -a SPLIT -n %%a -l "C:\temp\%timestamp%"
)
if exist "C:\temp\%timestamp%\XCLES01A.c" (
xcopy /Y "C:\Program Files\CA\AllFusion Gen\GEN\extrn\src\XCLES01A.c" "C:\temp\%timestamp%"
)
for %%a in (*.icm) do (
bldtool -c COMMAND -a BUILD -n %%a -l "C:\temp\%timestamp%" -f CodeMgr
)
pause

Related

Find all .sql-Files in a Directory and execute them with sqlcmd

I'm still a CMD beginner and would like to import all SQL files I have in a directory structure with sqlcmd to my DB.
If I copy all *.sql into the root folder the following command works:
#ECHO ON
FOR %%G in (*.sql) DO sqlcmd /S *SERVER* /d *DB* /U *USER* /P *PW* -i"%%G" > LOG_lastrun.txt 2> LOG_errors.txt
pause
#ECHO ON
FOR /F "tokens=1 delims=sql" %%G in (*.sql) DO sqlcmd /S *SERVER* /d *DB* /U *USER* /P *PW* -i"%%G" > LOG_lastrun.txt 2> LOG_errors.txt
pause
Unfortunately, it doesn't work for me FOR /F loops. Can you help me here?
The FOR /F works differently. It runs a command. Jusr *.sql is not a command. Also, I suspect that you want to concatenate stdout and stderr output using >>.
#ECHO ON
SET "SERVER_NAME=*SERVER*"
SET "DB_NAME=*DB*"
SET "LOGIN_ID=*USER*"
SET "PASS=*PW*"
FOR /F "delims=" %%G IN ('DIR /B "*.sql"') DO (
sqlcmd -S %SERVER_NAME% -d %DB_NAME% -U %LOGIN_ID% -P %PASS% -i "%%~G" >> LOG_lastrun.txt 2>> LOG_errors.txt
)
pause

CMD search results send to console

I would like to ask if anyone has an idea how to send search result to console in single command line but with some specific code before and after the search results output
I need to run program Xconverter that is made to be operated by console, and has parameters:
-s :silent mode (only mode suported yet)
-i [files] : list location of the files in quotes: {-i "a.txt" "b.txt"..}
-t [tool file] : tool file location
-m 5 : conversion style
syntax accepted for example (single command for XConverter):
"C:\XConverter.exe" -s -i "C:\a.pgm" "C:\abc\b.pgm" -t "tool.tlgx" -m 5
but I need about 500 -i [files] to list
so i created search that fill look for those .pgm in tree mode where .bat is located and adds search result (file locations) one after other
my code is working but the only way i made it to work is that i outputed echo result in to extra .bat file and started the file. Is there i way to reprogram my code that it will send these outpust in to single one line command?
echo off
SET mypath=%~dp0
::THIS STARTS PROGRAM
(
echo | set /p="""
echo | set /p="C:\Program Files (x86)\Scm Group\Maestro\XConverter.exe" -s -i "
)>seznam_konvertovanych_suboru.bat
::THIS SEARCHERS FOR PGM FILES AND ADDS PATH OF EACH FILE TO THE LINE
for /f "delims=" %%A in ('forfiles /s /m *.pgm /c "cmd /c echo #relpath"') do (
set "file=%%~A"
setlocal enableDelayedExpansion
echo | set /p="""
echo | set /p="%mypath:~0,-1%\!file:~2!"
echo | set /p="" "
)>>seznam_konvertovanych_suboru.bat
::THIS ADDS SOME EXTRA FILE PATH TO THE LINE OF TOOL FILE, THAT IS NEEDED
(
echo | set /p=-t "Tlgx\def.tlgx" -m 5
)>>seznam_konvertovanych_suboru.bat
seznam_konvertovanych_suboru.bat
This code will output file seznam_konvertovanych_suboru.bat exactly in this format
"C:\XConverter.exe" -s -i "C:\a.pgm" "C:\abc\b.pgm" -t "tool.tlgx" -m 5
Anyone knows how to reprogram this to send that output to console in single line without creating extra file to store that code?
Edited:
What i need is having START_CONVERTING.BAT file that will send to console one command :
"C:\XConverter.exe" -s -i "C:\a.pgm" "C:\abc\b.pgm" -t "tool.tlgx" -m 5
but "C:\a.pgm" "C:\abc\b.pgm" are the search results of all .pgm files in that folder and child folders. So i will copy this START_CONVERTING.BAT anywehere where i store .pgm files. (some of them are in folders and those folders in other folders) so i will just copy STAR_CONVERTING.BAT in major folder that will thake care of all files with all files in child folders.
I will just click START_CONVERTING.BAT and it will convert those .pgm files. (without creating creating any other output to any other .bat file.
SOLVED (code)
#echo off & setlocal enableDelayedExpansion
PushD "%~dp0"
Set "XC=C:\Program Files (x86)\Scm Group\Maestro\XConverter.exe"
Set "TOOLFILE=Tlgx\def.tlgx"
Set "Files="
:: Concatenate all files in one string
for /f "delims=" %%A in ('Dir /B /S *.pgm') do set Files=!Files! "%%~A"
:: command, command line length may be an issue
"%XC%" -s -i%Files% -t "%TOOLFILE%" -m 5
If I understand right this might work
#echo off & setlocal enableDelayedExpansion
PushD "%~dp0"
Set "XC=C:\Program Files (x86)\Scm Group\Maestro\XConverter.exe"
Set "Files="
:: Concatenate all files in one string
for /f "delims=" %%A in ('Dir /B /S *.pgm') do set Files=!Files! "%%~A"
:: store command in batch file, command line length may be an issue
>"seznam_konvertovanych_suboru.bat" Echo "%XC%" -s -i %Files% -t "tool.tlgx" -m 5
try with that batch
Xconverter is for xxl to PGMX
1 set all path for converter
2 creat to do list of xxl files on desktop on this exemple or desktop sub folder!
3 Masetro can't be install as Office PC you must run it on CN PC!
#echo OFF
COLOR F1
TITLE "Convertor XXL to PGMX"
echo ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
echo º Converteur Xconverter º
echo ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ
echo.
echo "Stop command with Ctrl+C"
echo.
Echo "Path SET"
TIMEOUT /t 5 >nul
Set todo="%userprofile%\desktop\todo.txt"
Set def="%PROGRAMFILES(x86)%\Scm Group\Maestro\Tlgx\def.tlgx"
Set Compil="%PROGRAMFILES(x86)%\Scm Group\Maestro\Xconverter.exe"
Set desktop="%userprofile%\desktop"
echo "To do list of *.XXL files"
TIMEOUT /T 10 >nul
cd %userprofile%\desktop
dir /a /b /o /s *.xxl > %todo%
echo "Files convert command"
TIMEOUT /T 10 >nul
for /f "delims=" %%a in (%todo%) do %Compil% ^ -s -m 4 ^
-i "%%a" ^
-t %def%
echo "Rapport files"
Timeout /T 10 >nul
cd %desktop%
dir /a /b /o /s *.pgmx > Rapport.txt
Echo "Delete to do list"
TIMEOUT /T 10 >nul
DEL %todo% /F /S /Q >nul
But haven't go detail of Convesion style -m X
if you can replie me detail of -m number it was great!
All the best, and hope this is helpful
D-Alexandre V.

Batch Script ErrorLevel Output

I need help - I am trying to output any ErrorLevel that is 1 or greater into a log file. When I issue the command the log file never get's generated. Any help would be greatly appreciated.
Script:
for /f "delims=" %%i in (C:\_\Restart\Computer.txt) do (
start "%%i" \_\PStools\psexec \\%%i -u Administrator -p Password -i c:\restart.cmd
if not %errorlevel%==0 echo %errorlevel% > error.log
)
This script allows me to use PSEXEC and issue a restart command to all the computer at once. However several of them fail and I'd like to know which ones fail.
Thanks!
Is this the format I should use?
setlocal EnableDelayedExpansion
for /f "delims=" %%i in (C:\_\Restart\Computer.txt) do (
start "%%i" \_\PStools\psexec \\%%i -u Administrator -p Password -i c:\restart.cmd
if errorlevel 1 echo !errorlevel! > error.log
)
Script V3:
setlocal EnableDelayedExpansion
for /f "delims=" %%i in (C:\temp\list.txt) do (
start "" "shutdown" /m \\%%i -r -f -t 900
echo !errorlevel! && echo %%i
if errorlevel 1 echo !errorlevel! >> c:\temp\log.txt && echo %%i >> c:\temp\log.txt
)
-m = use remote computer
-r = reboot
-f = Force reboot
-t = delay of time before rebooting
you can use shutdown -? for more help on argument that can be passed to the reboot command.
Script v4 without the start command:
setlocal EnableDelayedExpansion
for /f "delims=" %%i in (C:\temp\list.txt) do (
shutdown /m \\%%i -r -f -t 900
echo !errorlevel! && echo %%i
if errorlevel 1 echo !errorlevel! >> c:\temp\log.txt && echo %%i >> c:\temp\log.txt
)

Run all SQL queries in a folder and its sub folders

I am trying to run several hundreds queries that are located in many folders. All the folders are located under one "Master" directory.
I have the following batch code that allows me to run all the queries in one folder:
for %%G in (*.sql) do psql -U postgres -f "%%G" Satellites_Updated
pause
I am unsure how to make this batch file recursive so I can also check sub folders.
I have the following code, which does not work (only prints the current directory in the cmd window, then exits).
#echo off
call :treeProcess
goto :eof
:treeProcess
for %%G in (*) do (
if exist %1\* (
cd %1
call :treeProcess
cd ..
)
else (
psql -U postgres -f "%%G" Satellites_Updated
)
)
exit \b
Thanks in advance!
Here is the solution:
for /r %%G in (*.sql) do psql -U postgres -f "%%G" Satellites_Updated
pause
When it is so simple sometimes ...

running batch process over file named in unicode

I need to run a process over file named in unicode ?
How using a batch file ?
I tried :
dir /b %folder% > dir.lst
::cmd /u /c dir /b %folder% > dir.lst
FOR /F "delims= tokens=*" %%G IN (dir.lst) DO (
echo %%G
#set filename2=%%G
#echo %filename2%
#set filename=%pathlogs%\!filename2!
#echo %filename%
#echo %filename2%
#%binaries%\gawk -v fn=%filename% -v fn2=%filename2% -f %scripts%\AlexBank_barcode.awk %folder%\%filename2% 2> %scripts%\AlexBank_barcode.fil
#%binaries%\sort -k 1.129,1.144 -T F:\tmp %pathlogs%\%filename2%.log > %pathlogs%\%filename2%.log.tmp 2> %scripts%\%filename2%".sort.fil"
#set filename3=%filename%.log.tmp1
#%binaries%\gawk -f %scripts%\AlexBank_collect_barcode.awk %pathlogs%\%filename2%.log.tmp 2> %scripts%\AlexBank_collect_barcode.fil
#set filename3=%filename%.separate_all.log
#%binaries%\gawk -f %scripts%\AlexBank_separate_barcode.awk %pathlogs%\%filename2%.log.tmp1 2> %scripts%\AlexBank_separate_barcode.fil
#set filename3=%filename%.separate_statement_le.awk.log
#%binaries%\gawk -v fn=%filename3% -f %scripts%\AlexBank_separate_statement_le.awk %pathlogs%\%filename2%.log.tmp1 2> %scripts%\%filename2%".AlexBank_separate_statement_le.awk.fil"
#set filename3=%filename%.separate_statement_gt.awk.log
#%binaries%\gawk -v fn=%filename3% -f %scripts%\AlexBank_separate_statement_gt.awk %pathlogs%\%filename2%.log.tmp1 2> %scripts%\%filename2%".AlexBank_separate_statement_gt.awk.fil"
)
You have two options here:
Set a TrueType font for your console window
Don't use for /f to iterate over the output of dir /b. It's unnecessary in the vast majority of cases, more complicated to get right, more brittle and fails on Unicode file names when the settings are slightly wrong as you experience. You can use for to iterate over files directly:
for %%F in (%folder%\*) do ...

Resources