上下文
我目前正在使用批处理文件进行编程,以使用仅适用于cmd的特定pdf工具。
问题
我正在尝试运行for循环,它循环遍历查找所有* .pdf文件的目录。排除名为“Originals”的pdf内部文件夹
如果pdf文件位于名为“Originals”的父文件夹中,则必须跳过该文件夹。否则使用%counter%变量计算pdf文件。
目录结构示例
C:\New folder\file (1).pdf
C:\New folder\file (2).pdf
C:\New folder\Sub_1\file (1).pdf
C:\New folder\Sub_1\file (2).pdf
C:\New folder\Sub_1\file (3).pdf
C:\New folder\Sub_2\file (4).pdf
C:\New folder\Sub_2\file (5).pdf
C:\New folder\Originals\file (1).pdf
C:\New folder\Originals\file (2).pdf
示例批处理文件 - 查找所有pdf(不包括原件)
:: Example.bat
@echo off
set myDirectory=C:\New folder
:: Search through myDirectory to find all .pdf files (including subdirectories)
setlocal enableDelayedExpansion
for /R "%myDirectory%" %%G in (*.pdf) do (
set inputDirectory=%%~dpG
echo G = !%%G!
echo inputDirectory = !inputDirectory!
for /f "delims=\" %%F in ("!inputDirectory!") do (
set currentFolder=%%~nxF
echo currentFolder = !currentFolder!
)
if NOT "!currentFolder!"=="Originals" (
set /a count=count+1
)
)
echo There are %count% PDF's (excluding originals)
pause
请运行示例批处理文件以演示到目前为止我所拥有的内容。任何帮助或解决方案将不胜感激。
干杯!
解决
以下是我根据每个人的解决方案提出的建议!
@echo off
setlocal enabledelayedexpansion
set count=0
set myDirectory=C:\New folder
for /r "%myDirectory%" %%i in (*.pdf) do (
set inputDirectory=%%~dpi
set inputDirectoryNoSlash=!inputDirectory:~0,-1!
for %%j in (!inputDirectoryNoSlash!) do set sub=%%~nxj
if NOT !sub!==Originals (
set /a count=count+1
)
)
echo There are %count% PDF's (excluding originals)
pause
再次感谢你们!
答案 0 :(得分:2)
另一种类似的方法是:
@echo off
setlocal enabledelayedexpansion
set count=0
set dir=C:\TEST
echo %count%
for /r "%dir%" %%i in (*.pdf) do (
set dirx=%%~dpi
set con=!dirx:~0,-1!
for %%j in (!con!) do set sub=%%~nxj
if !sub!==Originals (
cls
) else (
set /a count=count+1
)
)
echo !count!
pause
*更改为适当的路径名
答案 1 :(得分:1)
%%~pG
将%%G
扩展为仅限路径,包括尾随\
反斜杠。不受欢迎,请将其清除如下:
for /f "tokens=* delims=\" %%F in ("!inputDirectory:~0,-1!") do (
set currentFolder=%%~nxF
echo currentFolder = !currentFolder!
)
答案 2 :(得分:1)
试试这个:
@echo off
setlocal EnableDelayedExpansion
set myDirectory=C:\New folder
set count=0
for /F "delims=" %%a in ('dir /S "%myDirectory%\*.pdf" ^| findstr /V /L "\Originals\"') do (
echo File: %%a
set /A count+=1
)
echo There are %count% PDF's (excluding originals)
pause