[英]Windows Batch script to find files with specific extensions, write their directories to a text file
首先,我是批处理文件的新手。
基本上,我正在创建一个备份程序,该程序根据其中包含的文件查找某些目录。 它会保存此列表,因此下次运行时无需再次搜索。
直到本周晚些时候,我才可以使用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
)
资源:
~
参数修饰符 for /F
循环针对另一个命令的结果
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.