2015-07-10 12 views
0

情況:我每天都會收到帶有名稱DFADV *(* =兩位或三位數字,沒有擴展名)的文件。我手動將它們歸檔一個月。 (我有單獨的.bat存檔每個月爲我)。每一天都可以是原始草稿文件,先前發送的草稿文件的第二次迭代或「最終」文件。然後我需要將它們移動到每個收件人文件夾路徑的指定FTP文件夾中,(7個收件人)。我有下面的代碼,這爲我做了這個,除了文件名變量是太泛型和混亂計數的迭代正確。文件名變量導致循環中的計數器出現問題

echo on 
rem searches each file by line for the word draft which indicates that 
rem files type, etc for final. else are the old files that haven't changed 
for /f %%i in ('findstr /i /m "draft" DFADV*.*') do move "%%i" Draft 
rem Pause 
for /f %%i in ('findstr /i /m "final" DFADV*.*') do move "%%i" Final 
rem Pause 
Move D:\FTP\Colby\Holding\DFADV*.* D:\FTP\Colby\Holding\Current 

set SourceD=D:\FTP\Colby\Holding\Draft\*.* 
set SourceF=D:\FTP\Colby\Holding\Final\*.* 
set SourceC=D:\FTP\Colby\Holding\Current\*.* 
set Dest001Draft=D:\FTP\Colby\Order001\Draft\ 
set Dest001Final=D:\FTP\Colby\Order001\Final\ 
set Dest001Curr=D:\FTP\Colby\Order001\Current\ 
set Dest007Draft=D:\FTP\Colby\Order007\Draft\ 
set Dest007Final=D:\FTP\Colby\Order007\Final\ 
set Dest007Curr=D:\FTP\Colby\Order007\Current\ 
set Dest030Draft=D:\FTP\Colby\Order030\Draft\ 
set Dest030Final=D:\FTP\Colby\Order030\Final\ 
set Dest030Curr=D:\FTP\Colby\Order030\Current\ 
set Dest032Draft=D:\FTP\Colby\Order032\Draft\ 
set Dest032Final=D:\FTP\Colby\Order032\Final\ 
set Dest032Curr=D:\FTP\Colby\Order032\Current\ 
set Dest033Draft=D:\FTP\Colby\Order033\Draft\ 
set Dest033Final=D:\FTP\Colby\Order033\Final\ 
set Dest033Curr=D:\FTP\Colby\Order033\Current\ 
set Dest124Draft=D:\FTP\Colby\Order124\Draft\ 
set Dest124Final=D:\FTP\Colby\Order124\Final\ 
set Dest124Curr=D:\FTP\Colby\Order124\Current\ 
set Dest126Draft=D:\FTP\Colby\Order126\Draft\ 
set Dest126Final=D:\FTP\Colby\Order126\Final\ 
set Dest126Curr=D:\FTP\Colby\Order126\Current\ 
set DestFileRecDraft=D:\FTP\Colby\Holding\FilesRec\Draft\ 
set DestFileRecFinal=D:\FTP\Colby\Holding\FilesRec\Final\ 
set DestFileRecCurr=D:\FTP\Colby\Holding\FilesRec\Current\ 
set Filename=*.* 
set a=0 
rem pause 
:loop 
if exist %Dest001Draft%\%Filename%(%a%).txt set /a a+=1 && goto :loop 
copy %SourceD% %Dest001Draft%\%Filename%(%a%).txt 
rem pause 
if exist %Dest001Final%\%Filename%(%a%).txt set /a a+=1 && goto :loop 
copy %SourceF% %Dest001Final%\%Filename%(%a%).txt 
rem Pause 
if exist %Dest001Curr%\%Filename%(%a%).txt set /a a+=1 && goto :loop 
copy %SourceC% %Dest001Curr%\%Filename%(%a%).txt 

if exist %Dest007Draft%\%Filename%(%a%).txt set /a a+=1 && goto :loop 
copy %SourceD% %Dest007Draft%\%Filename%(%a%).txt 
rem pause 
if exist %Dest007Final%\%Filename%(%a%).txt set /a a+=1 && goto :loop 
copy %SourceF% %Dest007Final%\%Filename%(%a%).txt 
rem Pause 
if exist %Dest007Curr%\%Filename%(%a%).txt set /a a+=1 && goto :loop 
copy %SourceC% %Dest007Curr%\%Filename%(%a%).txt 

if exist %Dest030Draft%\%Filename%(%a%).txt set /a a+=1 && goto :loop 
copy %SourceD% %Dest030Draft%\%Filename%(%a%).txt 
rem pause 
if exist %Dest030Final%\%Filename%(%a%).txt set /a a+=1 && goto :loop 
copy %SourceF% %Dest030Final%\%Filename%(%a%).txt 
rem Pause 
if exist %Dest030Curr%\%Filename%(%a%).txt set /a a+=1 && goto :loop 
copy %SourceC% %Dest030Curr%\%Filename%(%a%).txt 

if exist %Dest032Draft%\%Filename%(%a%).txt set /a a+=1 && goto :loop 
copy %SourceD% %Dest032Draft%\%Filename%(%a%).txt 
rem pause 
if exist %Dest032Final%\%Filename%(%a%).txt set /a a+=1 && goto :loop 
copy %SourceF% %Dest032Final%\%Filename%(%a%).txt 
rem Pause 
if exist %Dest032Curr%\%Filename%(%a%).txt set /a a+=1 && goto :loop 
copy %SourceC% %Dest032Curr%\%Filename%(%a%).txt 

if exist %Dest033Draft%\%Filename%(%a%).txt set /a a+=1 && goto :loop 
copy %SourceD% %Dest033Draft%\%Filename%(%a%).txt 
rem pause 
if exist %Dest033Final%\%Filename%(%a%).txt set /a a+=1 && goto :loop 
copy %SourceF% %Dest033Final%\%Filename%(%a%).txt 
rem Pause 
if exist %Dest033Curr%\%Filename%(%a%).txt set /a a+=1 && goto :loop 
copy %SourceC% %Dest033Curr%\%Filename%(%a%).txt 

if exist %Dest124Draft%\%Filename%(%a%).txt set /a a+=1 && goto :loop 
copy %SourceD% %Dest124Draft%\%Filename%(%a%).txt 
rem pause 
if exist %Dest124Final%\%Filename%(%a%).txt set /a a+=1 && goto :loop 
copy %SourceF% %Dest124Final%\%Filename%(%a%).txt 
rem Pause 
if exist %Dest124Curr%\%Filename%(%a%).txt set /a a+=1 && goto :loop 
copy %SourceC% %Dest124Curr%\%Filename%(%a%).txt 


if exist %Dest126Draft%\%Filename%(%a%).txt set /a a+=1 && goto :loop 
copy %SourceD% %Dest126Draft%\%Filename%(%a%).txt 
rem pause 
if exist %Dest126Final%\%Filename%(%a%).txt set /a a+=1 && goto :loop 
copy %SourceF% %Dest126Final%\%Filename%(%a%).txt 
Rem Pause 
if exist %Dest126Curr%\%Filename%(%a%).txt set /a a+=1 && goto :loop 
copy %SourceC% %Dest126Curr%\%Filename%(%a%).txt 

if exist %DestFileRecDraft%\%Filename%(%a%).txt set /a a+=1 && goto :loop 
Copy %SourceD% %DestFileRecDraft%\%Filename%(%a%).txt 
rem pause 
if exist %DestFileRecFinal%\%Filename%(%a%).txt set /a a+=1 && goto :loop 
Copy %SourceF% %DestFileRecFinal%\%Filename%(%a%).txt 
rem Pause 
if exist %DestFileRecCurr%\%Filename%(%a%).txt set /a a+=1 && goto :loop 
copy %SourceC% %DestFileRecCurr%\%Filename%(%a%).txt 

pause 

Del %SourceD% 
Del %SourceF% 
Del %SourceC% 

Pause 

下面是我的問題,

DFADV86.(0) {NOTE: DAY 1} 
DFADV64.(1) {NOTE: would be added second day if there were no other files 
        and this is the first time receiving DV64} 

第二天我運行此我得到了第二次迭代「(1)」,即使再文件不存在。 我相信這是找到一個匹配我

set Filename=*.* 

並不算作匹配。我該如何着手搜索保存文件夾中的每個文件特別是

可能的解決方案我不知道如何開展工作。

1)使創建我需要並重新工作這個引用txt文件

echo on 
start LISTFILE.bat 

LISTFILE.bat中的所有文件的列表文件代碼蝙蝠我的第二線

dir /b D:\FTP\Colby\Holding\DFADV*.* > fileslist.txt 
exit 

2)如何使用變量/列表/數組更好地設置文件名......但我不知道該怎麼做。我是n00b程序員,將我在網上找到的其他東西拼接在一起。

非常感謝您提供任何幫助。

+0

我會在開始處添加'setlocal enableDelayedExpansion'行,並用'!a!'替換所有'%a%'並在文件名周圍使用引號('「%Dest001Draft%\%Filename%(!a!)。 txt「')你可以試試嗎? – wOxxOm

+0

今天晚些時候美國東部時間7:20將試一試並告訴你一些事情。謝謝您的幫助! – user3249562

+0

@wOxxOm當我添加您建議的更改時,它會在退出之前在相同的文件上循環10次。這意味着它會創建相同文件的10個副本。 。(0)通過。(9)...我嘗試了組合,即沒有引號...沒有setlocal ....沒有「!a!」如果存在%Dest001Draft%\%Filename%(%a%).txt set/a a + = 1 && goto:loop' 'copy %SourceD%%Dest001Draft%\%Filename%(%a%).txt' 哪些引用 'set Filename = *。*' 這是我相信我的(文件在任何文件上找到匹配項)錯誤的地方。 我的問題(我相信)是如何檢查具體文件 – user3249562

回答

0

我改變了它的運行方式,而不是循環線上的迭代我調用了另一個蝙蝠程序。

echo on 8:30 AM 7/16/2015 
:: search files for string draft 
::pause 
for /f %%i in ('findstr /i /m "draft" DFADV*.*') do call draft.bat "%%~i" 
::pause 
:: search files for string final 
for /f %%i in ('findstr /i /m "final" DFADV*.*') do call Final.bat "%%~i" 
::pause 
:: if neither draft or final is found move to the current folder, 
:: these files havent been 
:: converted. should be a two digit division number. 
:: If a three digit division code is 
:: found there is an issue with file or batch script refernce to draft or final. 
:: moving old format to current root folder 
::pause 
Move D:\FTP\Colby\Holding\DFADV*.* D:\FTP\Colby\Holding\Current 
:: moving old format Dairylea 
::pause 
Move D:\FTP\Colby\Holding\Div*.* D:\FTP\Colby\Holding\Current 
:: set the file paths variables for the batch script 
::pause 
set SourceD=D:\FTP\Colby\Holding\Draft\ 
set SourceF=D:\FTP\Colby\Holding\Final\ 
set SourceC=D:\FTP\Colby\Holding\Current\ 
set Dest001Draft=D:\FTP\Colby\Order001\Draft\ 
set Dest001Final=D:\FTP\Colby\Order001\Final\ 
set Dest001Curr=D:\FTP\Colby\Order001\Current\ 
set Dest007Draft=D:\FTP\Colby\Order007\Draft\ 
set Dest007Final=D:\FTP\Colby\Order007\Final\ 
set Dest007Curr=D:\FTP\Colby\Order007\Current\ 
set Dest030Draft=D:\FTP\Colby\Order030\Draft\ 
set Dest030Final=D:\FTP\Colby\Order030\Final\ 
set Dest030Curr=D:\FTP\Colby\Order030\Current\ 
set Dest032Draft=D:\FTP\Colby\Order032\Draft\ 
set Dest032Final=D:\FTP\Colby\Order032\Final\ 
set Dest032Curr=D:\FTP\Colby\Order032\Current\ 
set Dest033Draft=D:\FTP\Colby\Order033\Draft\ 
set Dest033Final=D:\FTP\Colby\Order033\Final\ 
set Dest033Curr=D:\FTP\Colby\Order033\Current\ 
set Dest124Draft=D:\FTP\Colby\Order124\Draft\ 
set Dest124Final=D:\FTP\Colby\Order124\Final\ 
set Dest124Curr=D:\FTP\Colby\Order124\Current\ 
set Dest126Draft=D:\FTP\Colby\Order126\Draft\ 
set Dest126Final=D:\FTP\Colby\Order126\Final\ 
set Dest126Curr=D:\FTP\Colby\Order126\Current\ 
set DestFileRecDraft=D:\FTP\Colby\Holding\FilesRec\Draft\ 
set DestFileRecFinal=D:\FTP\Colby\Holding\FilesRec\Final\ 
set DestFileRecCurr=D:\FTP\Holding\FilesRec\Current\ 
::pause 
::copy all files in holding draft folder to each orders correct draft folder 
Copy %SourceD%*.* %Dest001Draft% 
Copy %SourceD%*.* %Dest007Draft% 
Copy %SourceD%*.* %Dest030Draft% 
Copy %SourceD%*.* %Dest032Draft% 
Copy %SourceD%*.* %Dest033Draft% 
Copy %SourceD%*.* %Dest124Draft% 
Copy %SourceD%*.* %Dest126Draft% 
::copy all files in holding\Final folder to each orders correct Final folder 
Copy %SourceF%*.* %Dest001Final% 
Copy %SourceF%*.* %Dest007Final% 
Copy %SourceF%*.* %Dest030Final% 
Copy %SourceF%*.* %Dest032Final% 
Copy %SourceF%*.* %Dest033Final% 
Copy %SourceF%*.* %Dest124Final% 
Copy %SourceF%*.* %Dest126Final% 
::copy all files in holding\Cur folder to each orders correct Current folder 
Copy %SourceC%*.* %Dest001Curr% 
Copy %SourceC%*.* %Dest007Curr% 
Copy %SourceC%*.* %Dest030Curr% 
Copy %SourceC%*.* %Dest032Curr% 
Copy %SourceC%*.* %Dest033Curr% 
Copy %SourceC%*.* %Dest124Curr% 
Copy %SourceC%*.* %Dest126Curr% 
::move each itteration to the files received corresponidng folders for the month. 
move %SourceD%*.* %DestFileRecDraft% 
move %SourceD%*.* %DestFileRecFinal% 
move %SourceD%*.* %DestFileRecCurr% 

這裏是draft.bat

:: draft.bat 8:32 AM 7/16/2015 
:: echo off 
:: echo the file passed is "%~1".(0) 
echo on 
set filea=%~1 
::pause 
echo:%filea% 
::checking to see if this file has been processed 
::the move command also renames to correct version of the file 
::pause 
:: echo if exist D:\FTP\Colby\Order001\Draft\%filea%.(0) goto check(1) 
::pause 
if exist D:\FTP\Colby\Order001\Draft\%filea%.(0) goto check(1) 
::pause 
:: echo move D:\FTP\Colby\Holding\%filea% D:\FTP\Colby\Holding\Draft\%filea%.(0) 
move D:\FTP\Colby\Holding\%filea% D:\FTP\Colby\Holding\Draft\%filea%.(0) 
::pause 
GOTO EOF 

::checking to see if this file has been processed twice 
:check(1) 
if exist D:\FTP\Colby\Order001\Draft\%filea%.(1) goto check(2) 
move D:\FTP\Colby\Holding\%filea% D:\FTP\Colby\Holding\Draft\%filea%.(1) 
GOTO EOF 

::checking to see if this file has been processed three times 
:check(2) 
if exist D:\FTP\Colby\Order001\Draft\%filea%.(2) goto check(3) 
move D:\FTP\Colby\Holding\%filea% D:\FTP\Colby\Holding\Draft\%filea%.(2) 
GOTO EOF 

::checking to see if this file has been processed four times 
:check(3) 
if exist D:\FTP\Colby\Order001\Draft\%filea%.(3) goto check(4) 
move D:\FTP\Colby\Holding\%filea% D:\FTP\Colby\Holding\Draft\%filea%.(3) 
GOTO EOF 
::checking to see if this file has been processed five times 
:check(4) 
if exist D:\FTP\Colby\Order001\Draft\%filea%.(4) goto check(5) 
move D:\FTP\Colby\Holding\%filea% D:\FTP\Colby\Holding\Draft\%filea%.(4) 
GOTO EOF 
::checking to see if this file has been processed six times 
:check(5) 
if exist D:\FTP\Colby\Order001\Draft\%filea%.(5) goto ECHO1 
move D:\FTP\Colby\Holding\%filea% D:\FTP\Colby\Holding\Draft\%filea%.(5) 
GOTO EOF 

:ECHO1 
ECHO "Error you have it set up for 0-5 you need more itterations!" 
::pause 

:EOF ::暫停 EXIT/B

final.bat幾乎是相同的。