2017-07-08 68 views
-1

裏面的文件夾名稱例如我需要一個批處理程序找到另一個文件夾

考慮:

H:\reports\test\12345\dailynews.pdf 

我想通過郵件使用MSSQL作業(SQL服務器2008 R2)發送這個PDF。 但每當其他作業創建pdf文件時,位置都會改變。 像

H:\reports\test\48596\dailynews.pdf 

我需要一個批處理程序,以找到該文件夾​​的名稱就是測試文件夾後存在。

+0

什麼是你的問題? [SO]不是腳本編寫服務,而是程序員在遇到明顯問題時幫助其他程序員的網站。 – LotPings

+0

你好Lotpings ...我知道堆棧溢出不是腳本編寫服務。 – Guru

+0

如果你沒有記錄你自己的發佈代碼**的努力,那麼你的**看起來像是爲你寫代碼的請求 - 這可能會導致你被拒絕,並且由於過於寬泛而關閉該問題。 – LotPings

回答

1

對於給定的信息,我會建議 處裂開了反斜槓字符串並提取這樣,第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_ 

性能可能會變得更糟。

+2

這是對問題**的一個很好的回答,而不是**問 - 這是一個功能請求。爲了得到'p',你應該首先遞歸地搜索'For/R'H:\ reports \ test「%% P in(dailynews.pdf)Do ...' – LotPings

+0

嗯,我假設/」讀取「這個問題:在測試後找到文件夾名稱?「但謝謝你指出。 – toschug

+0

請解釋downvote。 – toschug

1

遞歸檢查可能會報告樹中其他位置存在的相同命名文件,並且可能需要更長的時間。我的建議是用這個代替:

@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% 
相關問題