Issue passing variable to second batch file.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
N00b13
Posts: 3
Joined: 15 Jul 2015 09:05

Issue passing variable to second batch file.

#1 Post by N00b13 » 15 Jul 2015 09:18

Okay so I need to search each file for "draft" (non case sensitive) and call the second bat job with the files it finds having draft in it.

Code: Select all

echo on
:: search  files  for string draft in each file held in this folder and echoing result to make sure it is finding the file.
for /f %%i in ('findstr /i /m "draft" DFADV*.*')  DO echo Result: ..%%i..
pause
for /f %%i in ('findstr /i /m "draft" DFADV*.*') do call draft.bat ..%%i..

It is echoing the file(s) i'm expecting to find.

draft.bat does checks to see if it exits in a certain format in another folder.

Code: Select all

::draft.bat 
echo on
echo Result: ..\%%i..
::checking to see if this file has been processed
::the move command also renames to correct version of the file
if exist D:\FTP\Colby\Order001\%%i.(0) goto check(1)
move D:\FTP\Colby\Holding\\%%i D:\FTP\Colby\Holding\Draft\%%i.(0)
GOTO EOF

(repeated 6x)

Code: Select all

:check(5)
if exist D:\FTP\Colby\Order001\%%i.(5) goto ECHO1
move D:\FTP\Colby\Holding\Draft\%%i D:\FTP\Colby\Holding\Draft\%%i.(5)
GOTO EOF

:ECHO1
"Error you have it set up for 0-5 you need more itterations!"
pause

:EOF
pause
EXIT /B


How do I get the %%i to be passed into the called bat job?

Thanks for your help in advance!

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Issue passing variable to second batch file.

#2 Post by foxidrive » 15 Jul 2015 10:49

Try these two snippets to see how the term can be used in draft.bat

Code: Select all

@echo off
call draft.bat "myfile.txt"


Code: Select all

::draft.bat
@echo off
echo the file passed is "%~1"
pause

N00b13
Posts: 3
Joined: 15 Jul 2015 09:05

Re: Issue passing variable to second batch file.

#3 Post by N00b13 » 15 Jul 2015 12:00

@foxidrive Thank your for the help! :D :D :D It does pass the string "myfile.txt" but I am wanting to pass the %%i.
for /f %%i in ('findstr /i /m "draft" DFADV*.*') do echo ..%%1.. will show me on screen what is found (i.e. DFADV999) i want to pass that DFADV999 onto the called bat script. so i used "%%i" to replace the myfile.txt and it passed it! Thank you for helping me learn!

As is the case with most of my knowledge when i learn something i have more questions/errors.
(on time) now my draft.bat is closing abruptly on me even though i have added pause statements in between the lines. I can only assume it is a syntax error with the "%~1" in my if exist statement below i will add a ::<----- where it appears to be closing the cmd prompt window.

Code: Select all

::draft.bat 
echo off
echo the file passed is "%~1"
echo on :: to see what commands are being sent
pause
::checking to see if this file has been processed
::the move command also renames to correct version of the file
pause ::<----- (i see a new line appear and the window close with no regard to my following pause.)
echo if exist D:\FTP\Colby\Order001\%~1.(0) goto check(1)
pause
if exist D:\FTP\Colby\Order001\%~1.(0) goto check(1)
pause
move D:\FTP\Colby\Holding\%~1 D:\FTP\Colby\Holding\Draft\%~1.(0)
pause


I have tried %~1 and "%~1" in my if exists statements.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Issue passing variable to second batch file.

#4 Post by foxidrive » 15 Jul 2015 22:45

Possible issues are that there is no label for the goto command.

The parentheses () can cause problems in certain situations.
I mention that in case the code you pasted is not identical to the code you have tried.

But test this to see how it goes.

Code: Select all

::draft.bat 
@echo off
echo the file passed is "%~1"
if not exist "D:\FTP\Colby\Order001\%~1.(0)" move "D:\FTP\Colby\Holding\%~1" "D:\FTP\Colby\Holding\Draft\%~1.(0)"
pause

N00b13
Posts: 3
Joined: 15 Jul 2015 09:05

Re: Issue passing variable to second batch file.

#5 Post by N00b13 » 16 Jul 2015 04:44

before i saw the post was accepted. i did

Code: Select all

set filea=%~1
then referenced %filea% throught the rest of the draft.bat and it worked. Thank you for your help!

Post Reply