# Solved: Batch File to see if text files exist



## angelr82 (Jun 15, 2010)

Ok been trying to figure this out but hit a wall. I have a spreadsheet that contains 75K+ entries of text file paths. example: 
C:\text\text001.txt
C:\text\text002.txt 

And so on....

i know the if exist statement in dos works one line at a time but i have 75K+ items to check and wanted to output a report telling me if the a file exist or not based on the list of file paths. 

Any suggestions???

Thanks,
Angel


----------



## Squashman (Apr 4, 2003)

Batch cannot directly read an Excel file. Export the paths to a plain text file and then you can run a for loop to parse the text file.


```
@echo off
FOR /F "delims=" %%G in (filepaths.txt) do (
	IF EXIST "%%G" (
		ECHO %%G EXISTS
	) ELSE (
		ECHO %%G DOES NOT EXIST
	)
)
```


----------



## angelr82 (Jun 15, 2010)

Squashman said:


> Batch cannot directly read an Excel file. Export the paths to a plain text file and then you can run a for loop to parse the text file.
> 
> 
> ```
> ...


ok not sure if I understand that so i exported to a text file one path per line. copied to desktop and inserted the file path including the filename to my filepaths.txt in the () ran but nothing happens. it just flashes; my knowledge here is pretty limited.

Thanks in advance. 
Angel


----------



## Squashman (Apr 4, 2003)

I need to see to the exact code you used and show me what is in the filepaths text file.


----------



## angelr82 (Jun 15, 2010)

here is the code, i just copy and paste 
@echo off
FOR /F "delims=" %%G in (C:\Documents and Settings\aruiz\Desktop\TextFilePaths.txt) do (
IF EXIST "%%G" (
ECHO %%G EXISTS
) ELSE (
ECHO %%G DOES NOT EXIST
)
)


here is a snip of the file list i have 
D:\V005\TEXT\0373636.TXT
D:\V005\TEXT\0373639.TXT
D:\V005\TEXT\0376943.TXT
D:\V005\TEXT\0376947.TXT
D:\V005\TEXT\0376953.TXT
D:\V005\TEXT\0377071.TXT


----------



## Squashman (Apr 4, 2003)

So my batch file assumes that the BATCH file and the filepaths.txt file are in the same directory. This way I don't have to specify the path to the filepath.txt file.

Your batch file you are putting in the whole path and the path has spaces. In the world of batch file you need to enclose anything with spaces in quotes.

But there is another kicker. A For /F will interpret the quotes and think what is in the parenthesis is just TEXT and not a file name it has to parse. So when you need to use quotes around file paths in a FOR /F loop you have two options:
1) Use the USEBACKQ option.
2) Use the Type command and put the Filepath in quotes.

1.

```
@echo off
FOR /F "usebackq delims=" %%G in ("C:\Documents and Settings\aruiz\Desktop\TextFilePaths.txt") do (
	IF EXIST "%%G" (
		ECHO %%G EXISTS
	) ELSE (
		ECHO %%G DOES NOT EXIST
	)
)
pause
```
2.

```
@echo off
FOR /F "delims=" %%G in ('type "C:\Documents and Settings\aruiz\Desktop\TextFilePaths.txt"') do (
	IF EXIST "%%G" (
		ECHO %%G EXISTS
	) ELSE (
		ECHO %%G DOES NOT EXIST
	)
)
pause
```


----------



## angelr82 (Jun 15, 2010)

awesome, taking the path out works great since they are in the same DIR. one last question, is there a way to write the results to file say Log.txt or something that i can search and see what exactly is missing?


----------



## Squashman (Apr 4, 2003)

```
ECHO %%G DOES NOT EXIST>>DoesNotExist.log
```


----------



## angelr82 (Jun 15, 2010)

Cool i will try that i found this and it seems to work well too but have a few extra steps. 

batch.bat >Log.TXT 2>&1


----------

