Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 批处理文件以检查文件是否存在于不包含';文件列表中不存在_Batch File - Fatal编程技术网

Batch file 批处理文件以检查文件是否存在于不包含';文件列表中不存在

Batch file 批处理文件以检查文件是否存在于不包含';文件列表中不存在,batch-file,Batch File,我有一个批处理文件,它根据文件列表递归我的文件夹,基本上创建两个报告。一个文件存在,一个文件不存在。 我现在想做的是找出一个文件是否存在于目录中,但不存在于文件列表中(即我是否有额外的文件) 这是我当前的文件 FOR /F "usebackqdelims=" %%f IN ("filelist.txt") DO (IF EXIST "%%f" (ECHO %%f exists >> "%~dp0\Exists.txt" ) ELSE (ECHO %%f doesn't exist &

我有一个批处理文件,它根据文件列表递归我的文件夹,基本上创建两个报告。一个文件存在,一个文件不存在。 我现在想做的是找出一个文件是否存在于目录中,但不存在于文件列表中(即我是否有额外的文件)

这是我当前的文件

FOR /F "usebackqdelims=" %%f IN ("filelist.txt") DO (IF EXIST "%%f" (ECHO %%f exists >> "%~dp0\Exists.txt" ) ELSE (ECHO %%f doesn't exist >> "%~dp0\doesntexist.txt" ))

标准Windows控制台应用程序FINDSTR对于此任务来说无疑是一个非常好的选择,因为下面的代码可以看出它

@echo off
setlocal
set "ListFiles=%~dp0FileList.txt"
set "ListExist=%~dp0Exist.txt"
set "ListMissing=%~dp0NotExist.txt"
set "ListExtra=%~dp0Extra.txt"
set "ListCurrent=%TEMP%\ListCurrent.tmp"

rem Get list of all files in current directory and its subdirectories with
rem full path into a temporary list file. If the current directory tree
rem contains no file, delete empty list file and exit batch processing.
dir /A-D /B /ON /S >"%ListCurrent%" 2>nul

if errorlevel 1 (
    copy "%ListFiles%" "%ListMissing%" >nul
    if exist "%ListExist%" del "%ListExist%"
    if exist "%ListExtra%" del "%ListExtra%"
    goto EndBatch
)

%SystemRoot%\System32\findstr.exe /I /L /X    /G:"%ListFiles%" "%ListCurrent%" >"%ListExist%"
%SystemRoot%\System32\findstr.exe /I /L /X /V /G:"%ListExist%" "%ListFiles%"   >"%ListMissing%"
%SystemRoot%\System32\findstr.exe /I /L /X /V /G:"%ListExist%" "%ListCurrent%" >"%ListExtra%"

:EndBatch
del "%ListCurrent%"
endlocal
此批处理代码要求批处理文件目录中的
FileList.txt
包含完整路径的文件名

注意:
%~dp0
扩展到已以反斜杠结尾的批处理文件路径。因此,不要在文件名之前指定额外的反斜杠

要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读为每个命令显示的所有帮助页面

  • 复制/?
  • del/?
  • dir/?
  • echo/?
  • endlocal/?
  • findstr/?
    。。。最重要的是要了解如何过滤列表
  • goto/?
  • 如果/?
  • rem/?
  • 设置/?
  • setlocal/?

有关
>nul
2>nul

的解释,请参阅Microsoft文章about:
findstr
命令:
findstr/X/L/C:“file_name”“filelist.txt”
如果在
filelist.txt
中找到
文件名
,则返回
0的
错误级别
,否则返回
1
;在命令提示窗口中键入
findstr/?
以获取帮助…谢谢大家-这一个几乎可以工作。现在的问题是,“额外文件”输出仅显示与批处理文件位于同一目录下的文件;它似乎没有在我的文件所在的目录结构中进行搜索,尽管存在/不存在报告。在'Filelist'txt'中,我有每个文件的路径,但仅从与批处理文件相关的位置开始(也就是说,在完整路径上,您没有向我们显示
filelist.txt
文件的内容,因此我们只能猜测,
for
命令只处理当前目录中的文件,因此您需要添加
/R
以这种方式切换到它:
for/R%%f in(**)do(
。请注意,文本文件中的名称必须与
for/R
返回的名称具有相同的格式;只需在命令提示符下使用
@echo%f
命令运行
for/R
,即可查看此格式。很抱歉,我的文件列表中存在大量NDA!该/R非常有效。现在我的问题是,整个文件夹的子文件夹都会递归与文件列表中列出的目录相反的目录。例如,假设我有一个子目录“Red”和“Gold”。我只想在“Red”中搜索文件,并在文件列表中包含该路径名。R还使其通过“Gold”目录。我猜我现在需要将Red和Gold设置为变量。。?
@echo off
setlocal

rem Load the list of filenames in *the subscripts* of an array
FOR /F "usebackq delims=" %%f IN ("filelist.txt") DO set "name["%%f"]=1"

rem Process files, remove elements of existent files from array
for %%f in (*.*) do (
   IF defined name["%%f"] (
      ECHO %%f exists >> "%~dp0Exists.txt"
      set "name["%%f"]="
   ) ELSE (
      ECHO %%f is extra >> "%~dp0ExtraFiles.txt"
   )
)

rem Report remaining elements in the array
for /F "tokens=2 delims=[]" %%f in ('set name[') do (
   ECHO %%~f doesn't exist >> "%~dp0doesntexist.txt"
)