Link to home
Start Free TrialLog in
Avatar of Luis Diaz
Luis DiazFlag for Colombia

asked on

Windows batch: flag to call API

Hello experts,

The following procedure allows me to transfer specific files from temp to requests folder and launch API java.

echo off
setlocal EnableDelayedExpansion

set ROOT_DIR=C:\API
set APP_NAME=api-name
rem set APP_VERSION=1.0.1
set MODE=2

rem Setting folders path
set APP_PATH=%ROOT_DIR%\%APP_NAME%
set APP_PATH_FOLDERS=%ROOT_DIR%\folders-%APP_NAME%

set BIN_DIR= %APP_PATH%\bin
set LIB_DIR=%APP_PATH%\lib
set CONF_DIR=%APP_PATH%\conf
set VAR_DIR=%APP_PATH%\var
set LOG_DIR=%APP_PATH%\log
set FLG_DIR=%APP_PATH%\flag
cd %BIN_DIR%
set BaseDir=%APP_PATH_FOLDERS%\CostCodes2\Temp
set DestDir=%APP_PATH_FOLDERS%\CostCodes2\Requests
set MaxFiles=18

call :GetStamp

set StampInitial=%Stamp%
set LogFile=%LOG_DIR%\C1_%StampInitial%_MOVE.log

echo %Stamp%: Base folder is "%BaseDir%">>"%LogFile%"
call :GetStamp
echo %Stamp%: Destination folder is "%DestDir%">>"%LogFile%"

set i=0
for /f "tokens=*" %%A in ('dir /b /o-d /a-d "%BaseDir%\*.csv"') do (
    
	if !i! GEQ %MaxFiles% goto :Movedone
    move "%BaseDir%\%%~A" "%DestDir%">NUL
    call :GetStamp
    echo !Stamp!: Moved file "%%~A">>"%LogFile%"
	set /a i+=1
	
)

:Movedone
call :GetStamp
echo %Stamp%: Total files moved = %i% >>"%LogFile%"

if !i! EQU 0 GOTO EXITNOFILE


rem --------------------------------------
rem Call API java
rem --------------------------------------

set JAVA_ARGS=-showversion -Dlog4j.overwrite=true -Xmx4096m -Duse_description=true
java %JAVA_ARGS% -jar "..\lib\mstt-synchro-cost-codes-2.jar" "..\conf\psconnect.properties" C:\API\folders-mstt-synchro-cost-codes-2\CostCodes2\


exit /b

:EXITNOFILE
echo %Stamp%: no file moved, exit 1 >>"%LogFile%"

exit 1

:GetStamp
  rem Get current date in YYYYMMDD_hhmmss format
  set Stamp=
  for /f "tokens=* skip=1" %%A in ('wmic os get LocalDateTime') do (
    if not defined Stamp (
      set Stamp=%%A
      set Stamp=!Stamp:~0,8!_!Stamp:~8,6!
      exit /b
    )
  )
  exit /b
  
exit

Open in new window


I would like to add the following requirement:
Add a TempFlag. If TempFlag equals to 1 execute the procedure as is transfer specific number of files from temp to requests folder and do the other actions.
If TempFlag is equal to 0 check the number of files available in requests folder (avoid the previous temp to request folder actions). If equal to 0 exit and log this action
If not log the number available in requests folder and go to API java lines to call the API java.
If you have questions, please contact me.
Avatar of Bill Prew
Bill Prew

Give this a try.

echo off
setlocal EnableDelayedExpansion

set ROOT_DIR=C:\API
set APP_NAME=api-name
rem set APP_VERSION=1.0.1
set MODE=2
set TempFlag=1

rem Setting folders path
set APP_PATH=%ROOT_DIR%\%APP_NAME%
set APP_PATH_FOLDERS=%ROOT_DIR%\folders-%APP_NAME%

set BIN_DIR= %APP_PATH%\bin
set LIB_DIR=%APP_PATH%\lib
set CONF_DIR=%APP_PATH%\conf
set VAR_DIR=%APP_PATH%\var
set LOG_DIR=%APP_PATH%\log
set FLG_DIR=%APP_PATH%\flag
cd %BIN_DIR%
set BaseDir=%APP_PATH_FOLDERS%\CostCodes2\Temp
set DestDir=%APP_PATH_FOLDERS%\CostCodes2\Requests
set MaxFiles=18

call :GetStamp
set LogFile=%LOG_DIR%\C1_%Stamp%_MOVE.log

echo %Stamp%: Base folder is "%BaseDir%">>"%LogFile%"

call :GetStamp
echo %Stamp%: Destination folder is "%DestDir%">>"%LogFile%"

set i=0
for /f "tokens=*" %%A in ('dir /b /o-d /a-d "%BaseDir%\*.csv"') do (
    
    set /a i+=1

    if %TempFlag% EQU 0 (
	    if !i! GTR %MaxFiles% goto :Movedone
        move "%BaseDir%\%%~A" "%DestDir%">NUL
        call :GetStamp
        echo !Stamp!: Moved file "%%~A">>"%LogFile%"
    )
	
)

:Movedone
call :GetStamp
echo %Stamp%: Total files moved = %i% >>"%LogFile%"

if !i! EQU 0 (
    call :GetStamp
    echo %Stamp%: no file moved, exit 1 >>"%LogFile%"
    exit 1
)

if %TempFlag% EQU 1 (
    rem --------------------------------------
    rem Call API java
    rem --------------------------------------
    set JAVA_ARGS=-showversion -Dlog4j.overwrite=true -Xmx4096m -Duse_description=true
    java %JAVA_ARGS% -jar "..\lib\mstt-synchro-cost-codes-2.jar" "..\conf\psconnect.properties" C:\API\folders-mstt-synchro-cost-codes-2\CostCodes2\
)

exit /b

:GetStamp
    rem Get current date in YYYYMMDD_hhmmss format
    set Stamp=
    for /f "tokens=* skip=1" %%A in ('wmic os get LocalDateTime') do (
        if not defined Stamp (
            set Stamp=%%A
            set Stamp=!Stamp:~0,8!_!Stamp:~8,6!
            exit /b
        )
    )
    exit /b
exit

Open in new window


»bp
Avatar of Luis Diaz

ASKER

Thank you Bill, I will test it and keep you informed.
Just one clarification Bill, if TempFlag mode is on =1 temp to request transfer should be done but also continue with API action.  API action should be done  no matter theTemFlag mode value. Expect the cases when Temp folder is empty with TempFlag mode on and when request folder is empty when TempFlag mode is off.
Is that the case?
I think this is closer to what you are after.

echo off
setlocal EnableDelayedExpansion

set ROOT_DIR=C:\API
set APP_NAME=api-name
rem set APP_VERSION=1.0.1
set MODE=2
set TempFlag=1

rem Setting folders path
set APP_PATH=%ROOT_DIR%\%APP_NAME%
set APP_PATH_FOLDERS=%ROOT_DIR%\folders-%APP_NAME%

set BIN_DIR= %APP_PATH%\bin
set LIB_DIR=%APP_PATH%\lib
set CONF_DIR=%APP_PATH%\conf
set VAR_DIR=%APP_PATH%\var
set LOG_DIR=%APP_PATH%\log
set FLG_DIR=%APP_PATH%\flag
cd %BIN_DIR%
set BaseDir=%APP_PATH_FOLDERS%\CostCodes2\Temp
set DestDir=%APP_PATH_FOLDERS%\CostCodes2\Requests
set MaxFiles=18

call :GetStamp
set LogFile=%LOG_DIR%\C1_%Stamp%_MOVE.log

echo %Stamp%: Base folder is "%BaseDir%">>"%LogFile%"

call :GetStamp
echo %Stamp%: Destination folder is "%DestDir%">>"%LogFile%"

set i=0
for /f "tokens=*" %%A in ('dir /b /o-d /a-d "%BaseDir%\*.csv"') do (
    
    set /a i+=1

    if %TempFlag% EQU 1 (
	    if !i! GTR %MaxFiles% goto :Movedone
        move "%BaseDir%\%%~A" "%DestDir%">NUL
        call :GetStamp
        echo !Stamp!: Moved file "%%~A">>"%LogFile%"
    )
	
)

:Movedone
if !i! EQU 0 (
    call :GetStamp
    echo %Stamp%: no file moved, exit 1 >>"%LogFile%"
    exit 1
) else (
    call :GetStamp
    echo %Stamp%: Total files moved = %i% >>"%LogFile%"
)

rem --------------------------------------
rem Call API java
rem --------------------------------------
set JAVA_ARGS=-showversion -Dlog4j.overwrite=true -Xmx4096m -Duse_description=true
java %JAVA_ARGS% -jar "..\lib\mstt-synchro-cost-codes-2.jar" "..\conf\psconnect.properties" C:\API\folders-mstt-synchro-cost-codes-2\CostCodes2\

exit /b

:GetStamp
    rem Get current date in YYYYMMDD_hhmmss format
    set Stamp=
    for /f "tokens=* skip=1" %%A in ('wmic os get LocalDateTime') do (
        if not defined Stamp (
            set Stamp=%%A
            set Stamp=!Stamp:~0,8!_!Stamp:~8,6!
            exit /b
        )
    )
    exit /b
exit

Open in new window


»bp
Bill,

I tested and I have a little issue:
1-When I put TempFlagMode=0 and requests folder is empty API action are done. however this shouldn't be the case.
When TempFlagMode=0 primary check should be if they are files at requests folder if not exit and log:
20191101_211434: Requests folder is empty unable to proceed
Else continue with API actions.
The part related to TempFlagMode=1 is correct.
I checked and the difference are the following:
User generated imageCould you please explain me the difference (if it is ok with you :-)):
>GTR vs GEQ and the impact placing set /a i+=1 at the beginning or the end of the statement

Thank you in advance for your help.
1-When I put TempFlagMode=0 and requests folder is empty API action are done.

Because of the following logic that is always executed regardless of TempFlag value I don't see any way that the API is invoked when there are no files found.

:Movedone
if !i! EQU 0 (
    call :GetStamp
    echo %Stamp%: no file moved, exit 1 >>"%LogFile%"
    exit 1
) else (
    call :GetStamp
    echo %Stamp%: Total files moved = %i% >>"%LogFile%"
)

Open in new window

Could you please explain me the difference (if it is ok with you :-)):
>GTR vs GEQ and the impact placing set /a i+=1 at the beginning or the end of the statement

That's a personal preference thing, I always like to increment at the start of the loop rather than the end, just makes more sense to me.  As a result though the logic in the loop has to be aware of that and act accordingly.

In your case the first iteration of the loop will use a value of i  of 0.  In my case since I add one to it at the top of the loop, then the first iteration will have a useful value of i  of 1.  As a result, to execute the same number of times in one case we use >, in the other >=.


»bp
Thank you Bill for your comment.

Concerning your previous comment:

https://www.experts-exchange.com/questions/29162811/Windows-batch-flag-to-call-API.html#a42972237.


The aim of TempFlagMode =0 is to by pass all the action related to Temp>Requests folder and process go to API invocation which aim is to process all files putted in requests folder.
As a result I expect to put all csv files in Requests folder and call API when TempFlagMode=0.
Concerning TempFlagMode=1 I expect to keep the existing process when TempFlagMode = 1
> Transfer a limit of files from temp to requests and call the API java.

How can I make work the TempFlagMode = 0 with the following sequence:
Transfer csv files and expect that procedure check files located at requests folder (and not at temp folder) and if it is not empty go to API java?

Thank you in advance for your help.
So, does this indicate what you want?  Is there any other processing difference between the two modes?

Mode 0:
  • no files get moved from Temp to Requests.
  • only execute the API if there are files in Temp.
Mode 1:
  • files get moved from Temp to Requests, up to MaxFiles.
  • only execute the API if there are files in Requests.


»bp
Mode 0:
No files get moved from Temp to Requests.
only execute the API if there are files at Request folder. If no files at Request folder go to end and exit.
Mode 1
Files get moved from Temp to Requests, up to MaxFiles and execute API. If no files at Temp folder go to end and exit.

Mode 0 acts exclusively at request folder.
Mode 1
Fies get moved from Temp to Requests, up to MaxFiles and execute API. If no files at Temp folder go to end and exit.

So is the check for files in Temp folder done before or after any are moved to Requests?  For example, I can imagine a scene where there are 5 files in Temp and 0 in Request at start of a Mode 1 execution.  The 5 files will be moved from Temp to Requests, and no longer in Temp at that point.  Do we exit, or call API process?


»bp
Mode 0:
Check is done prior to the transfer.
We call the API as long as there is a transfer. API is not called as long as there is not a transfer or Temp folder is empty.
Mode 1:
API is called as long as there are files in request folder no matter the number of files. The unique case when API is not called when request folder is empty.
Let me know if it is clearer.
First you said:

Mode 0:
No files get moved from Temp to Requests.
only execute the API if there are files at Request folder. If no files at Request folder go to end and exit.

Which sounded like in mode 0 we were checking the Requests folder.

Now you said:

Mode 0:
Check is done prior to the transfer.
We call the API as long as there is a transfer. API is not called as long as there is not a transfer or Temp folder is empty.

Which indicates checking file count in Temp.

Honestly I'm still confused on the logic you want the script to follow...


»bp
Sorry Bill.
I made a mistake in my previous comment.
Here is another approach to make me understand.
Mode 0 (just actions at request folder)
No actions from temp to request.
Just check request folder, if empty exit. If files are at request just call API that is all for Mode 0.
Mode 1 (temp to request transfer)
Actions from temp to request folder based on threshold number of files (as is).
Check exclusively at temp folder, if temp folder is empty exit and don't call APi java. If temp folder is not empty do the transfer from temp to request folder and call API.
Hope it is clearer, if not let me know.
Okay, this should do what you described last.

@echo off
setlocal EnableDelayedExpansion

set ROOT_DIR=C:\API
set APP_NAME=api-name
rem set APP_VERSION=1.0.1
set MODE=2
set TempFlag=1

rem Setting folders path
set APP_PATH=%ROOT_DIR%\%APP_NAME%
set APP_PATH_FOLDERS=%ROOT_DIR%\folders-%APP_NAME%

set BIN_DIR= %APP_PATH%\bin
set LIB_DIR=%APP_PATH%\lib
set CONF_DIR=%APP_PATH%\conf
set VAR_DIR=%APP_PATH%\var
set LOG_DIR=%APP_PATH%\log
set FLG_DIR=%APP_PATH%\flag

set BaseDir=%APP_PATH_FOLDERS%\CostCodes2\Temp
set DestDir=%APP_PATH_FOLDERS%\CostCodes2\Requests
set MaxFiles=18

cd %BIN_DIR%

call :GetStamp
set LogFile=%LOG_DIR%\C1_%Stamp%_MOVE.log

call :GetStamp
echo %Stamp%: Base folder is "%BaseDir%">>"%LogFile%"

call :GetStamp
echo %Stamp%: Destination folder is "%DestDir%">>"%LogFile%"

if "%TempFlag%" EQU "0" (
    call :DoMode0
) else (
    if "%TempFlag%" EQU "1" (
        call :DoMode1
    ) else (
        call :GetStamp
        echo %Stamp%: Invalid value for TempFlag ("%TempFlag"), must be "0" or "1".>>"%LogFile%"
    )
)

:DoMode0
    if exist "%DestDir%\*.csv" (
        call :CallAPI
    )
    exit /b

:DoMode1
    if exist "%BaseDir%\*.csv" (
        call :MoveFiles
        call :CallAPI
    )
    exit /b

:MoveFiles
    set i=0
    for /f "tokens=*" %%A in ('dir /b /o-d /a-d "%BaseDir%\*.csv"') do (
        set /a i+=1
        if !i! GTR %MaxFiles% goto :Movedone
        move "%BaseDir%\%%~A" "%DestDir%">NUL
        call :GetStamp
        echo !Stamp!: Moved file "%%~A">>"%LogFile%"
    )
    :Movedone
    if !i! EQU 0 (
        call :GetStamp
        echo %Stamp%: no files moved, exit 1 >>"%LogFile%"
        exit 1
    ) else (
        call :GetStamp
        echo %Stamp%: Total files moved = %i% >>"%LogFile%"
    )
    exit /b

:CallAPI
    rem --------------------------------------
    rem Call API java
    rem --------------------------------------
    set JAVA_ARGS=-showversion -Dlog4j.overwrite=true -Xmx4096m -Duse_description=true
    java %JAVA_ARGS% -jar "..\lib\mstt-synchro-cost-codes-2.jar" "..\conf\psconnect.properties" C:\API\folders-mstt-synchro-cost-codes-2\CostCodes2\
    exit /b

:GetStamp
    rem Get current date in YYYYMMDD_hhmmss format
    set Stamp=
    for /f "tokens=* skip=1" %%A in ('wmic os get LocalDateTime') do (
        if not defined Stamp (
            set Stamp=%%A
            set Stamp=!Stamp:~0,8!_!Stamp:~8,6!
            exit /b
        )
    )
    exit /b

Open in new window


»bp
Thank you Bill I will test it and keep you informed.
Bill,
I tested and it works.
I have just two little issues with TempFlagMode=1
1)If temp folder is empty APIJAVA is called which shouldn't be the case:
Log content report the following:
20191106_093224: Base folder is "C:\API\folders-mstt-synchro-cost-codes-2\CostCodes2\Temp"
20191106_093225: Destination folder is "C:\API\folders-mstt-synchro-cost-codes-2\CostCodes2\Requests"

Open in new window


2)When I set up MaxFile=18 and they are 19 files in temp folder, I got the moved of the 18 files (which is ok) however log content report the following:
20191106_092920: Base folder is "C:\API\folders-mstt-synchro-cost-codes-2\CostCodes2\Temp"
20191106_092921: Destination folder is "C:\API\folders-mstt-synchro-cost-codes-2\CostCodes2\Requests"
20191106_092921: Moved file "ImportCC_Req139257.csv"
20191106_092922: Moved file "ImportCC_Req139262.csv"
20191106_092922: Moved file "ImportCC_Req139261.csv"
20191106_092923: Moved file "ImportCC_Req139260.csv"
20191106_092923: Moved file "ImportCC_Req139258.csv"
20191106_092923: Moved file "ImportCC_Req139256.csv"
20191106_092924: Moved file "ImportCC_Req139083.csv"
20191106_092924: Moved file "ImportCC_Req139074.csv"
20191106_092925: Moved file "ImportCC_Req139264.csv"
20191106_092925: Moved file "ImportCC_Req139090.csv"
20191106_092926: Moved file "ImportCC_Req139089.csv"
20191106_092926: Moved file "ImportCC_Req139088.csv"
20191106_092926: Moved file "ImportCC_Req139087.csv"
20191106_092927: Moved file "ImportCC_Req139086.csv"
20191106_092927: Moved file "ImportCC_Req139085.csv"
20191106_092928: Moved file "ImportCC_Req139084.csv"
20191106_092928: Moved file "ImportCC_Req139263.csv"
20191106_092928: Moved file "ImportCC_Req139259.csv"
20191106_092928: Total files moved = 19 

Open in new window

18 files are moved however I got a last line which report Total files moved = 19.
Probably we should I just:
:MoveFiles
    set i=0
    for /f "tokens=*" %%A in ('dir /b /o-d /a-d "%BaseDir%\*.csv"') do (
        set /a i+=1
        if !i! GTR %MaxFiles% goto :Movedone
        move "%BaseDir%\%%~A" "%DestDir%">NUL
        call :GetStamp
        echo !Stamp!: Moved file "%%~A">>"%LogFile%"
    )

Open in new window

to:
:MoveFiles
    set i=0
    for /f "tokens=*" %%A in ('dir /b /o-d /a-d "%BaseDir%\*.csv"') do (
        if !i! GEQ %MaxFiles% goto :Movedone
        move "%BaseDir%\%%~A" "%DestDir%">NUL
        call :GetStamp
        echo !Stamp!: Moved file "%%~A">>"%LogFile%"
        set /a i+=1
    )

Open in new window

Let me know what do you think.
Thank you in advance for your help.
This should correct the total files copied logging.

@echo off
setlocal EnableDelayedExpansion

set ROOT_DIR=C:\API
set APP_NAME=api-name
rem set APP_VERSION=1.0.1
set MODE=2
set TempFlag=1

rem Setting folders path
set APP_PATH=%ROOT_DIR%\%APP_NAME%
set APP_PATH_FOLDERS=%ROOT_DIR%\folders-%APP_NAME%

set BIN_DIR= %APP_PATH%\bin
set LIB_DIR=%APP_PATH%\lib
set CONF_DIR=%APP_PATH%\conf
set VAR_DIR=%APP_PATH%\var
set LOG_DIR=%APP_PATH%\log
set FLG_DIR=%APP_PATH%\flag

set BaseDir=%APP_PATH_FOLDERS%\CostCodes2\Temp
set DestDir=%APP_PATH_FOLDERS%\CostCodes2\Requests
set MaxFiles=18

cd %BIN_DIR%

call :GetStamp
set LogFile=%LOG_DIR%\C1_%Stamp%_MOVE.log

call :GetStamp
echo %Stamp%: Base folder is "%BaseDir%">>"%LogFile%"

call :GetStamp
echo %Stamp%: Destination folder is "%DestDir%">>"%LogFile%"

if "%TempFlag%"" EQU "0" (
    call :DoMode0
) else (
    if "%TempFlag%"" EQU "1" (
        call :DoMode1
    ) else (
        call :GetStamp
        echo %Stamp%: Invalid value for TempFlag ("%TempFlag"), must be "0" or "1".>>"%LogFile%"
    )
)

:DoMode0
    if exist "%DestDir%\*.csv" (
        call :CallAPI
    )
    exit /b

:DoMode1
    if exist "%BaseDir%\*.csv" (
        call :MoveFiles
        call :CallAPI
    )
    exit /b

:MoveFiles
    set i=0
    for /f "tokens=*" %%A in ('dir /b /o-d /a-d "%BaseDir%\*.csv"') do (
        set /a i+=1
        if !i! GTR %MaxFiles% goto :Movedone
        move "%BaseDir%\%%~A" "%DestDir%">NUL
        call :GetStamp
        echo !Stamp!: Moved file "%%~A">>"%LogFile%"
    )
    :Movedone
    if !i! EQU 0 (
        call :GetStamp
        echo %Stamp%: no files moved, exit 1 >>"%LogFile%"
        exit 1
    ) else (
        set /a i-=1
        call :GetStamp
        echo %Stamp%: Total files moved = %i% >>"%LogFile%"
    )
    exit /b

:CallAPI
    rem --------------------------------------
    rem Call API java
    rem --------------------------------------
    set JAVA_ARGS=-showversion -Dlog4j.overwrite=true -Xmx4096m -Duse_description=true
    java %JAVA_ARGS% -jar "..\lib\mstt-synchro-cost-codes-2.jar" "..\conf\psconnect.properties" C:\API\folders-mstt-synchro-cost-codes-2\CostCodes2\
    exit /b

:GetStamp
    rem Get current date in YYYYMMDD_hhmmss format
    set Stamp=
    for /f "tokens=* skip=1" %%A in ('wmic os get LocalDateTime') do (
        if not defined Stamp (
            set Stamp=%%A
            set Stamp=!Stamp:~0,8!_!Stamp:~8,6!
            exit /b
        )
    )
    exit /b

Open in new window


»bp
Thank you Bill, I will test it and keep you informed.
Bill,

I tested but I got the following:

Microsoft Windows [Version 6.3.9600]
(c) 2013 Microsoft Corporation. All rights reserved.
The syntax of the command is incorrect.
C:\API\mstt-synchro-cost-codes-2\bin>if "1"" EQU "0" (
C:\API\mstt-synchro-cost-codes-2\bin>m

Open in new window


"%TempFlag%
""
: extra quote, I don't know if just this cause the problem.
Thank you in advance for your help.
Yes, not sure how that got in there but remove that extra double quote.


»bp
Thank you Bill.
I removed and I am still getting the wrong number of files transferred for TempFlagMode=1

20191106_191621: Base folder is "C:\API\folders-mstt-synchro-cost-codes-2\CostCodes2\Temp"
20191106_191622: Destination folder is "C:\API\folders-mstt-synchro-cost-codes-2\CostCodes2\Requests"
20191106_191622: Moved file "ImportCC_Req139366.csv"
20191106_191623: Moved file "ImportCC_Req139364.csv"
20191106_191623: Moved file "ImportCC_Req139353.csv"
20191106_191623: Moved file "ImportCC_Req139283.csv"
20191106_191624: Moved file "ImportCC_Req139282.csv"
20191106_191624: Moved file "ImportCC_Req139281.csv"
20191106_191624: Moved file "ImportCC_Req139279.csv"
20191106_191625: Moved file "ImportCC_Req139278.csv"
20191106_191625: Moved file "ImportCC_Req139277.csv"
20191106_191625: Moved file "ImportCC_Req139276.csv"
20191106_191626: Moved file "ImportCC_Req139274.csv"
20191106_191626: Moved file "ImportCC_Req139273.csv"
20191106_191626: Moved file "ImportCC_Req139261.csv"
20191106_191627: Moved file "ImportCC_Req139260.csv"
20191106_191627: Moved file "ImportCC_Req139258.csv"
20191106_191627: Moved file "ImportCC_Req139257.csv"
20191106_191628: Moved file "ImportCC_Req139256.csv"
20191106_191628: Moved file "ImportCC_Req139083.csv"
20191106_191628: Total files moved = 19 

Open in new window


Thank you in advance for your help.
Okay, try this.

@echo off
setlocal EnableDelayedExpansion

set ROOT_DIR=C:\API
set APP_NAME=api-name
rem set APP_VERSION=1.0.1
set MODE=2
set TempFlag=1

rem Setting folders path
set APP_PATH=%ROOT_DIR%\%APP_NAME%
set APP_PATH_FOLDERS=%ROOT_DIR%\folders-%APP_NAME%

set BIN_DIR=%APP_PATH%\bin
set LIB_DIR=%APP_PATH%\lib
set CONF_DIR=%APP_PATH%\conf
set VAR_DIR=%APP_PATH%\var
set LOG_DIR=%APP_PATH%\log
set FLG_DIR=%APP_PATH%\flag

set BaseDir=%APP_PATH_FOLDERS%\CostCodes2\Temp
set DestDir=%APP_PATH_FOLDERS%\CostCodes2\Requests
set MaxFiles=18

cd %BIN_DIR%

call :GetStamp
set LogFile=%LOG_DIR%\C1_%Stamp%_MOVE.log

call :GetStamp
echo %Stamp%: Base folder is "%BaseDir%">>"%LogFile%"

call :GetStamp
echo %Stamp%: Destination folder is "%DestDir%">>"%LogFile%"

if "%TempFlag%" EQU "0" (
    call :DoMode0
) else (
    if "%TempFlag%" EQU "1" (
        call :DoMode1
    ) else (
        call :GetStamp
        echo %Stamp%: Invalid value for TempFlag ("%TempFlag"), must be "0" or "1".>>"%LogFile%"
    )
)

:DoMode0
    if exist "%DestDir%\*.csv" (
        call :CallAPI
    )
    exit /b

:DoMode1
    if exist "%BaseDir%\*.csv" (
        call :MoveFiles
        call :CallAPI
    )
    exit /b

:MoveFiles
    set i=0
    for /f "tokens=*" %%A in ('dir /b /o-d /a-d "%BaseDir%\*.csv"') do (
        if !i! GEQ %MaxFiles% goto :Movedone
        move "%BaseDir%\%%~A" "%DestDir%">NUL
        call :GetStamp
        echo !Stamp!: Moved file "%%~A">>"%LogFile%"
        set /a i+=1
    )

    :Movedone
    if !i! EQU 0 (
        call :GetStamp
        echo %Stamp%: no files moved, exit 1 >>"%LogFile%"
        exit 1
    ) else (
        call :GetStamp
        echo %Stamp%: Total files moved = !i! >>"%LogFile%"
    )
    exit /b

:CallAPI
    rem --------------------------------------
    rem Call API java
    rem --------------------------------------
    ECHO set JAVA_ARGS=-showversion -Dlog4j.overwrite=true -Xmx4096m -Duse_description=true
    ECHO java %JAVA_ARGS% -jar "..\lib\mstt-synchro-cost-codes-2.jar" "..\conf\psconnect.properties" C:\API\folders-mstt-synchro-cost-codes-2\CostCodes2\
    exit /b

:GetStamp
    rem Get current date in YYYYMMDD_hhmmss format
    set Stamp=
    for /f "tokens=* skip=1" %%A in ('wmic os get LocalDateTime') do (
        if not defined Stamp (
            set Stamp=%%A
            set Stamp=!Stamp:~0,8!_!Stamp:~8,6!
            exit /b
        )
    )
    exit /b

Open in new window


»bp
Thank you Bill, I will test it and keep you informed.
Bill, I tested and know I have the proper count in the log.

>However API is called when temp folder is empty for TempFlagMode =1 which shouldn’t be the case.
>For TempFlagMode=0, I got the following message when files are at request folder.
C:\API\mstt-synchro-cost-codes-2\bin>if "0" EQU "0" (call :DoMode0 )  else (if "0" EQU "1" (call :DoMode1 )  else (
call :GetStamp
 echo 20191107_091820: Invalid value for TempFlag ("LogFile"
) )

Open in new window

Thank you for your help.
>However API is called when temp folder is empty for TempFlagMode =1 which shouldn’t be the case.
Currently the Mode=1 logic looks like:

:DoMode1
    if exist "%BaseDir%\*.csv" (
        call :MoveFiles
        call :CallAPI
    )
    exit /b


So as you can see, if the Temp folder starts out with no CSV files in it, then neither the move logic or the API call are executed.  It is possible of course that if there were files in Temp, and then the move files moves them all to the Requests folder that the API is still called, is that the problem you are seeing?  And then we would need to add another check after MoveFiles is called to see if Temp became empty?



>For TempFlagMode=0, I got the following message when files are at request folder.
It looks like this like:

echo %Stamp%: Invalid value for TempFlag ("%TempFlag"), must be "0" or "1".>>"%LogFile%"

is missing a % symbol, change it to:

echo %Stamp%: Invalid value for TempFlag ("%TempFlag%"), must be "0" or "1".>>"%LogFile%"


»bp
Thank you Bill,

I retested and I have a little issue concerning TempFlagMode=1

C:\API\mstt-synchro-cost-codes-2\bin>echo 20191107_184946: Destination folder is "C:\API\folders-mstt-synchro-cost-codes-2\CostCodes2\Requests" 1>>"C:\API\mstt-synchro-cost-codes-2\log\C1_20191107_184
945_MOVE.log"
must was unexpected at this time.
C:\API\mstt-synchro-cost-codes-2\bin>        echo 20191107_184946: Invalid value for TempFlag ("0"), must be "0" or "1".>>"C:\API\mstt-synchro-cost-codes-2\log\C1_20191107_184945_MOVE.log"
C:\API\mstt-synchro-cost-codes-2\bin>

Open in new window

I don't understand the line:
must was unexpected at this time.

Concerning TempFlagMode=0 I retested and you were right, API it doesn’t called, when I made the test I putted files <>.csv this is why API was called. We are going to leave as it.

And then we would need to add another check after MoveFiles is called to see if Temp became empty?
We don’t need this because Temp folder can still with files after the moved as there is a MaxFile variable setup.

Here is the testing version:

rem @echo off
setlocal EnableDelayedExpansion

set ROOT_DIR=C:\API
set APP_NAME=mstt-synchro-cost-codes-2
rem set APP_VERSION=1.0.1
set MODE=2
set TempFlag=0

rem Setting folders path
set APP_PATH=%ROOT_DIR%\%APP_NAME%
set APP_PATH_FOLDERS=%ROOT_DIR%\folders-%APP_NAME%

set BIN_DIR=%APP_PATH%\bin
set LIB_DIR=%APP_PATH%\lib
set CONF_DIR=%APP_PATH%\conf
set VAR_DIR=%APP_PATH%\var
set LOG_DIR=%APP_PATH%\log
set FLG_DIR=%APP_PATH%\flag

set BaseDir=%APP_PATH_FOLDERS%\CostCodes2\Temp
set DestDir=%APP_PATH_FOLDERS%\CostCodes2\Requests
set MaxFiles=18

cd %BIN_DIR%

call :GetStamp
set LogFile=%LOG_DIR%\C1_%Stamp%_MOVE.log

call :GetStamp
echo %Stamp%: Base folder is "%BaseDir%">>"%LogFile%"

call :GetStamp
echo %Stamp%: Destination folder is "%DestDir%">>"%LogFile%"

if "%TempFlag%" EQU "0" (
    call :DoMode0
) else (
    if "%TempFlag%" EQU "1" (
        call :DoMode1
    ) else (
        call :GetStamp
        echo %Stamp%: Invalid value for TempFlag ("%TempFlag%"), must be "0" or "1".>>"%LogFile%"
    )
)

:DoMode0
    if exist "%DestDir%\*.csv" (
        call :CallAPI
    )
    exit /b

:DoMode1
    if exist "%BaseDir%\*.csv" (
        call :MoveFiles
        call :CallAPI
    )
    exit /b

:MoveFiles
    set i=0
    for /f "tokens=*" %%A in ('dir /b /o-d /a-d "%BaseDir%\*.csv"') do (
        if !i! GEQ %MaxFiles% goto :Movedone
        move "%BaseDir%\%%~A" "%DestDir%">NUL
        call :GetStamp
        echo !Stamp!: Moved file "%%~A">>"%LogFile%"
        set /a i+=1
    )

    :Movedone
    if !i! EQU 0 (
        call :GetStamp
        echo %Stamp%: no files moved, exit 1 >>"%LogFile%"
        exit 1
    ) else (
        call :GetStamp
        echo %Stamp%: Total files moved = !i! >>"%LogFile%"
    )
    exit /b

:CallAPI
    rem --------------------------------------
    rem Call API java
    rem --------------------------------------
    set JAVA_ARGS=-showversion -Dlog4j.overwrite=true -Xmx4096m -Duse_description=true
    java %JAVA_ARGS% -jar "..\lib\mstt-synchro-cost-codes-2.jar" "..\conf\psconnect.properties" C:\API\folders-mstt-synchro-cost-codes-2\CostCodes2\
    exit /b

:GetStamp
    rem Get current date in YYYYMMDD_hhmmss format
    set Stamp=
    for /f "tokens=* skip=1" %%A in ('wmic os get LocalDateTime') do (
        if not defined Stamp (
            set Stamp=%%A
            set Stamp=!Stamp:~0,8!_!Stamp:~8,6!
            exit /b
        )
    )
    exit /b

Open in new window


Thank you in advance for your help.
Try this to resolve the "must" problem.

rem @echo off
setlocal EnableDelayedExpansion

set ROOT_DIR=C:\API
set APP_NAME=mstt-synchro-cost-codes-2
rem set APP_VERSION=1.0.1
set MODE=2
set TempFlag=0

rem Setting folders path
set APP_PATH=%ROOT_DIR%\%APP_NAME%
set APP_PATH_FOLDERS=%ROOT_DIR%\folders-%APP_NAME%

set BIN_DIR=%APP_PATH%\bin
set LIB_DIR=%APP_PATH%\lib
set CONF_DIR=%APP_PATH%\conf
set VAR_DIR=%APP_PATH%\var
set LOG_DIR=%APP_PATH%\log
set FLG_DIR=%APP_PATH%\flag

set BaseDir=%APP_PATH_FOLDERS%\CostCodes2\Temp
set DestDir=%APP_PATH_FOLDERS%\CostCodes2\Requests
set MaxFiles=18

cd %BIN_DIR%

call :GetStamp
set LogFile=%LOG_DIR%\C1_%Stamp%_MOVE.log

call :GetStamp
echo %Stamp%: Base folder is "%BaseDir%">>"%LogFile%"

call :GetStamp
echo %Stamp%: Destination folder is "%DestDir%">>"%LogFile%"

if "%TempFlag%" EQU "0" (
    call :DoMode0
) else (
    if "%TempFlag%" EQU "1" (
        call :DoMode1
    ) else (
        call :GetStamp
        echo %Stamp%: Invalid value for TempFlag "%TempFlag%", must be "0" or "1".>>"%LogFile%"
    )
)

:DoMode0
    if exist "%DestDir%\*.csv" (
        call :CallAPI
    )
    exit /b

:DoMode1
    if exist "%BaseDir%\*.csv" (
        call :MoveFiles
        call :CallAPI
    )
    exit /b

:MoveFiles
    set i=0
    for /f "tokens=*" %%A in ('dir /b /o-d /a-d "%BaseDir%\*.csv"') do (
        if !i! GEQ %MaxFiles% goto :Movedone
        move "%BaseDir%\%%~A" "%DestDir%">NUL
        call :GetStamp
        echo !Stamp!: Moved file "%%~A">>"%LogFile%"
        set /a i+=1
    )

    :Movedone
    if !i! EQU 0 (
        call :GetStamp
        echo %Stamp%: no files moved, exit 1 >>"%LogFile%"
        exit 1
    ) else (
        call :GetStamp
        echo %Stamp%: Total files moved = !i! >>"%LogFile%"
    )
    exit /b

:CallAPI
    rem --------------------------------------
    rem Call API java
    rem --------------------------------------
    set JAVA_ARGS=-showversion -Dlog4j.overwrite=true -Xmx4096m -Duse_description=true
    java %JAVA_ARGS% -jar "..\lib\mstt-synchro-cost-codes-2.jar" "..\conf\psconnect.properties" C:\API\folders-mstt-synchro-cost-codes-2\CostCodes2\
    exit /b

:GetStamp
    rem Get current date in YYYYMMDD_hhmmss format
    set Stamp=
    for /f "tokens=* skip=1" %%A in ('wmic os get LocalDateTime') do (
        if not defined Stamp (
            set Stamp=%%A
            set Stamp=!Stamp:~0,8!_!Stamp:~8,6!
            exit /b
        )
    )
    exit /b

Open in new window


»bp
Thank you Bill, I will test it and keep you informed.
Bill,

I tested and it works for TempFlagMode = 0 and TempFlagMode = 1.
Great news!
The unique remark that I have but I can manage this without a problem is when request or temp folders are empty, log report the following:
20191116_110219: Base folder is "C:\API\folders-mstt-synchro-cost-codes-2\CostCodes2\Temp"
20191116_110219: Destination folder is "C:\API\folders-mstt-synchro-cost-codes-2\CostCodes2\Requests"

Open in new window

I would prefer that log report the following:

"C:\API\folders-mstt-synchro-cost-codes-2\CostCodes2\Temp" is empty for TempFlagMode = 1 API java hasn't been called

"C:\API\folders-mstt-synchro-cost-codes-2\CostCodes2\Request" is empty for for TempFlagMode = 0 API java hasn't been called

If this can be done please let me know otherwise I would assign your previous message as solution.

Thank you for your help.
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

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
Thank you Bill, I will test it and keep you informed.
Bill, I tested and it works! Thank you very much for your help!
Welcome, thanks for the feedback.


»bp