Link to home
Start Free TrialLog in
Avatar of sasi v
sasi v

asked on

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??
Avatar of Lee W, MVP
Lee W, MVP
Flag of United States of America image

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%.
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sasi v
sasi v

ASKER

thanks very much expertssss
Glad to help, take a moment to assign a points split for the provided answers as you feel they have helped you.
Avatar of sasi v

ASKER

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?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sasi v

ASKER

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
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

Avatar of sasi v

ASKER

yes correct your are saying expert. thanks code is working..

can u explain this code which belongs \R and %%A ???
/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.
Avatar of sasi v

ASKER

thank you expertsss
Avatar of sasi v

ASKER

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??
@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

Avatar of sasi v

ASKER

mapped drive expert
Avatar of sasi v

ASKER

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"
Avatar of sasi v

ASKER

experts????
@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)
Avatar of sasi v

ASKER

close this question