Advice Question - how to check to see if file has already been ran?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
SIMMS7400
Posts: 446
Joined: 07 Jan 2016 07:47

Advice Question - how to check to see if file has already been ran?

#1 Post by SIMMS7400 » 21 Dec 2018 17:12

Hi Folks -

I have a process that consumes data files from a specific directory and then loads into a target application. However, after the load, the files are not moved/archived. Therefore, when I access the directory with each call to the function, the same file is read into the for loop again obviously.

My question is there a way to check if the file name has already been used and if so, skip and read in the next file? My only thought (and work around right now) has been after the file has been consumed, move it to the TEMP directory. Then, after all files have been processed, move all files back to the main directory.

Here is the script - please refer to the function :GETDATAFILENAME:

Code: Select all

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
::--:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::-- Script Name: FDMEE_Data_Load.cmd		                         		 --::
::                                                                           --::
::-- Description: Load Actuals to EPBCS                                      --::
::--																		 --::
::  																		 --::
::--  Calls:      _env.cmd                                                   --::
::--  Called By:  Not Applicable                                             --::
::               														   	 --::
::-- Parameters:  Call _env.cmd to get variable to determine:                --::
::               														   	 --::
::--              1. Path variables                                          --::
::--              2. User ID(s)                                              --::
::--              3. Logon(s)                                                --::
::--              4. Password(s)									         --::
::--              5. Server(s)								         	     --::
::--              6. Application(s)	    						             --::
::--			  7. Database(s)   		    							     --::
::--			  8. etc													 --::
::  																		 --::
::-- Author:	  Firm										     --::
::-- Date:	      10/10/2018                           	         		     --::
::  																		 --::
::--:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

::-- Set Working Directory as Script Path --::

CD /D "%~dp0"

::-- Call Environment File --::

CALL "_env.cmd"

::-- Set Process Output Variables --::

SET "PLOGPATH=%~n0_Logs\"
SET "PERRORPATH=%~n0_Errors\"

::-- Establish Intrapath variables & directories --::

SET "INTRAPATH=%LOGPATH%%PLOGPATH%"
SET "ERRORINTRAPATH=%ERRORPATH%%PERRORPATH%"

FOR %%F IN ( "%INTRAPATH%" "%ERRORINTRAPATH%" ) DO IF NOT EXIST "%%~F" MKDIR "%%~F"

::-- Parse DOS TIME variable in saveable format --::

FOR /F "tokens=1-2 delims=/:" %%A IN ("%TIME%")   DO ( SET "TIMESTAMP=%%A%%B" )
FOR /F "tokens=* delims= " %%C IN ("%TIMESTAMP%") DO ( SET "TIMESTAMP=%%C" )
SET "DATETIME=%date:~-4,4%%date:~-10,2%%date:~-7,2%_%TIMESTAMP%"
SET "YYYY_MMDD=%date:~-4,4%_%date:~-10,2%%date:~-7,2%"

::-- Establish Log ^& Error files --::

SET "LOGFILE=%INTRAPATH%%DATETIME%_%~n0.log"
SET "ERRORFILE=%ERRORINTRAPATH%%DATETIME%_%~n0.err"

::-- Perform Additional Variable Assignments --::

SET "FDMEE_BIN=%FILEPATH%FDMEE\PandL\"

SET "DLR1=DLR_SAP_EPBCS_PANDL_BS"    & SET "FDMEE_INBOX1=inbox/SAP_EPBCS_PANDL" & SET "DATAFILE1=hp_balancesheet_extract"  & SET "DLRLF1=SKY"      
SET "DLR2=DLR_SAP_EPBCS_PANDL_PC"    & SET "FDMEE_INBOX2=%FDMEE_INBOX1%"        & SET "DATAFILE2=hp_profitandloss_extract" & SET "DLRLF1=%DLRLF1%"
SET "DLR3=DLR_SAP_EPBCS_EXP_DT_ACT"  & SET "FDMEE_INBOX3=inbox/SAP_EPBCS_EXP"   & SET "DATAFILE3=hp_data_extracts"         & SET "DLRLF1=%DLRLF1%"
SET "DLR4=DLR_SAP_EPBCS_EXP_OTH_ACT" & SET "FDMEE_INBOX4=%FDMEE_INBOX3%"        & SET "DATAFILE4=hp_other_extract"         & SET "DLRLF1=%DLRLF1%"

SET "SVAR_MONTH1=AUTOMATION_Month"
SET "SVAR_YEAR1=AUTOMATION_Year"

SET "PRE_BR=AUTO_Clear_PandL"
SET "POST_BR=AUTO_Agg_PandL"

SET /A "CNT=0" & SET /A "TOTCNT=0"
SET "PING_DELAY=0"
SET "CMD=FOR %%F IN ( "%ERRORFILE%" "%LOGFILE%" ) DO ECHO"

::-- Begin Script Processing --::
>>"%LOGFILE%" (
ECHO ********************************************************
ECHO %~n0 - starting at %TIME%                		   	    
ECHO ********************************************************
ECHO.
ECHO ********************************************************
ECHO Login to Cloud Environment                                         
ECHO ********************************************************
ECHO.
ECHO Action    : Login
ECHO Login     : %CLOUD_UNAME%
ECHO Password  : %CLOUD_PSSWD%
ECHO Cloud URL : %CLOUD_URL%
ECHO Domain    : %CLOUD_DOMAIN%
ECHO --------------------------------------------------------
ECHO.
)

ECHO Logging into Cloud Environment...
PING -n %PING_DELAY% 127.0.0.1>nul

CALL "%EPMAUTOMATE%" login %CLOUD_CREDS% %CLOUD_URL% %CLOUD_DOMAIN% && (
	ECHO Successful : Login to Cloud Environment >>"%LOGFILE%" 
	) || (
	%CMD% Failed : Login to Cloud Environment >>"%%~F"
	SET "ERR=T" & GOTO END
)

::-- Get %DATAFILENAME% ^& Determine Load POV --::

CLS
:RUNLOAD
CALL :GETDATAFILENAME

FOR %%A IN ( "ERR" "ERR1" ) DO IF DEFINED %%~A GOTO END

IF "%CNT%"=="1" (
	>>"%LOGFILE%" (
	ECHO.
	ECHO ********************************************************
	ECHO Set Substitution Variables                     
	ECHO ********************************************************
	ECHO.
	ECHO Action  : setsubstvars
	ECHO Subvar  : %SVAR_MONTH1% = %MONTH%
	ECHO Subvar  : %SVAR_YEAR1%  = FY%YEAR%
	ECHO --------------------------------------------------------
	ECHO.
	)

	ECHO Setting Substitution Variables...
	PING -n %PING_DELAY% 127.0.0.1>nul

	CALL "%EPMAUTOMATE%" setsubstvars All %SVAR_MONTH1%=%MONTH% %SVAR_YEAR1%=FY%YEAR% >nul 2>&1 && (
		ECHO Successful : Update Substitution Variables >>"%LOGFILE%"
		) || (
		%CMD% Failed : Update Substitution Variables >>"%%~F"
		SET "ERR=T" & GOTO END
	)

	CLS

	>>"%LOGFILE%" (
	ECHO.
	ECHO ********************************************************
	ECHO Execute Business Rule                      
	ECHO ********************************************************
	ECHO.
	ECHO Action        : runbusinessrule
	ECHO Business Rule : %PRE_BR%
	ECHO Subvar        : %SVAR_MONTH1% = %MONTH%
	ECHO Subvar        : %SVAR_YEAR1%  = FY%YEAR%
	ECHO --------------------------------------------------------
	ECHO.
	)

	ECHO Executing Business Rule - %PRE_BR%
	PING -n %PING_DELAY% 127.0.0.1>nul

	CALL "%EPMAUTOMATE%" runbusinessrule "%PRE_BR%" >nul 2>&1 && (
		ECHO Successful : Execute Business Rule >>"%LOGFILE%"
		) || (
		%CMD% Failed : Execute Business Rule >>"%%~F"
		SET "ERR=T" & GOTO END
	)

	CLS
)

CLS 

>>"%LOGFILE%" (
ECHO.
ECHO ********************************************************
ECHO Delete FDMEE Data File                      
ECHO ********************************************************
ECHO.
ECHO Action    : deletefile
ECHO File Name : %DATAFILENAME%
ECHO --------------------------------------------------------
ECHO.
)
ECHO Deleting FDMEE Data File - %DATAFILENAME%
PING -n %PING_DELAY% 127.0.0.1>nul

CALL "%EPMAUTOMATE%" deletefile %FDMEE_INBOX%/%DATAFILENAME% >nul 2>&1 && (
	ECHO Successful : Delete FDMEE Data File >>"%LOGFILE%" 
	) || (
	%CMD% Warning : %DATAFILENAME% can not be deleted >>"%%~F"
    IF EXIST "deletefile*.log" MOVE /Y "deletefile*.log" "%INTRAPATH%" >nul 2>&1
)

CLS

>>"%LOGFILE%" (
ECHO.
ECHO ********************************************************
ECHO Upload FDMEE Data File                      
ECHO ********************************************************
ECHO.
ECHO Action    : uploadfile
ECHO File Name : %DATAFILENAME%
ECHO --------------------------------------------------------
ECHO.
)
ECHO Uploading FDMEE Data File - %DATAFILENAME%
PING -n %PING_DELAY% 127.0.0.1>nul

CALL "%EPMAUTOMATE%" uploadfile "%FDMEE_BIN%%DATAFILENAME%" %FDMEE_INBOX% >nul 2>&1 && (
	ECHO Successful : Upload FDMEE Data File >>"%LOGFILE%" 
	) || (
	%CMD% Failed : Upload FDMEE Data File >>"%%~F"
    SET "ERR=T" & GOTO END
)

CLS

>>"%LOGFILE%" (
ECHO.
ECHO ********************************************************
ECHO Execute Data Load Rule                      
ECHO ********************************************************
ECHO.
ECHO Action    : rundatarule
ECHO DLR Name  : %DLR%
ECHO DLR Inbox : %FDMEE_INBOX%
ECHO Start POV : %STARTPOV%
ECHO End POV   : %ENDPOV%
ECHO --------------------------------------------------------
ECHO.
)
ECHO Executing Data Load Rule - %DLR%
PING -n %PING_DELAY% 127.0.0.1>nul

CALL "%EPMAUTOMATE%" rundatarule "%DLR%" %STARTPOV% %ENDPOV% REPLACE STORE_DATA %DATAFILENAME% >nul 2>&1 && (
	ECHO Successful : Execute Data Load Rule >>"%LOGFILE%"
	) || (
	%CMD% Failed : Execute Data Load Rule >>"%%~F"
	SET "ERR=T" & SET "FDMEE_ERR=T"
)

CALL :EXTRACT_FDMEE_LOG "%DLRLF%" || GOTO END

IF DEFINED DLRFLAG (
	
	SET "DLR=DLR_SAP_EPBCS_PANDL_PL"
	>>"%LOGFILE%" (
	ECHO.
	ECHO ********************************************************
	ECHO Execute Data Load Rule                      
	ECHO ********************************************************
	ECHO.
	ECHO Action    : rundatarule
	ECHO DLR Name  : !DLR!
	ECHO DLR Inbox : %FDMEE_INBOX%
	ECHO Start POV : %STARTPOV%
	ECHO End POV   : %ENDPOV%
	ECHO --------------------------------------------------------
	ECHO.
	)
	ECHO Executing Data Load Rule - !DLR!
	PING -n %PING_DELAY% 127.0.0.1>nul

	CALL "%EPMAUTOMATE%" rundatarule "!DLR!" %STARTPOV% %ENDPOV% REPLACE STORE_DATA %DATAFILENAME% >nul 2>&1 && (
		ECHO Successful : Execute Data Load Rule >>"%LOGFILE%"
		) || (
		%CMD% Failed : Execute Data Load Rule >>"%%~F"
		SET "ERR=T" & SET "FDMEE_ERR=T"
	)

	CALL :EXTRACT_FDMEE_LOG "%DLRLF%" || GOTO END

	SET "DLR=DLR_SAP_EPBCS_PANDL_PLANT"
	>>"%LOGFILE%" (
	ECHO.
	ECHO ********************************************************
	ECHO Execute Data Load Rule                      
	ECHO ********************************************************
	ECHO.
	ECHO Action    : rundatarule
	ECHO DLR Name  : !DLR!
	ECHO DLR Inbox : %FDMEE_INBOX%
	ECHO Start POV : %STARTPOV%
	ECHO End POV   : %ENDPOV%
	ECHO --------------------------------------------------------
	ECHO.
	)
	ECHO Executing Data Load Rule - !DLR!
	PING -n %PING_DELAY% 127.0.0.1>nul

	CALL "%EPMAUTOMATE%" rundatarule "!DLR!" %STARTPOV% %ENDPOV% REPLACE STORE_DATA %DATAFILENAME% >nul 2>&1 && (
		ECHO Successful : Execute Data Load Rule >>"%LOGFILE%"
		) || (
		%CMD% Failed : Execute Data Load Rule >>"%%~F"
		SET "ERR=T" & SET "FDMEE_ERR=T"
	)

	CALL :EXTRACT_FDMEE_LOG "%DLRLF%" || GOTO END
	SET "DLRFLAG="
)

IF "%CNT%"=="4" (
	>>"%LOGFILE%" (
	ECHO.
	ECHO ********************************************************
	ECHO Execute Business Rule                      
	ECHO ********************************************************
	ECHO.
	ECHO Action        : runbusinessrule
	ECHO Business Rule : %POST_BR%
	ECHO Subvar        : %SVAR_MONTH1% = %MONTH%
	ECHO Subvar        : %SVAR_YEAR1%  = FY%YEAR%
	ECHO --------------------------------------------------------
	ECHO.
	)
	ECHO Executing Business Rule - %POST_BR%
	PING -n %PING_DELAY% 127.0.0.1>nul

	CALL "%EPMAUTOMATE%" runbusinessrule "%POST_BR%" >nul 2>&1 && (
		ECHO Successful : Execute Business Rule >>"%LOGFILE%"
		) || (
		%CMD% Failed : Execute Business Rule >>"%%~F"
		SET "ERR=T" & GOTO END
	)
)

IF "%CNT%" NEQ "4" GOTO RUNLOAD

:END

CLS
>>"%LOGFILE%" (
ECHO.
ECHO ********************************************************
ECHO Logout of Cloud Environment                                         
ECHO ********************************************************
ECHO.
ECHO Action : Logout
ECHO --------------------------------------------------------
ECHO.
)

ECHO Logging out of Cloud Environment ...
PING -n %PING_DELAY% 127.0.0.1>nul

CALL "%EPMAUTOMATE%" logout >nul 2>&1 && (
	ECHO Successful : Logout of Cloud Environment >>"%LOGFILE%" 
	) || (
	%CMD% Failed : Logout of Cloud Environment >>"%%~F"
	SET "ERR=T"
)
CLS

>>"%LOGFILE%" (
ECHO.
ECHO ********************************************************
ECHO Execute Purge Maintenance                                       
ECHO ********************************************************
ECHO.
)

ECHO Executing Purge Maintenance...
PING -n %PING_DELAY% 127.0.0.1>nul

::-- Paths are added to below function call --::
::-- Age is set in function --::

CALL :PURGE ""%INTRAPATH:~0,-1%","%ERRORINTRAPATH:~0,-1%","%ARCHIVEPATH:~0,-1%""

::-- Determine Exit Protocol --::
IF DEFINED ERR GOTO AbnormalExit

:NormalExit
>>"%LOGFILE%" (
ECHO.
ECHO ********************************************************
ECHO %~n0 - Completed Successfully                           
ECHO ********************************************************

ECHO ********************************************************
ECHO Normal Exit - %~nx0                                    
ECHO ********************************************************
DATE /t                                                     
TIME /t                                                     
)

CALL :EMAIL
CALL :ARCHIVE
EXIT /B 0

:AbnormalExit
FOR %%L IN ( "%LOGFILE%" "%ERRORFILE%" ) DO (
	>>"%%~L" (
	ECHO.
	ECHO ********************************************************
	ECHO %~n0 results in Abnormal Exit                           
	ECHO ********************************************************
	ECHO.
	ECHO ********************************************************
	ECHO Abnormal Exit - %~nx0                                   
	ECHO ********************************************************
	DATE /t                                                      
	TIME /t                                                      
	)
)

CALL :EMAIL
CALL :ARCHIVE
EXIT /B 1

:::::::::::::::::::::::::::::::::::::::::::::::::
::-- F U N C T I O N S   B E L O W   H E R E --::
:::::::::::::::::::::::::::::::::::::::::::::::::

:ARCHIVE

::-- Archive Log ^& Error Files in YYYY_MMDD Folder --::
::-- RD then used to remove empty error subdirectories --::
        
FOR %%F IN ( "%INTRAPATH%" "%ERRORINTRAPATH%" ) DO (
	IF NOT EXIST "%%~F%YYYY_MMDD%" MKDIR "%%~F%YYYY_MMDD%"
	IF EXIST "%%~F*" MOVE "%%~F*" "%%~F%YYYY_MMDD%" >nul 2>&1
)
RD "%ERRORINTRAPATH:~0,-1%" >nul 2>&1

GOTO :EOF

:EMAIL

::-- Check to see if there are any epmautomate generated log files --::
::-- If so, attach to failure email message --::
CALL :EPMAUTO_CHK

SETLOCAL DISABLEDELAYEDEXPANSION

IF NOT DEFINED ERR (
	SET "SW_BODY=%SW_MAIL_BIN%SW_BODYTEXT\FDMEE_LOAD.txt"
	SET "SW_SUB=ATTENTION : %~nx0 - Successful"
	SET "SW_PRIORITY="
    SET "SW_TO_EMAIL=%SW_FDMEE_LOAD_DL%"
) ELSE (
	SET "SW_BODY=%SW_MAIL_BIN%SW_BODYTEXT\FDMEE_LOAD_ERR.txt"
	SET "SW_SUB=WARNING : %~nx0 - Failed"
	SET "SW_PRIORITY=/Priority "High""
    SET "SW_TO_EMAIL=%SW_FDMEE_LOAD_ERR_DL%"
)

REM CALL "%SW_MAIL_BIN%SwithMail.exe" /s /x "%SW_MAIL_BIN%SwithMailSettings.xml"^
REM	/SSL /to "%SW_TO_EMAIL%" /sub "%SW_SUB%" /btxt "%SW_BODY%" /a "%SW_ATTACH%" %SW_PRIORITY%

IF EXIST "*.log" MOVE /Y "*.log" "%INTRAPATH%" >nul 2>&1
GOTO :EOF

:EPMAUTO_CHK

FOR %%F IN ( "*.log" ) DO SET "ERR_FILE=%%~F"

SET "SW_ATTACH=%LOGFILE%"

IF DEFINED ERR_FILE IF DEFINED FDMEE_ERR IF DEFINED FDMEE_KOFV SET "SW_ATTACH=%LOGFILE%|%ERR_FILE%|%INTRAPATH%%DATETIME%_%MAXFILE%|%INTRAPATH%%FDMEE_KOF%"
IF NOT DEFINED ERR_FILE IF DEFINED FDMEE_ERR IF DEFINED FDMEE_KOFV SET "SW_ATTACH=%LOGFILE%|%INTRAPATH%%DATETIME%_%MAXFILE%|%INTRAPATH%%FDMEE_KOF%"
IF DEFINED ERR_FILE IF NOT DEFINED FDMEE_ERR SET "SW_ATTACH=%LOGFILE%|%ERR_FILE%"

::-- Break out of CALL :EPMAUTO_CHK function --::
EXIT /B 0

:EXTRACT_FDMEE_LOG

SET "OUTBOX_BIN=outbox/logs/"
SET "NAME=%~1"
SET "MAXFILE="
SET "FILE=temp.txt"

CALL "%EPMAUTOMATE%" Listfiles > "%FILE%"

FOR /F "tokens=*" %%A IN ('FINDSTR /LC:"%OUTBOX_BIN%%NAME%_" %FILE%') DO (
  SET "NAME=%%~nA" & IF "!NAME!" GTR "!MAXFILE!" SET "MAXFILE=!NAME!.log"
)

CLS
>>"%LOGFILE%" (
ECHO.
ECHO ********************************************************
ECHO Download FDMEE Log File                                         
ECHO ********************************************************
ECHO.
ECHO Action   : downloadfile
ECHO Log Name : %MAXFILE%
ECHO --------------------------------------------------------
ECHO.
)
ECHO Downloading FDMEE Log File - %MAXFILE%
PING -n %PING_DELAY% 127.0.0.1>nul

CALL "%EPMAUTOMATE%" downloadfile %OUTBOX_BIN%%MAXFILE%  && (
	ECHO Successful : Download FDMEE Log File >>"%LOGFILE%"
	) || (
	%CMD% Failed : Download FDMEE Log File >>"%%~F"
    SET "ERR=1" & EXIT /B 1
)

IF EXIST "%MAXFILE%" MOVE /Y "%MAXFILE%"^
 "%INTRAPATH%%DATETIME%_%MAXFILE%" & DEL /F /Q "%FILE%" >nul 2>&1

::-- Extract FDMEE Kickouts from FDMEE process log if FDMEE_ERR variable is defined --::
IF DEFINED FDMEE_ERR (
	PUSHD "%INTRAPATH%"
		SET "SEARCH=Error: 3303"
		SET "FDMEE_KOF=%DATETIME%_KICKOUTS_%MAXFILE%"
		FOR /F "tokens=* delims=" %%A IN ('FINDSTR /LC:"!SEARCH!" "%DATETIME%_%MAXFILE%"') DO (
			ECHO %%A>>"!FDMEE_KOF!"
		)
        IF EXIST "!FDMEE_KOF!" SET "FDMEE_KOFV=T"
	POPD & EXIT /B 1
)
CLS	
GOTO :EOF

:GETDATAFILENAME

PUSHD "%FDMEE_BIN%"

    IF "%CNT%"=="0" FOR %%A IN ("*") DO SET /A "TOTCNT+=1"
    SET "DATAFILENAME="
    FOR %%A IN ("*") DO SET "DATAFILENAME=%%~nxA" & IF DEFINED DATAFILENAME GOTO SETINBOXPATH
	
    ::-- No Data Load Files Detected --::
	IF "%CNT%"=="0" IF NOT DEFINED DATAFILENAME (
		FOR %%F IN ( "%ERRORFILE%" "%LOGFILE%" ) DO (
			>>"%%~F" (
				ECHO.
				ECHO.
				ECHO ^|//^|\\//^|\\//^|\\//^|\\//^|\\//^|\\//^|\\//^|\\//^|\\//^|\\^|
				ECHO            WARNING: No FDMEE Data File^(s^) Available
				ECHO ^|//^|\\//^|\\//^|\\//^|\\//^|\\//^|\\//^|\\//^|\\//^|\\//^|\\^|
				ECHO.
			)
		)
	)
    
	::-- Breakout out of function --::
	IF "%CNT%"=="0"    POPD & SET "ERR=T"  & EXIT /B 1
	IF "%CNT%" NEQ "0" POPD & SET "ERR1=T" & EXIT /B 0
    
:SETINBOXPATH
SET "FDMEE_INBOX=" & SET /A "CNT+=1"

ECHO "%DATAFILENAME%" | FINDSTR /LC:"%DATAFILE1%" >nul 2>&1 && ( SET "FDMEE_INBOX=%FDMEE_INBOX1%" & SET "DLR=%DLR1%" & SET "DLRLF=%DLRLF1%")
ECHO "%DATAFILENAME%" | FINDSTR /LC:"%DATAFILE2%" >nul 2>&1 && ( SET "FDMEE_INBOX=%FDMEE_INBOX2%" & SET "DLR=%DLR2%" & SET "DLRLF=%DLRLF1%" & SET "DLRFLAG=T")
ECHO "%DATAFILENAME%" | FINDSTR /LC:"%DATAFILE3%" >nul 2>&1 && ( SET "FDMEE_INBOX=%FDMEE_INBOX3%" & SET "DLR=%DLR3%" & SET "DLRLF=%DLRLF1%")
ECHO "%DATAFILENAME%" | FINDSTR /LC:"%DATAFILE4%" >nul 2>&1 && ( SET "FDMEE_INBOX=%FDMEE_INBOX4%" & SET "DLR=%DLR4%" & SET "DLRLF=%DLRLF1%")
ECHO "%DATAFILENAME%" | FINDSTR /LC:"%DATAFILE5%" >nul 2>&1 && ( SET "FDMEE_INBOX=%FDMEE_INBOX5%" & SET "DLR=%DLR5%" & SET "DLRLF=%DLRLF1%")

IF NOT DEFINED FDMEE_INBOX (
    SET "UNMAPPED_BIN=%FDMEE_BIN%\FDMEE_Unmapped\%YYYY_MMDD%"
    IF NOT EXIST "!UNMAPPED_BIN!" MKDIR "!UNMAPPED_BIN!"
    
    MOVE /Y "%DATAFILENAME%" "!UNMAPPED_BIN!" >nul 2>&1
    
    >>"%LOGFILE%" (
        ECHO WARNING    : %DATAFILENAME% not recognized
        ECHO              File has been moved to the following path for review:
        ECHO              !UNMAPPED_BIN!
    )
    SET "ERR=T" & GOTO GETDATAFILENAME
)

::-- Set START ^& END POV --::
SET "FLAG="
ECHO "%FDMEE_INBOX%" | FINDSTR /LC:"EMP_HDCT_EXPENSE" >nul 2>&1 && (SET "FLAG=T")

IF DEFINED FLAG (
	FOR /F "tokens=3,4 delims=_." %%A IN ("%DATAFILENAME%") DO (
		SET "MONTH=%%A"
		SET "YEAR=%%B" & SET "YEAR=!YEAR:~2,2!"
		SET "STARTPOV=!MONTH!-!YEAR!" & SET "ENDPOV=!MONTH!-!YEAR!"
	)
) ELSE (
	SET "MONTH=Xxx"
	SET "YEAR=%DATAFILENAME:~-6%"
	SET "YEAR=!YEAR:~0,2!"
	SET "ENDYEAR=!YEAR:~0,2!" & SET /A "STARTYEAR=!ENDYEAR!-1"
	SET "STARTPOV=Oct-!STARTYEAR!" & SET "ENDPOV=Sep-!ENDYEAR!"
)

POPD
GOTO :EOF

:PURGE
SET "PURGE_DIRS=%~1"
 
FOR %%D IN ( %PURGE_DIRS:,= % ) DO (
SET "SKIP="

	IF NOT EXIST "%%~D" (
		ECHO "%%~D" | FINDSTR /C:"Errors" >nul 2>&1 && ( SET "SKIP=T" )
		IF NOT DEFINED SKIP (
			%CMD% The path "%%~D" does not exist >>"%%~F"
			SET "ERR=T" & SET "SKIP=T"
		)
	)
		IF NOT DEFINED SKIP (
			ECHO "%%~D" | FINDSTR /C:"Archive" >nul 2>&1 && ( SET "PROC=Archives" & SET "AGE=14" )
			ECHO "%%~D" | FINDSTR /C:"Errors"  >nul 2>&1 && ( SET "PROC=Logs"     & SET "AGE=14" )
			ECHO "%%~D" | FINDSTR /C:"Logs"    >nul 2>&1 && ( SET "PROC=Errors"   & SET "AGE=14" )

			FORFILES -p "%%D" /s -m *.* -d -!AGE! -c "cmd  /c DEL /q @path" 2>nul
			FORFILES -p "%%D" -d -!AGE! -c "cmd /c IF @isdir == TRUE RD /S /Q @path" 2>nul
			
			ECHO Successful : Purge !PROC! >>"%LOGFILE%"
		)
)
GOTO :EOF

aGerman
Expert
Posts: 3706
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Advice Question - how to check to see if file has already been ran?

#2 Post by aGerman » 23 Dec 2018 07:15

If you have to move your files temporarily out of the scope of your code then you might have already a big design problem in your script. Unfortunately your scripts are by far too long and not comprehensible for outsiders.
To answer your question:
Moving files on the same logical drive doesn't cause any physical moving on the hardware. Only the file table of the file system gets updated. But if you have your data on a server share and you move it to the temp directory in the clients profile then you will cause traffic in the network and you provoke data losses.
Think about using the archive flag (or other attributes) of the files (ATTRIB /? and DIR /? will help you).

Steffen

sst
Posts: 79
Joined: 12 Apr 2018 23:45

Re: Advice Question - how to check to see if file has already been ran?

#3 Post by sst » 23 Dec 2018 08:38

If you can consider changing the design of your script then something like this should do the job

Code: Select all

for %%A in ("%FDMEE_BIN%\*") do call :RUNLOAD "%%~A"
But if you want to stick with your current design then you can try this less efficient method

Code: Select all

REM Initialization
dir /a-d /b "%FDMEE_BIN%\" >"%TEMP%\DataFiles_Static.txt"
set "skip=0"

REM Other stuff

:GETDATAFILENAME
pushd "%FDMEE_BIN%"
set "DATAFILENAME="
(
    for /L %%A in (1,1,%skip%) do (
        REM skip previously used files
        set /p "="
    )
    set /p "DATAFILENAME="
)<"%TEMP%\DataFiles_Static.txt"
set /a "skip+=1"
if not defined DATAFILENAME REM No more data files

REM Rest of the function...

Squashman
Expert
Posts: 4111
Joined: 23 Dec 2011 13:59

Re: Advice Question - how to check to see if file has already been ran?

#4 Post by Squashman » 24 Dec 2018 00:06

You expect someone to go through all that code? You would be villafied with extreme prejudice on other forums. How about you post a minimal but complete and verifiable example.

Post Reply