filename变量导致循环中的计数器问题

时间:2015-07-10 15:34:33

标签: loops batch-file

情况:我每天都会收到名为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)制作我的第二行代码bat,创建我需要的所有文件的列表文件,然后重新工作以引用txt文件

echo on
start LISTFILE.bat

LISTFILE.bat

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

2)如何使用变量/列表/数组更好地设置文件名...但我不知道如何做到这一点。我是n00b程序员,将我在网上找到的其他东西拼接在一起。

提前感谢您提供任何帮助。

1 个答案:

答案 0 :(得分:0)

我改变了它的运行方式,而不是在for line上调用另一个bat程序的循环迭代。

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    ::暂停    退出/ B

final.bat几乎是一样的。