FOR LOOP in windows cmd and batch file not working - batch-file

I am trying to call a batch file that runs a for loop and calls a second script:
for /f "usebackq" %%i in (`dir/b /o:d %partionHome%\tmp\queue\*.t~#`) do %partitionHome%\conf\SQLLoader\SQL_Loader_%DSNname%\Script.bat %1 %2 %3 %4 %5 %6 %7 %%i %has_prefix% %partionHome%
Script.bat never runs; I've tried adding do call but i believe this gets ignored with FOR /F, I just cannot get the loop to call the script. All parameters are available and paths etc. are correct?

The DIR command is probably not returning anything, so there is nothing for FOR /F to iterate.
Looks to me like you have a spelling mistake: %partionHome% vs. %partitionHome%.
Also, make sure your variables partitionHome and DSNname are defined properly.
The CALL is definitely needed (once you fix the other problems)
Lastly, you should enclose your paths in quotes, just in case there are spaces and/or poison characters within the value.
for /f "usebackq" %%i in (`dir/b /o:d "%partitionHome%\tmp\queue\*.t~#"`) do call "%partitionHome%\conf\SQLLoader\SQL_Loader_%DSNname%\Script.bat" %1 %2 %3 %4 %5 %6 %7 "%%I" %has_prefix% "%partitionHome%"

Related

Checking parameters for information

I have a Script that reads other .bat Files and counts the Commentlines. I pass the .bat file that i want to test as a parameter.
For example, this is how i start my script.
C:\User\User\Desktop>CommentReader.bat test.bat
Right now test.bat needs to be in the same folder with the Skript.
I want the option that I can pass multiple .bat files that need testing and the option that I can pass the path too. for example:
C:\User\User\Desktop>CommentReader.bat D:\testfolder\test1.bat E:\test2.bat test3.bat
I also want to pass commands for example /l so the script also reads empty lines.
C:\User\User\Desktop>CommentReader.bat /l D:\testfolder\test1.bat E:\test2.bat test3.bat
I know how I can code that it should also read empty lines but
what is the best way to go through my parameters and check the information?
My Idea was something like this:
FOR /f tokens=1,2,3,4,5,6,7,8,9, delims= " %%a in (%*) DO (
)
But maybe there is a better way?
As Stephan stated in the comments section, SHIFT is what you're looking for. This script will loop through all parameters and echo them out. Just replace the ECHO command with your actual code.
#ECHO OFF
:LOOP
IF NOT "%1"=="" (
ECHO %1
SHIFT
GOTO LOOP
)
Your first parameter is always %1, the second one is %2 and so on. The SHIFT command will "forget" the original %1 and decrease all indices by 1, so %2 will become %1, %3 will become %2 and so on. As soon as you reach the last argument SHIFT will turn %1 into an empty string so that you beak out of the loop.

Set command puts all parameters into %1 %2 %3 etc

A few years back I remember using a set command that would take a variable separated by spaces that would assign each value to %1 %2 %3 etc
I read a line from a file which has the following structure
"log file" "B:\Current\log" "B:\Backup\warehouse" "Warehouse_Day"
and assign it to a variable lets say params, then do the infamous set command params, and then automagically I could use each parameter independently.
If I did echo %1, I would get log file
if I did echo %2, I would get B:\Current\log
if I did echo %3, I would get B:\Backup\warehouse
Does anyone remember or know what the command was?
When I understand you right, this is what you think of:
#echo off
set /p "params="<params.bat
echo %params%
call :program %params%
echo %1, %2, %3, %4 are gone.
echo --%param4%--
goto :eof
:program
echo %1
echo %2
echo %3
echo %4
set param4=%4
Of course the %n parametes are only valid within the called subroutine and are gone, when you return from the subroutine (but you can derive other variables from them).
If you need to get rid of the quotes, use %~1, %~2 and so on.

call ant script in windows cmd with parameters and redirect output

I'm trying to call a windows cmd script that runs ant with a variable number of parameters and redirect the output to a logfile. The reason why I call the script that runs ant is because then I ant returns the control the my calling script in which I can proceed with other stuff. My call fails on the redirect at the end, it looks like the redirect is seen as a parameter.
The call look like this:
call ant_call ant -buildfile %BUILDDIR%\cadis\cm\build.xml -Dcvs.tag=%1 deploy_cmdscripts >> %LOGFILE% 2>&1
Since I have an unknown number of parameters the ant script looks like this
%1 %2 %3 %4 %5 %6 %7 %8 %9
Is there an easy way to solve this?
Use %* instead of %1 %2 %3 %4 %5 %6 %7 %8 %9.
%* contains all parameters from the command line, even more than nine.

Echo parameters to a log file

I need to add logging to a batch file to know what parameters were passed to it.
So if the batch file is run.bat, I have tried adding the line
ECHO %1 %2 %3 %4 %5 %6 %7 %8 %9 >> run.log
Which just echoes the parameters and the string >> run.log.
I suspect you have an unclosed quote somewhere in your parameter list (e.g. foo.cmd "bar would cause that behaviour). You can likely solve this by putting the redirection in front of the echo (yes, this works):
>> run.log echo %*

Batch Script : How do i set a 2nd arguments in a CALL?

for /f %%j in ('dir /b *.txt') do (
findstr /m /i "yoyoyo" %%j
if !ERRORLEVEL! == 0 (
set post=yoyoyo
CALL postset.bat "yoyoyo" %%jj
)
)
I'm trying to pass 2 arguments to a CALL
the first is going through but not the second.
edit my real problem was with the other batch, didn't use %1 and %2, my bad!
The code works well for me. When passing 2 or more parametes to the postset.bat I am able to print out %1 till %9 (if set) from postset.bat.
Because you are not passing the variable %%j but %%jj.......
As this doesn't exist a null-value (nothing) will be passed to the bat-file.
Update:
The ERRORLEVEL test doesn't work as intended due to the way variable expansion works so the bat-file is never called at all.
Use if errorlevel 1 in stead.

Resources