logo
down
shadow

if/then/else logic in batch file


if/then/else logic in batch file

By : user2953635
Date : November 21 2020, 01:01 AM
wish helps you I am writing a batch file that zips up MS Access 2010 files (file extension .accdb), renames the zip file with the date and time, then moves the zip file to a subfolder called Backups. The following shows the code so far:
code :
if exist *.laccdb (echo Send the message) else (echo continue the processing)
if exist *.laccdb (
 echo Send the message
 goto sendmessage
)
rem the file does not exist, so carry on with the ZIP operation
... your ZIP operation goes in here
goto :eof

:sendmessage
rem put whatever code you want to send the message in here.
goto :eof
if condition (
 dothis_if_true
) else (
 dothis_if_false
)


Share : facebook icon twitter icon
Batch File if giving unexpected results/weird logic

Batch File if giving unexpected results/weird logic


By : Vladislav Utkin
Date : March 29 2020, 07:55 AM
this will help The main problem is the set output =... you use a variable named output not output.
You should avoid (unneccessary) spaces in batch files.
code :
if "%~3"=="" (
    set filter=FullExclude.txt
    set output=USERDATA
) else (
    set "filter=%~3"
    set output=CustomDiffData
)
echo %output%
Batch file logic to delete files based on date in filename

Batch file logic to delete files based on date in filename


By : Nattha Abdulslam
Date : March 29 2020, 07:55 AM
it helps some times The Batch file below convert file date to Julian Day Number, that is a sequential number of days, and use it to know how many days old is each one. The number of days to delete files is given in the parameter.
code :
@echo off
setlocal EnableDelayedExpansion

rem Get Julian Day Number of today's date
rem The assumed format is MM/DD/YYYY, change C-A-B order in accordance with your locale
for /F "tokens=1-3 delims=/" %%a in ("%date%") do (
   set todayDate=%%c%%a%%b
)
call :DateToJDN %todayDate% todayJDN=

for /F "tokens=1-3 delims=_." %%a in ('dir /B /A-D *.*') do (
   call :DateToJDN %%b fileJDN=
   set /A daysOld=todayJDN - fileJDN
   if !daysOld! gtr %1 (
      echo File "%%a_%%b.%%c" is !daysOld! days old
   )
)
goto :EOF

:DateToJDN yyyymmdd jdn=
set yyyymmdd=%1
set /A yyyy=%yyyymmdd:~0,4%, mm=1%yyyymmdd:~4,2% %% 100, dd=1%yyyymmdd:~6% %% 100
set /A a=(mm-14)/12, %2=(1461*(yyyy+4800+a))/4+(367*(mm-2-12*a))/12-(3*((yyyy+4900+a)/100))/4+dd-32075
exit /B
I have a logic, I need the same logic to be written in a batch file (windows). Below is the code

I have a logic, I need the same logic to be written in a batch file (windows). Below is the code


By : magetogi
Date : November 22 2020, 12:01 PM
hop of those help? As understood from you, you want to transfer 5 files into each folder. Which means, files 1 to 5 are put into folder 1, 6 to 10 in folder 2, 11 to 15 in folder 3 and so on.
Try out this code, should work well with just one loop.
code :
@echo off
setlocal enabledelayedexpansion enableextensions

set total=45
set /a result=(total/num1)

set foldernum=0
FOR /L %%I IN (1,1,%total%) DO (
    set /A var=%%I %% 5
    if !var! == 1 (
        set /A foldernum=foldernum + 1
    )
    ECHO %%I And !foldernum!
)
pause
Batch file (ForFiles multiple conditional commands for logic)?

Batch file (ForFiles multiple conditional commands for logic)?


By : MrTit
Date : March 29 2020, 07:55 AM
it fixes the issue Yes, nesting if commands in their then branches is the way how-to have logical AND. Note proper quoting in next code snippet:
code :
@ECHO ON >NUL
@SETLOCAL enableextensions
set "name=SO"
set "nam2=SU"
pushd "D:\VB_scripts"
@rem all directories 
ForFiles /D -2 /C "CMD /C if @ISDIR==TRUE echo @FILE"
@rem all directories except "SO" 
ForFiles /D -2 /C "CMD /C if @ISDIR==TRUE if not @FNAME=="""%name%""" echo @FILE"
@rem all directories except "SO" and "SU" 
ForFiles /D -2 /C "CMD /C if @ISDIR==TRUE if not @FNAME=="""%name%""" if not @FNAME=="""%nam2%""" echo @FILE"
popd
@ENDLOCAL
==>D:\bat\SO\31346676.bat

==>set "name=SO"

==>set "nam2=SU"

==>pushd "D:\VB_scripts"

==>ForFiles /D -2 /C "CMD /C if @ISDIR==TRUE echo @FILE"

"Class Pack"
"Oldies"
"SO"
"SU"
"WMI"

==>ForFiles /D -2 /C "CMD /C if @ISDIR==TRUE if not @FNAME=="""SO""" echo @FILE"

"Class Pack"
"Oldies"
"SU"
"WMI"

==>ForFiles /D -2 /C "CMD /C if @ISDIR==TRUE if not @FNAME=="""SO""" if not @FNAME=="
""SU""" echo @FILE"

"Class Pack"
"Oldies"
"WMI"

==>popd
Dynamically apply custom logic on every item when creating a batch file in spring batch

Dynamically apply custom logic on every item when creating a batch file in spring batch


By : user2523857
Date : March 29 2020, 07:55 AM
wish help you to fix your issue The LineAggregator strategy interface allows you to define how to aggregate an item into a line to be written to the output file. You can implement your custom logic in there. Here is a quick example:
code :
static class Person {
    private int id;
    private String name;
    private Gender gender;
    enum Gender {
        M, F;
    }
}

class PersonLineAggregator implements LineAggregator<Person> {
    @Override
    public String aggregate(Person person) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(person.id).append(",");
        stringBuilder.append(person.name).append(",");
        stringBuilder.append(person.gender.equals(Person.Gender.M) ? 1 : 0);
        return stringBuilder.toString();
    }
}
@Bean
public FlatFileItemWriter<Person> personFlatFileItemWriter() {
    return new FlatFileItemWriterBuilder<Person>()
            .name("personWriter")
            .resource(new FileSystemResource("persons.csv"))
            .lineAggregator(new PersonLineAggregator())
            .headerCallback(writer -> writer.write("id,name,code"))
            .build();
}
shadow
Privacy Policy - Terms - Contact Us © ourworld-yourmove.org