1.

Solve : Batch file to check files exist?

Answer»

I have a batch file that checks if our image & DR files have been transferred to our DR server.

This file is faily long and I'm wanting to either reduce the amount of line or make it easier to edit...main section is as follows;

This checks for 7zip files...
Code: [Select]echo -------------------------------------------------------------->> %logfile%
set FINDSTR=%DRIVELETTER%:\SERVER1\e\backup\FILE.7z
if not exist %FINDSTR% (
echo ***ERROR*** - %FINDSTR%>> %logfile%
) ELSE (
echo Located - %FINDSTR%>> %logfile%
)

This check for modified date on image files...
Code: [Select]set FINDSTR="%DRIVELETTER%:\SERVER1\e\Image files\FILE8.tib"
dir %FINDSTR% | %SystemRoot%\system32\find "%DATESTR%/%MONTHSTR%/%YEARSTR%" >nul
if %ERRORLEVEL%==1 echo Error with SERVER1 TIB files, does not exist or incorrect file date>> %logfile%

This repeats for each database & image file and currently is just over 500 lines and will be around 900 when complete by doing it this way.

What I'd prefer to do is create a sub routine that can be called to execute the actual checks, then go back and change FINDSTR to the next file to be checked then jump to the sub routine again...then do the same for the image files.

Code: [Select]set FINDSTR
goto CheckRoutine
set FINDSTR
goto CheckRoutine
.
.
goto end
:CheckRoutine
blah blah
RESUME BACK IN MAIN CODE
:end
Any help would be appreciatedYou don't need a subroutine, you need a loop.

Hard coding the 7z files in the batch file is counterproductive. Each file is processed the same, so why not code their names in a text file (FileList.txt); one name per line and read each label prior to testing it's existence?

The tib files are more problematic. You're just checking the date but you need to feed the loop from a directory list.

Perhaps this will help or at least get you started.

Code: [Select]echo off
set logfile=logfile.log
for /f "tokens=* delims=" %%i in (FileList.txt) do (
  if exist "%%i" (echo Located - %%i >> %logfile%) else (echo ***ERROR*** - %%i >> %logfile%)
)

for /f "tokens=* delims=*" %%i in ('dir /s /b "%DRIVELETTER%:\SERVER1\e\Image files\*.tib"') do (
  dir %%i ^| find "%DATESTR%/%MONTHSTR%/%YEARSTR%" > nul
  if errorlevel 1 echo Error with SERVER1 TIB files, does not exist or incorrect file date >> %logfile%
)

Good luck.  So...
Code: [Select]for /f "tokens=* delims=" %%i in (FileList.txt)...will import a line at a time into the loop.

I'll test it and let you know how I get on.

Thanks for the responseThanks, that worked..but I have 2 issues

I can't pass a parameter between list file and the batch file.

dr1-zip.txt contains the FOLLOWING lines...
Code: [Select]\server1\e\backup\db\database1_
\server1\e\backup\db\database2_
\server2\e\backup\db\database1_
\server2\e\backup\db\database2_

I need to create an import file for 7z & tib files...we also have BAK files...so that's 3 import file per DR server making it 9 files in total.

Code: [Select]for /f "tokens=* delims=*" %%i in (dr1-zip.txt) do (
if exist %DRIVELETTER%:%%i%CHECKSTR%*.7z (
echo Located - "\\dr1%%i%CHECKSTR%*.7z" >> %logfile%
) else (
echo ***ERROR*** - \\dr1%%i%CHECKSTR%*.7z >> %logfile%
)
)
for /f "tokens=* delims=*" %%i in (dr1-tib.txt) do (
dir %DRIVELETTER%:%%i*.tib ^| find "%DATESTR%/%MONTHSTR%/%YEARSTR%" > nul
if %ERRORLEVEL%==0 (
echo Located - "\\dr1%%i" TIB files >> %logfile%
) else (
echo ***ERROR*** - "\\dr1%%i", does not exist or incorrect file date >> %logfile%
)
)
I'd then have to create FOR loop for BAK files, then do it all again on 2 other DR SERVERS.

But I'd prefer the following for the list file...
Code: [Select]\server1\e\backup\db\database1_%CHECKSTR%*.7z
\server1\e\backup\db\database2_%CHECKSTR%*.7z
\server2\e\backup\db\database1_%CHECKSTR%*.7z
\server2\e\backup\db\database2_%CHECKSTR%*.7z
Code: [Select]for /f "tokens=* delims=*" %%i in (dr1-zip.txt) do (
if exist %DRIVELETTER%:%%i (
echo Located - "\\dr1%%i" >> %logfile%
) else (
echo ***ERROR*** - \\dr1%%i >> %logfile%
)
)
Pass %CHECKSTR% back to my batch file - %CHECKSTR% = date to check for, defined earlier in the batch file.
Second issue is, can I parse the last 3 chars of %%i and create a if,else loop?

Code: [Select]for /f "tokens=* delims=*" %%i in (dr1-zip.txt) do (
  if LAST3CHARS==.7z (
    if exist %DRIVELETTER%:%%i (
      echo Located - "\\dr1%%i" >> %logfile%
    ) else (
      echo ***ERROR*** - \\dr1%%i >> %logfile%
    )
) else if LAST3CHARS==tib (
  dir %DRIVELETTER%:%%i*.tib ^| find "%DATESTR%/%MONTHSTR%/%YEARSTR%" > nul
  if %ERRORLEVEL%==0 (
    echo Located - "\\dr1%%i" TIB files >> %logfile%
  ) else (
    echo ***ERROR*** - "\\dr1%%i", does not exist or incorrect file date >> %logfile%
  )
)
Quote

I can't pass a parameter between list file and the batch file. Pass %CHECKSTR% back to my batch file - %CHECKSTR% = date to check for, defined earlier in the batch file.

You're right. The text file has no knowledge of what goes on in the batch file. This might work with delayed expansion. If not, there might be other ways, but this seems the simplest.

Code: [Select]\server1\e\backup\db\database1_!CHECKSTR!*.7z
\server1\e\backup\db\database2_!CHECKSTR!*.7z
\server2\e\backup\db\database1_!CHECKSTR!*.7z
\server2\e\backup\db\database2_!CHECKSTR!*.7z


Also, add a setlocal enabledelayedexpansion line to the beginning of your batch file.

Quote
Second issue is, can I parse the last 3 chars of %%i and create a if,else loop?

You can use the notation %%~xi to isolate the extension of the file. Note: %%~xi includes the dot.

I couldn't test the code on this machine,  so I wish you luck. 

FYI: The if/else construct is not a loop, the for instruction as written is. Keep in mind, batch code is not a programming language, it's a command language. You might want to check out VBScript which was installed with your OS. It's a lightweight scripting language which has more functionality than batch language.I don't think I have the FOR command correct...

Code: [Select]for /f "tokens=* delims=" %%i in (zip.txt) do (
    for /F %%a in ("%%i") do (
      set FILETYPE=%%~xI
      echo %FILETYPE%
   )
)
FILETYPE contains no value...My first reply to you was this basic framework:

Quote
echo off
for /f "tokens=* delims=" %%i in (FileList.txt) do (
  if exist "%%i" (echo Located - %%i >> %logfile%) else (echo ***ERROR*** - %%i >> %logfile%)
)

for /f "tokens=* delims=*" %%i in ('dir /s /b "%DRIVELETTER%:\SERVER1\e\Image files\*.tib"') do (
  dir %%i ^| find "%DATESTR%/%MONTHSTR%/%YEARSTR%" > nul
  if errorlevel 1 echo Error with SERVER1 TIB files, does not exist or incorrect file date >> %logfile%
)

You then posted back that it worked, but claimed there were some issues. Fair enough, but how it went from my original post to this convoluted mess, I'll never know.

Quote
for /f "tokens=* delims=*" %%i in (dr1-zip.txt) do (
  if LAST3CHARS==.7z (
    if exist %DRIVELETTER%:%%i (
      echo Located - "\\dr1%%i" >> %logfile%
    ) else (
      echo ***ERROR*** - \\dr1%%i >> %logfile%
    )
) else if LAST3CHARS==tib (
  dir %DRIVELETTER%:%%i*.tib ^| find "%DATESTR%/%MONTHSTR%/%YEARSTR%" > nul
  if %ERRORLEVEL%==0 (
    echo Located - "\\dr1%%i" TIB files >> %logfile%
  ) else (
    echo ***ERROR*** - "\\dr1%%i", does not exist or incorrect file date >> %logfile%
  )
)
I suggested some changes to the input file. Did they work? If not, this issue should be resolved before moving on.

Next you posted this nested for loop, which for the life of me can't figure out why it's needed or what context you're using it in.

Quote
for /f "tokens=* delims=" %%i in (zip.txt) do (
    for /F %%a in ("%%i") do (
      set FILETYPE=%%~xI
      echo %FILETYPE%
   )
)

I would SUGGEST getting back to basics. Create a for loop for the 7z files and get it working. Add a second for loop for the tib files and get that working. Then you can add a third for loop for the bak files. If later you find DUPLICATE code for two of the file extensions, you can work at combining/streamlining the logic. It's easier to debug code as you develop it, rather than writing all the code and debugging from line 1. Above all, keep it simple.

 

Using the !CHECKSTR! worked fine...thanks for that pointer.

There are file types of tib, bak, 7z and DAT that need to be checked to make sure they have been succesfully copied to the server.

The 7z & bak files contain the date of the backup within the file name, so I can check if the correct date is there and that works great.

Only way to check tib & dat files is to get the modified date from the file, check this is the date we expect.

We have 3 servers, maybe more to come...with 4 file types, that's 12 list files to maintain so I wanted to check the ext of the filename and execute the checking commands based of filetype...that's what I added the extra FOR inside the first one.


Discussion

No Comment Found