开发者

Check for file existence

开发者 https://www.devze.com 2023-02-13 18:21 出处:网络
I\'m looking to write a script that will load up a file that has a bunch of directory/filenames in CSV format, and then output what files are not found.

I'm looking to write a script that will load up a file that has a bunch of directory/filenames in CSV format, and then output what files are not found.

This is what I have so far:

for /f "delims=, tokens=1,2,3" %i in (C:\foo.csv) do dir "Y:\%i\%j\%k" >> C:\temp\log.txt

Which outputs everything.

What I really want is for it to l开发者_C百科og nothing if it is found, and then just a single line error of something similar to:

File AC\0d\234csf-234wfv-423vx-23fsdfs.jpg was not found

But right now it logs everything.

How can I achieve this?


You seem to use the command interactively so here's a solution to be used directly in the console:

for /f "delims=, tokens=1,2,3" %i in (C:\foo.csv) do dir "Y:\%i\%j\%k" >nul 2>nul || echo File "Y:\%i\%j\%k" was not found>> C:\temp\log.txt

A more readable multi-line version (to be used in a batch script, obviously, hence %% instead of %):

for /f "delims=, tokens=1,2,3" %%i in (C:\foo.csv) do (
dir "Y:\%%i\%%j\%%k" >nul 2>nul || echo File "Y:\%%i\%%j\%%k" was not found>> C:\temp\log.txt
)


You have to use if not exist. You should also put the delims at the end, to be able to specify space as a separator, besides the comma.

Here's what you need:

@echo off
for /f "delims=, tokens=1,2,3" %%i in (foo.csv) do (
    if not exist Y:\%%i\%%j\%%k (
        echo Y:\%%i\%%j\%%k doesn't exist >> log.txt
    )
)


try c:\ if /? and study the help shown , look for exists.

0

精彩评论

暂无评论...
验证码 换一张
取 消