i need to know how to run if statements multiple lines in a batch file.

hi experts,

i need to know how to run if statements multiple lines in a batch file.
first statement only running. next if statement is not running. its coming out of the batch file.

code:
if %programName%=="CD41_2018" or IF %programName%=="CD41_2019"  
(
for /f "tokens=*" %%A in ('dir /b /s /a-d "%ProgramPath%\%programName%\%dateFile%\*_CD391N.txt" "%ProgramPath%\%programName%\%dateFile%\*_CD533.txt" 
	"%ProgramPath%\%programName%\%dateFile%\*_CD391C.txt" "%ProgramPath%\%programName%\%dateFile%\*_CD391E.txt"') do (
    copy "%%~A" "%BackupPath%\PAT BTT Text Files\%programName%\New\"
	))
pause

IF %programName%=="CD42" 
(
for /f "tokens=*" %%A in ('dir /b /s /a-d  "%ProgramPath%\%programName%\%dateFile%\*_CD539C.txt" "%ProgramPath%\%programName%\%dateFile%\*_CD539E_CD390.txt"') do (
    copy "%%~A" "%BackupPath%\PAT BTT Text Files\%programName%\New\"
	))
pause
 if %programName%=="C520MCA"
(
for /f "tokens=*" %%A in ('dir /b /s /a-d "%ProgramPath%\%programName%\%dateFile%\*_C520CH_CCk.txt" "%ProgramPath%\%programName%\%dateFile%\*_C520EU_CBS.txt" 
	"%ProgramPath%\%programName%\%dateFile%\*_C520NA.txt" "%ProgramPath%\%programName%\%dateFile%\*_C520TW_CCZ.txt"') do (
    copy "%%~A" "%BackupPath%\PAT BTT Text Files\%programName%\New\"
	))	
pause

Open in new window



 like that goes on


help me regarding that how to run all if statement??
bhavani vAsked:
Who is Participating?
 
oBdAConnect With a Mentor Commented:
Try it like that; this is in test mode and will only show the copy commands it would normally run. Remove the uppercase ECHO in front of line 16 to run it for real.
@echo off
setlocal

set patternList=
if /i "%programName%"=="CD41_2018"	set patternList=*_CD391N.txt *_CD533.txt *_CD391C.txt *_CD391E.txt
if /i "%programName%"=="CD41_2019"	set patternList=*_CD391N.txt *_CD533.txt *_CD391C.txt *_CD391E.txt
if /i "%programName%"=="CD42"		set patternList=*_CD539C.txt *_CD539E_CD390.txt
if /i "%programName%"=="C520MCA"	set patternList=*_C520CH_CCk.txt *_C520EU_CBS.txt *_C520NA.txt *_C520TW_CCZ.txt
if not defined patternList goto :eof

:Loop
for /f "tokens=1*" %%a in ("%patternList%") do (set fileMask=%%a&set patternList=%%b)
echo Processing '%fileMask%' ...
for /f "tokens=*" %%a in ('dir /b /s /a-d "%ProgramPath%\%programName%\%dateFile%\%fileMask%"') do (
	echo Backing up '%%~nxa' ...
	ECHO copy "%%~a" "%BackupPath%\PAT BTT Text Files\%programName%\New\"
)
if defined patternList goto Loop
pause

Open in new window

0
 
Lee W, MVPTechnology and Business Process AdvisorCommented:
First, there is no "or" in an if statement - no if x==y OR if y==z

If you need two possible options to do the same thing, make a block of code that gets called.

Also, your
IF %programName%=="CD42"
parts PROBABLY should be
IF "%programName%"=="CD42"

Unless %programName% includes the quotes.
I'd probably do something like:

If "%programName%"=="CD42" Goto CD42andCD44
If "%programName%"=="CD43" Goto CD43
If "%programName%"=="CD44" Goto CD42andCD44
Goto End
:CD42andCD44
Rem Do something here
Goto End
:CD43
Rem Do something here
:End

Open in new window


This assumes only one possible %programName%.
0
 
Ben Personick (Previously QCubed)Connect With a Mentor Lead Network EngineerCommented:
Method one:

SET "_CD41Matched="
IF /I "%programName%" EQU "CD41_2018" ( SET "_IsCD41Match=YES" )
IF /I "%programName%" EQU "CD41_2019" ( SET "_IsCD41Match=YES" )
IF DEFINED _CD41Matched (
	for /f "tokens=*" %%A in ('dir /b /s /a-d "%ProgramPath%\%programName%\%dateFile%\*_CD391N.txt" "%ProgramPath%\%programName%\%dateFile%\*_CD533.txt" "%ProgramPath%\%programName%\%dateFile%\*_CD391C.txt" "%ProgramPath%\%programName%\%dateFile%\*_CD391E.txt"') do (
		copy "%%~A" "%BackupPath%\PAT BTT Text Files\%programName%\New\"
	)
)
pause

IF /I "%programName%" EQU "CD42" (
	for /f "tokens=*" %%A in ('dir /b /s /a-d  "%ProgramPath%\%programName%\%dateFile%\*_CD539C.txt" "%ProgramPath%\%programName%\%dateFile%\*_CD539E_CD390.txt"') do (
		copy "%%~A" "%BackupPath%\PAT BTT Text Files\%programName%\New\"
	)
)
pause
IF /I "%programName%" EQU "C520MCA" (
	for /f "tokens=*" %%A in ('dir /b /s /a-d "%ProgramPath%\%programName%\%dateFile%\*_C520CH_CCk.txt" "%ProgramPath%\%programName%\%dateFile%\*_C520EU_CBS.txt" "%ProgramPath%\%programName%\%dateFile%\*_C520NA.txt" "%ProgramPath%\%programName%\%dateFile%\*_C520TW_CCZ.txt"') do (
		copy "%%~A" "%BackupPath%\PAT BTT Text Files\%programName%\New\"
	)
)
pause

Open in new window


method 2:

:Main
	CALL :%programName%
	pause
GOTO :EOF

:CD41_2018
	CALL :CD41
GOTO :EOF

:CD41_2019
	CALL :CD41
GOTO :EOF

:CD41
	for /f "tokens=*" %%A in ('dir /b /s /a-d "%ProgramPath%\%programName%\%dateFile%\*_CD391N.txt" "%ProgramPath%\%programName%\%dateFile%\*_CD533.txt" "%ProgramPath%\%programName%\%dateFile%\*_CD391C.txt" "%ProgramPath%\%programName%\%dateFile%\*_CD391E.txt"') do (
		copy "%%~A" "%BackupPath%\PAT BTT Text Files\%programName%\New\"
	)
GOTO :EOF

:CD42
	for /f "tokens=*" %%A in ('dir /b /s /a-d  "%ProgramPath%\%programName%\%dateFile%\*_CD539C.txt" "%ProgramPath%\%programName%\%dateFile%\*_CD539E_CD390.txt"') do (
		copy "%%~A" "%BackupPath%\PAT BTT Text Files\%programName%\New\"
	)
GOTO :EOF

:C520MCA
	for /f "tokens=*" %%A in ('dir /b /s /a-d "%ProgramPath%\%programName%\%dateFile%\*_C520CH_CCk.txt" "%ProgramPath%\%programName%\%dateFile%\*_C520EU_CBS.txt" "%ProgramPath%\%programName%\%dateFile%\*_C520NA.txt" "%ProgramPath%\%programName%\%dateFile%\*_C520TW_CCZ.txt"') do (
		copy "%%~A" "%BackupPath%\PAT BTT Text Files\%programName%\New\"
	)
GOTO :EOF

Open in new window


and that isn't even getting into making this even further streamlined, but I'll not bother as oDBA already did a rendition similar to what I would now that I'm looking at the actual tasks you're trying to accomplish.

 Still, if you need to truly manage Or conditions, as does sometimes happen these two methods work well.

And if you have a lot of cases, then Method 2 is best.

But if you have easily duplicable sets of actions then I'd double Loop it (refer to oDBA)
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
bhavani vAuthor Commented:
thanks very much expertssss
0
 
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
Glad to help, take a moment to assign a points split for the provided answers as you feel they have helped you.
0
 
bhavani vAuthor Commented:
one more doubt experts.
code:
Copy "%ProgramPath%\%programName%\%dateFile%\*.zip"  "%BackupPath%\%programName%\"

for the above code its possible to use for loop to check *.zip in the date file. then copy to the destination.

i tried this:
for /d %%A in ("%ProgramPath%\%programName%\%dateFile%\*.*") do (
    copy "%%~A\*.zip" "%BackupPath%\%programName%\"
      )

but its searching different inside the datefile.. can you guys help me for the code?
0
 
Ben Personick (Previously QCubed)Connect With a Mentor Lead Network EngineerCommented:
FOR /D will only match directories if you use wildcards.

For your purposes, it seems that you only need to use a normal for loop.


FOR %%A IN ("%ProgramPath%\%programName%\%dateFile%\*.Zip") DO (
     COPY "%%~A" "%BackupPath%\%programName%\"
)

Open in new window


furthermore, if you only want to see if any file with .zip exists then copy them all you can easily use IF with a wildcard (one of the rare times it's possible to use a wildcard in an IF, so worth pointing out, and faster)

IF EXIST "%ProgramPath%\%programName%\%dateFile%\*.zip" (
     COPY "%ProgramPath%\%programName%\%dateFile%\*.zip" "%BackupPath%\%programName%\"
)

Open in new window

0
 
bhavani vAuthor Commented:
its working but i would like to know the code like,

wherever the zip file is available inside the datefile , that *.zip file i need to consume and copy to destination.
your code is working only inside the datefile and i need also inside the subfolder also its possible to take the *.zip file.

thanks in advacne
1
 
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
Hello Bhavani v,

To clarify I believe you are saying the following:

You would like to look for all zip files in the "Datefile" Folder as well as any folders underneath the "Datefile" folder and copy all of them to your destination folder.

To do this you can do the following:

FOR /R "%ProgramPath%\%programName%\%dateFile%" %%A IN (*.zip) DO (
     COPY "%%~A" "%BackupPath%\%programName%\"
)

Open in new window

0
 
bhavani vAuthor Commented:
yes correct your are saying expert. thanks code is working..

can u explain this code which belongs \R and %%A ???
1
 
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
/R is an option it tells "FOR" to walk over a directory tree starting at the given directory, and match the terms present inside the criteria set.

%%A is a Loop variable.

it is used as a temporary placeholder for the results generated by the For Loop.
0
 
bhavani vAuthor Commented:
thank you expertsss
1
 
bhavani vAuthor Commented:
experts one issue got in that code:
:echo off
setlocal

set patternList=
if /i "%programName%"=="CD41_2018"      set patternList=*_CD391N.txt *_CD533.txt *_CD391C.txt *_CD391E.txt
if /i "%programName%"=="CD41_2019"      set patternList=*_CD391N.txt *_CD533.txt *_CD391C.txt *_CD391E.txt
if /i "%programName%"=="CD42"            set patternList=*_CD539C.txt *_CD539E_CD390.txt
if /i "%programName%"=="C520MCA"      set patternList=*_C520CH_CCk.txt *_C520EU_CBS.txt *_C520NA.txt *_C520TW_CCZ.txt
if not defined patternList goto :eof

:Loop
for /f "tokens=1*" %%a in ("%patternList%") do (set fileMask=%%a&set patternList=%%b)
echo Processing '%fileMask%' ...
for /f "tokens=*" %%a in ('dir /b /s /a-d "%ProgramPath%\%programName%\%dateFile%\%fileMask%"') do (
      echo Backing up '%%~nxa' ...
      ECHO copy "%%~a" "%BackupPath%\PAT BTT Text Files\%programName%\New\"
)
if defined patternList goto Loop
pause


if this bat file i will keep in my local then its backing up to the  destination. if i keep in some other drive means its not copying., so can u chck this code??
0
 
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
@Bhavi,

  You are not sharing your whole script with us.

  The script does not take the time to Define these variables:
  • %dateFile%
  • %ProgramPath%
  • %BackupPath%

  Understanding the full script would be important for trying to assist you.

  Also when you say "keep in my local" versus "Keep in some other drive" do you mean you are keeping the script in a mapped drive?

  Or are you trying to run the script on another server?

  How are you running the script?

  I also notice that you have this set as an ECHO, I amended it to echo the command and then run the command.

setlocal enabledelayedexpansion
echo off

set "patternList="
if /i "%programName%"=="CD41_2018"      set "patternList=*_CD391N.txt *_CD533.txt *_CD391C.txt *_CD391E.txt"
if /i "%programName%"=="CD41_2019"      set "patternList=*_CD391N.txt *_CD533.txt *_CD391C.txt *_CD391E.txt"
if /i "%programName%"=="CD42"            set "patternList=*_CD539C.txt *_CD539E_CD390.txt"
if /i "%programName%"=="C520MCA"      set "patternList=*_C520CH_CCk.txt *_C520EU_CBS.txt *_C520NA.txt *_C520TW_CCZ.txt"
if not defined patternList goto :eof

:Loop
for  %%a in ("%patternList%") do (
	set "fileMask=%%a"
	echo Processing '!fileMask!' ...
	for /f "tokens=*" %%a in ('dir /b /s /a-d "%ProgramPath%\%programName%\%dateFile%\!fileMask!"') do (
		echo Backing up '%%~nxa' ...
		set "_CopyCMD=copy "%%~a" "%BackupPath%\PAT BTT Text Files\%programName%\New\""
		ECHO !_CopyCMD!
		CALL !_CopyCMD!
	)
)
pause
GOTO :EOF

Open in new window

0
 
bhavani vAuthor Commented:
mapped drive expert
0
 
bhavani vAuthor Commented:
hi, i need to move the zip file then if moved completely then only i need to copy some files to the destination. so in my below code what i have to add ??

code:

SET /P programName=Enter vehicle program name :

SET ProgramPath=C:\FNASmartServices\REPORTS\BPNO

SET BackupPath=\\ecc9000203\proj\DI_PMTI_SE\PAT\PAT\PATFiles

setlocal EnableDelayedExpansion & time /T
date /T

FOR /R "%ProgramPath%\%programName%\%dateFile%" %%A IN (*.zip) DO (
     COPY "%%~A" "%BackupPath%\%programName%\"
       )
       if(*zip==
dir /a =%BackupPath%\%programName%\ /b > filename1.txt
rem Initialize variables
set OldestDate=999999
set OldestFile=

rem Read each line from list file
for /f "usebackq tokens=*" %%A in ("filename1.txt") do (

    rem Get right 6 characters of file name and arrange in YYMMDD format
    set FileName=%%~nA
    set FileDate=!FileName:~-2!!FileName:~-6,4!

     rem See if this is the oldest file so far, if so save it
    if "!FileDate!" LSS "!OldestDate!" (
        set OldestDate=!FileDate!
        set OldestFile=%%~A
    ))
rem Delete the oldest file
echo Deleting oldest file "%OldestFile%"
del "%BackupPath%\%programName%\%OldestFile%"
del "filename1.txt"
0
 
bhavani vAuthor Commented:
experts????
0
 
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
@Moderato

Bhavani is trying to use a ticket which is getting responses to answer multiple Questions.

  He had already posted that last portion as a separate question prior to posting it here.
  See Separate Q Posted on 2018/03/29 Here

  Then, four days later, as I was being helpful on his follow-up Qs here he posted it as the last response in this chain.

  Not only is the question already open separately, it is separate in scope and need from this question and needs further clarification from Bhavani V, in order to ever be answerable.

  This has been requested in the original question he cross-posted from by Bill Prew:

Expert Comment

by:Bill Prew
ID: 42523570
1d
I can't understand what you are asking based on:

zip file i need to move completely then only i need to copy some files and delete the old txt file and move the new txt file in no 2 code to the destination.

Please try added further explanation and break this down very clearly to what you need help with.  As worded I doubt any experts will know what you want, and won't be able to help as a result.

Furthermore, I spent a lot of time with Bhavi V answering follow-up questions he had which were tenuously related to the original question and spent a good deal of time on that assistance which he does indicate solved his problems with the similar scenarios.

By looking back through his question history he has an issue of repeatedly doing this sort of thing.. phpht.

In any case, I stand by my believe that the points should be split as follows:

Accepted Solution: Points 500 - oBdA ID: 42499161     2018-03-14 (Author originally accepted and offers one of several accepted approaches to the original Q)
Assisted Solution: Points 250 - QCubed ID: 42499240     2018-03-14 (Offers two of several accepted approaches to the original Q)
Assisted Solution: Points 250 - QCubed ID: 42505274     2018-03-20 (Answers Follow-up Qs which were directed at resolving an issue he had with oBdA's code)
1
 
bhavani vAuthor Commented:
close this question
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.