繁体   English   中英

Windows Batch脚本可查找具有特定扩展名的文件,并将其目录写入文本文件

[英]Windows Batch script to find files with specific extensions, write their directories to a text file

首先,我是批处理文件的新手。

  • 我想找到所有扩展名为.doc或.pdf的文件
  • 第一次运行时,我要将包含这些文件的目录保存到文本文件中
    • 不创建重复条目
  • 然后,我想从该文件中读取并复制所有列出的目录。

基本上,我正在创建一个备份程序,该程序根据其中包含的文件查找某些目录。 它会保存此列表,因此下次运行时无需再次搜索。

直到本周晚些时候,我才可以使用Windows钻机,因此该代码未经测试/伪代码。

for /R "c:\my\dir" %%f in (*.pdf | *.doc) do (
    set "folderPath=%%f"
    echo %folderPath%>> directoryList.txt
)

load %readPath% from directoryList.txt   ::Not sure how to read lines one by one?
if %readPath% != eof (
    xcopy %folderPath% %writePath% /e/v/c/y/h/r/d >> %logFile%
)

下一个脚本可能成为您的起点。 通过rem注释在代码中进行解释。

@ECHO OFF >NUL
    rem set up output directory
set "writePath=c:\my\writedir"

    rem create empty file
type nul>directoryList.txt

    rem get a list of unique folder names
    rem might contain recursion in names, e.g.
    rem c:\my\dir\foo 
    rem c:\my\dir\foo\subfoo 
for /F "tokens=*" %%f in ('dir /B /A:D /S "c:\my\dir\*.*"') do (
    if exist "%%~ff\*.doc" ( 
        echo "%%~ff">> directoryList.txt
    ) else (
        if exist "%%~ff\*.pdf" echo "%%~ff">> directoryList.txt
    )
)
    rem treat the list of unique names
    rem check the list, compare to output 
for /F "tokens=*" %%f in ('type directoryList.txt') do (
        rem check next line otput carefully before removing echo
    echo xcopy "%%~f" "%writePath%" /e/v/c/y/h/r/d
        rem ? or with trailing backslashes "%%~f\"  "%writePath%\"
        rem ? or with trailing \ escaped   "%%~f^\" "%writePath%^\"
        rem not tested: XCOPY logic, paths, switches
        rem not tested: recursion in input file
)

资源:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM