# Batch File to Check ZIPS



## ColeDuncan (Mar 28, 2013)

Running on server 2012, I have ZIP files FTP'ed to the server and then archived. Is there any way to run a batch file to check the ZIP file to see if it is a good file? I'm not getting data corruption, just FTP failures after it starts sending, so it is more of an incomplete ZIP altogether.

Thanks


----------



## foxidrive (Oct 20, 2012)

You could get the FTP server list of files/filesizes to compare with the local copy and see if the filesize matches. It doesn't guarantee a perfect copy but it should be a fair check.


----------



## Squashman (Apr 4, 2003)

If it is an incomplete ZIP file then it is corrupt.

Besides checking dates & file sizes, the best you could is run some type of check sum software whether that be MD5, CRC etc.
But that would basically require a lot of extra work. You would have to create a checksum of the file before the FTP. Then FTP the file. Then re-download the file and verify the checksum on it again.

My job requires us to FTP hundreds of files every day. If the files are getting corrupted during FTP then I would think there is some other issue at hand that is causing it. It is very very very rare that any file we send is corrupted or incomplete when we send it. The only time that usually happens is if we are sending a file to another site and they have an auto-sweep program that downloads and deletes files from the directory we upload the file too. Sometimes their software doesn't realize the file is still being upload and either tries to download it or delete it while it is being used and that corrupts it.


----------



## Grobu (May 2, 2013)

Are you looking for a way to tell if a group of recently transferred files are valid or corrupted .ZIP archives?
I suppose you cannot do any file comparison with the original copies (on the remote source computer). If you have WinRAR (or the freely available RAR utility from rarlab), you can test a series of files :

```
rar t [I]archive_name[/I]
```
and test the errorlevel environment variable (will be > 0 if the archive is corrupted).

Here is an example .BAT script for checking the content of a given directory :

```
@echo off
if "%1"=="_fileTest" goto test
if not exist "%1" goto noDir
for %%z in (%1\*.zip) do %0 _fileTest %%z
goto end

:test
	shift
	rar t -inul %1
	if errorlevel 1 goto error
	goto end

:noDir
	echo directory "%1" cannot be found
	goto end

:error
echo.
echo ---------------------------
echo archive seemingly corrupted
echo ---------------------------
echo %1
echo ---------------------------
echo .

:end
```
Hope that helps.


----------



## foxidrive (Oct 20, 2012)

From the look of your code, you were a batch programmer in MSdos or Win9x.

In NT windows you could do something this, using your command line:


```
@echo off
for %%a in ("%~1\*.zip") do (
"c:\path\rar" t -inul "%%a" >nul || echo "%%a" seems to be corrupted
)
```
But the original question was to see if the files on the FTP server were corrupted or not. This method would entail downloading them again and then download corruptions could still occur to confuse the issue.


----------



## Grobu (May 2, 2013)

:up: for the modern batch coding style.



> I have ZIP files FTP'ed to the server and then archived


Correct me if I'm wrong, but it seems to me that the OP needs a method to tell if the archives residing on his server computer have been fully transferred or not.
Running RAR t (or any other .ZIP compatible utility allowing to test for file integrity) requires no additional file download.
It will issue a warning on every incomplete file found among the already downloaded set.


----------



## foxidrive (Oct 20, 2012)

Grobu said:


> Correct me if I'm wrong, but it seems to me that the OP needs a method to tell if the archives residing on his server computer have been fully transferred or not.
> Running RAR t (or any other .ZIP compatible utility allowing to test for file integrity) requires no additional file download.


It would seem that the FTP server is not local.


----------



## Elvandil (Aug 1, 2003)

..and if the server is not local, any verification or hashing requires redownloading the file that was transferred, as *Squashman* pointed out.


----------



## Grobu (May 2, 2013)

> Running on server 2012, I have ZIP files FTP'ed to *the* server


So you guys can tell from this sentence that the server isn't local? Hats off.

It's common sense that any solution involving re-downloading files is out of question.

Either the Original Poster owns the server machine, and can access it physically (or remotely, over a Telnet session) to run a file integrity check before moving the freshly transferred archives to another folder (c.f. RAR script) ...

... or, if he is at the sending end of the FTP channel only (which isn't obvious to me), and he can perform a file size comparison between the remote directory listing and his local upload folder. The following batch snippet attempts to automate that step.


```
@echo off

[COLOR="Green"]REM --- prompt user for FTP host and credentials[/COLOR]
set /p ftp_host="FTP host: "
set /p ftp_login="Login: "
set /p ftp_pw="Password: "
set /p ftp_zipDir="Remote path for .ZIP storage: "

[COLOR="green"]REM --- create command file[/COLOR]
prompt>ftp_zip.txt

[COLOR="green"]REM --- append FTP commands to file[/COLOR]
for %%a in ("open %FTP_host%" "%ftp_login%" "%ftp_pw%" "cd %ftp_zipDir%" "binary") do echo>>ftp_zip.txt %%~a

[COLOR="green"]REM --- append .ZIP file upload commands[/COLOR]
for /f %%a in ('dir /a:-d /b *.zip') do echo>>ftp_zip.txt put %%a

[COLOR="green"]REM --- append additional commands (including a remote directory listing)[/COLOR]
for %%a in ("ascii" "dir %ftp_zipDir% ftp_output" "disc" "quit") do echo>>ftp_zip.txt %%~a

[COLOR="green"]REM --- instruct ftp client to process commands in file[/COLOR]
ftp -v -s:ftp_zip.txt >nul

echo uploading files ...

[COLOR="green"]REM --- clean up "." and ".." from listing[/COLOR]
type ftp_output | find "zip" >ftp_output.tmp

echo Comparing remote and local file sizes ...

[COLOR="green"]REM --- for each line of remote listing, get file name and file size[/COLOR]
for /f "tokens=5,9" %%a in (ftp_output.tmp) do 	(

[INDENT][COLOR="green"]REM --- list local corresponding .ZIP file[/COLOR]
dir %%b | find "zip" >_zip_.tmp

[COLOR="green"]REM --- get file size and perform comparison[/COLOR]
for /f "tokens=4" %%c in (_zip_.tmp) do if not "%%c"=="%%a" echo File %%b got corrupted during transfer[/INDENT]

)

[COLOR="green"]REM --- temp files clean-up[/COLOR]
for %%a in (ftp_zip.txt ftp_output ftp_output.tmp _zip_.tmp) do del %%a

echo Done
											
:end
set ftp_host=
set ftp_login=
set ftp_pw=
set ftp_zipDir=
```
Hope that helps.


----------



## foxidrive (Oct 20, 2012)

Grobu said:


> So you guys can tell from this sentence that the server isn't local? Hats off.


Most commonly a batch file would use copy or xcopy or robocopy on any server that is local, and an FTP server is most commonly a server that is remote from the user.

You can ask the OP if they ever return.


----------



## Squashman (Apr 4, 2003)

If the server supported cross crc checksums you could verify it without redownloading the file but cross crc is not part of the official protocol standards so most ftp servers and clients do not have it built in.


----------

