Check for File Exist

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
manoj12
Posts: 3
Joined: 22 Jan 2016 07:59

Check for File Exist

#1 Post by manoj12 » 02 Feb 2016 09:17

Hi Sir,

This is my source code which will copy the latest file from a F:\ to D:\ and will send email if file exists.

Code: Select all

@echo off

REM Copy Latest File in one destination to another --------------------------------------------
rem The first part of your code goes here

rem The rest of your code goes here
if exist "F:\Staffing_Dashboard_Data\Super_Alpha\Super_Alpha_source_files\" goto :FileExists

:FileExists
setlocal
set srcDir=F:\My_Dashboard_Data\Alpha1\Source_files\
set destdir=D:\My_Dashboard_Data\Alpha1\Source_files\
set lastmod=
pushd %srcDir%
for /f "tokens=*" %%a in ('dir *.csv /b /o-d /a-d 2^>NUL') do set lastmod=%%a & goto :xx
:xx
if "%lastmod%"=="" echo Could not locate files.&goto :eof
:::
copy "%lastmod%" "%destDir%"

set SCRIPT_DIR=E:\Copy_Script
set FROM_ADDRESS=iticsamoffshorehr.nar@capgemini.com
::REM set TO_ADDRESS=iticsamoffshorehr.nar@capgemini.com
::set TO_ADDRESS=iticsamoffshorehr.nar@capgemini.com
set TO_ADDRESS=iticsamoffshorehr.nar@capgemini.com
set CC=iticsamoffshorehr.nar@capgemini.com ;
set SUCCESS_SBJ=Super_Alpha:File Copied Status.
set SUCCESS_BODY1=The File "%lastmod%" is succesfully copied.This is an automated email. Please do not reply.
set SUCCESS_BODY2=The File "%lastmod% is succesfully copied.This is an automated email. Please do not reply.
E:
cd %SCRIPT_DIR%
sendEmail -f %FROM_ADDRESS% -t %TO_ADDRESS% -cc %CC% -u %SUCCESS_SBJ% -m %SUCCESS_BODY1% -s ismtp.corp.capgemini.com
PAUSE
else:
:Fail
set FROM_ADDRESS=mysupport@email.com
::REM set TO_ADDRESS=mysupport@email.com
::set TO_ADDRESS=mysupport@email.com
set TO_ADDRESS=mysupport@email.com
set CC=mysupport@email.com;
set SUCCESS_SBJ=Super_Alpha:File Copied Status.
set SUCCESS_BODY1=The File does not exist.This is an automated email. Please do not reply.
set SUCCESS_BODY2=The File does not exist.This is an automated email. Please do not reply.
E:
cd %SCRIPT_DIR%
sendEmail -f %FROM_ADDRESS% -t %TO_ADDRESS% -cc %CC% -u %SUCCESS_SBJ% -m %SUCCESS_BODY1% -s ismtp.corp.mysupport.com
pause


Now what i want is if file does not exist it should fail giving error as File does not exist and send email to mysupport@email.com

Appreciate your help on this

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Check for File Exist

#2 Post by Compo » 03 Feb 2016 05:22

How does this work for you?

Code: Select all

@Echo Off
SetLocal
Set "LastMod="
Set "DirPath=\My_Dashboard_Data\Alpha1\Source_files\"
If "%CD%\" NEq "F:%DirPath%" PushD "F:%DirPath%"||GoTo :EOF

Set "SCRIPT_DIR=E:\Copy_Script"
Set "SUCCESS_SBJ=Super_Alpha:File Copied Status."
Set "FROM_ADDRESS=iticsamoffshorehr.nar@capgemini.com"
Set "TO_ADDRESS=iticsamoffshorehr.nar@capgemini.com"
Set "CC=iticsamoffshorehr.nar@capgemini.com;"
Set "SUCCESS_BODY1=The File "%LastMod%" was succesfully copied."
Set "SUCCESS_BODY1=%SUCCESS_BODY1% This is an automated email."
Set "SUCCESS_BODY1=%SUCCESS_BODY1% Please do not reply."

For /F "Tokens=*" %%a In ('Dir/B/A-D/O-D *.csv 2^>Nul') Do Set "LastMod=%%a"
If Defined LastMod (Copy "%LastMod%" "D:%DirPath%") Else (Call :Fail)

PushD %SCRIPT_DIR%
SendEmail -f %FROM_ADDRESS% -t %TO_ADDRESS% -cc %CC% -u "%SUCCESS_SBJ%" -m "%SUCCESS_BODY1%" -s ismtp.corp.mysupport.com
Pause
Exit/B

:Fail
Echo=Could not locate files.
Set "FROM_ADDRESS=mysupport@email.com"
Set "TO_ADDRESS=mysupport@email.com"
Set "CC=mysupport@email.com;"
Set "SUCCESS_BODY1=The File does not exist."
Set "SUCCESS_BODY1=%SUCCESS_BODY1% This is an automated email."
Set "SUCCESS_BODY1=%SUCCESS_BODY1% Please do not reply."
<Edit />
Changed %LastMod% to LastMod on line 17
Last edited by Compo on 05 Feb 2016 04:42, edited 1 time in total.

manoj12
Posts: 3
Joined: 22 Jan 2016 07:59

Re: Check for File Exist

#3 Post by manoj12 » 04 Feb 2016 11:09

Hi Sir,

This script always gives File does not exist even if file is present in the F:\My_Dashboard_Data\Alpha1\Source_files\ Location

I wanted if latest file exist then return latest file else give me file does not exist.

Appreciate your help sir

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Check for File Exist

#4 Post by foxidrive » 04 Feb 2016 16:28

Are you trying to determine if the .csv files exist? The second line here can help you with that.

Code: Select all

pushd %srcDir%
if not exist *.csv echo do your error routine here

manoj12
Posts: 3
Joined: 22 Jan 2016 07:59

Re: Check for File Exist

#5 Post by manoj12 » 05 Feb 2016 04:23

Hi Sir,

Code: Select all



REM Copy File in one destination to another --------------------------------------------

if not exist "F:\Staffing_Dashboard_Data\GX\GX_CSV_Files\*.csv" goto :fail

:FileExists
setlocal
set srcDir=F:\Staffing_Dashboard_Data\GX\GX_CSV_Files\
set destdir=D:\Staffing_Dashboard_Data\GX\GX_CSV_Files\
set lastmod=
pushd %srcDir%
for /f "tokens=*" %%a in ('dir *.csv /b /o-d /a-d 2^>NUL') do set lastmod=%%a & goto :xx
:xx
if "%lastmod%"=="" echo File Not Found.&goto :fail
:::
copy "%lastmod%" "%destDir%"
set SCRIPT_DIR=E:\Copy_Script
set FROM_ADDRESS=iticsamoffshorehr.nar@mysupport.com
::REM set TO_ADDRESS=iticsamoffshorehr.nar@mysupport.com
::set TO_ADDRESS=iticsamoffshorehr.nar@mysupport.com
set TO_ADDRESS=iticsamoffshorehr.nar@mysupport.com
set CC=iticsamoffshorehr.nar@mysupport.com ;
set SUCCESS_SBJ=GX_LOAD:File Copied Status.
set SUCCESS_BODY1=The File "%lastmod%" is succesfully copied.This is an automated email. Please do not reply.
set SUCCESS_BODY2=The File "%lastmod% is succesfully copied.This is an automated email. Please do not reply.
E:
cd %SCRIPT_DIR%
sendEmail -f %FROM_ADDRESS% -t %TO_ADDRESS% -cc %CC% -u %SUCCESS_SBJ% -m %SUCCESS_BODY1% -s ismtp.corp.mysupport.com.com



:fail
set SCRIPT_DIR=E:\Copy_Script
set FROM_ADDRESS=iticsamoffshorehr.nar@mysupport.com
::REM set TO_ADDRESS=iticsamoffshorehr.nar@mysupport.com
::set TO_ADDRESS=iticsamoffshorehr.nar@mysupport.com
set TO_ADDRESS=iticsamoffshorehr.nar@mysupport.com
set CC=iticsamoffshorehr.nar@mysupport.com
set FAIL_SBJ=GX_LOAD:File Copied Status.
set FAIL_ERROR1=The File of GX_LOAD does not exist.This is an automated email. Please do not reply.
E:
cd %SCRIPT_DIR%
sendEmail -f %FROM_ADDRESS% -t %TO_ADDRESS% -cc %CC% -u %FAIL_SBJ% -m %FAIL_ERROR1% -s ismtp.corp.mysupport.com.com
pause


This script is sending two emails of which File is succesfully copied as welll as the file of GX_LOAD does not exist.

What I really want is File does not exist only one mail should be triggered as File of GX_LOAD does not exist else the file is succesfully copied.

I am really not able to understand why it is not sending only one email instead of two.

I also tried the same thing using the approach given by Compo and foxidrive but it is really not working as expected.

Appreciate your help sir.

Regards,

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Check for File Exist

#6 Post by Compo » 05 Feb 2016 04:44

manoj12 wrote:Hi Sir,

This script always gives File does not exist even if file is present in the F:\My_Dashboard_Data\Alpha1\Source_files\ Location

I wanted if latest file exist then return latest file else give me file does not exist.

Appreciate your help sir
I have made a quick edit of my code, see if that helps!

Post Reply