里面的文件夹名称例如我需要一个批处理程序找到另一个文件夹
考虑:
H:\reports\test\12345\dailynews.pdf
我想通过邮件使用MSSQL
作业(SQL服务器2008 R2)发送这个PDF
。 但每当其他作业创建pdf
文件时,位置都会改变。 像
H:\reports\test\48596\dailynews.pdf
我需要一个批处理程序,以找到该文件夹的名称就是测试文件夹后存在。
里面的文件夹名称例如我需要一个批处理程序找到另一个文件夹
考虑:
H:\reports\test\12345\dailynews.pdf
我想通过邮件使用MSSQL
作业(SQL服务器2008 R2)发送这个PDF
。 但每当其他作业创建pdf
文件时,位置都会改变。 像
H:\reports\test\48596\dailynews.pdf
我需要一个批处理程序,以找到该文件夹的名称就是测试文件夹后存在。
对于给定的信息,我会建议 处裂开了反斜杠字符串并提取这样,第4单元:
set p=H:\reports\test\12345\dailynews.pdf
for /F "delims=\ tokens=4" %%a in ("%p%") do echo %%a
注意双引号("%p%")
,所以它被解释为字符串(而不是作为文件名或命令)。
如果你想确保,即第三个元素是test
文件夹,然后我会建议:
set p=H:\reports\test\12345\dailynews.pdf
set o=H:\reports\live\55555\dailynews.pdf
for /F "delims=\ tokens=3,4" %%a in ("%p%") do if "%%a"=="test" echo %%b
for /F "delims=\ tokens=3,4" %%a in ("%o%") do if "%%a"=="test" echo %%b
输出12345
但不55555
,这是在live
文件夹中。
编辑: 我意识到,我的回答冷落完全,如何能够检测到最新dailynews.pdf
,这是什么OP可能寻找。 就像dailynews例程在21.00运行一样,并且你想在22.00邮寄该报告。 (再次,又一个假设)
IMO,棘手的部分是解析和比较文件的日期/时间信息。 一个简单的dir dailynews.txt /S /O:-D
不适用于subdirs。
所以......如果你不在一段时间清理BASEDIR
@echo off
cls
set base=H:\reports\test
set filename=dailynews.pdf
set current_date=19000101
set current_time=0000
set current_file=
::debug output
dir "%filename%" /S /O:-D
:: inspect each entry
for /F "usebackq" %%a in (`dir "%filename%" /S /B /O:-D`) do call :inspect "%%a"
:: all done, quit
goto :quit
:: subroutine for comparing current with actual newest entry
:inspect
echo %1
set _date=
set _time=
:: get comparable date time info
:: change to your regional date/time settings/locale! (german here)
:: maybe check SO for other locales
for /F "usebackq tokens=1,2,3,4,5 delims=.: " %%a in (`dir %1^|findstr "%filename%"`) do echo %%a %%b %%c %%d %%e&set _date=%%c%%b%%a&set _time=%%d%%e
:: we have to split in two components, otherwise it seems too big for comparing
if /I %_date% LSS %current_date% goto :eof
if /I %_time% LSS %current_time% goto :eof
:: this one is newer! set new values
set current_date=%_date%
set current_time=%_time%
set current_file=%1
goto :eof
:quit
set current_
性能可能会变得更糟。
递归检查可能会报告树中其他位置存在的相同命名文件,并且可能需要更长的时间。我的建议是用这个代替:
@Echo Off
Set "MyFile="
For /D %%A In ("H:\reports\test\*"
) Do If Exist "%%A\dailynews.pdf" Set "MyFile=%%~fA\dailynews.pdf"
If Not Defined MyFile Exit/B
Echo %%MyFile%% = %MyFile%
什么是你的问题? [SO]不是脚本编写服务,而是程序员在遇到明显问题时帮助其他程序员的网站。 – LotPings
你好Lotpings ...我知道堆栈溢出不是脚本编写服务。 – Guru
如果你没有记录你自己的发布代码**的努力,那么你的**看起来像是为你写代码的请求 - 这可能会导致你被拒绝,并且由于过于宽泛而关闭该问题。 – LotPings