我们有客户端在共享目录中删除的文件(没有扩展名):
\\\Server\SharedDirectory\BRSURVEY802DATA
\\\Server\SharedDirectory\BRSURVEYLOGINDATA
\\\Server\SharedDirectory\BRSURVEYLOGOUTDATA
这些文件必须重命名并移动到其正确的目录中,如下所示:
\\\Server\SharedDirectory\802\ORDDFILE_&DATE#&TIME#.802
\\\Server\SharedDirectory\SurveyLogin\ORDDFILE_&DATE#&TIME#.LOGIN
\\\Server\SharedDirectory\SurveyLogout\ORDDFILE_&DATE#&TIME#.LOGOUT
任何其他文件:
\\\Server\SharedDirectory\Rejected\ORDDFILE_&DATE#&TIME#.REJECTED
我尝试了以下操作,其中一部分运行正常:
! Setting time variables
@SET DATEFORMAT# ='YYYYMMDD'
@SET DATE# = SYS_DATE(DATEFORMAT#)
@SET TIMEFORMAT# = 'HHMMSS'
@SET TIME# = SYS_TIME(TIMEFORMAT#)
! Input directory values saved in VARA object
: PRINT 'SOURCEDIR = &ORDDFilesFromLocation#'
: PRINT '802DIR = &ORDDFilesToLocation802#'
: PRINT 'LOGINDIR = &ORDDFilesToLocationLogin#'
: PRINT 'LOGOUTDIR = &ORDDFilesToLocationLogout#'
: PRINT 'REJECTEDDIR = &ORDDFilesToLocationRejected#'
! Local variables
@SET INPUT802=&ORDDFilesFromLocation#BRSURVEY802DATA
@SET INPUTLOGIN=&ORDDFilesFromLocation#BRSURVEYLOGINDATA
@SET INPUTLOGOUT=&ORDDFilesFromLocation#BRSURVEYLOGOUTDATA
@SET OUTPUT802=&ORDDFilesToLocation802#ORDDFile_DATE#TIME#.802
@SET OUTPUTLOGIN=&ORDDFilesToLocationLogin#ORDDFile_DATE#TIME#.LOGIN
@SET OUTPUTLOGOUT=&ORDDFilesToLocationLogin#ORDDFile_DATE#TIME#.LOGOUT
@SET OUTPUTREJECTED=&ORDDFilesToLocationRejected#ORDDFile_DATE#TIME#.REJECTED
!! *** Rename and move files to destination folder ***
! 802
if exist %INPUT802% (
for %%I in ("%INPUT802%") do move %%I "%OUTPUT802%"
)
! Login
if exist %INPUTLOGIN% (
for %%I in ("%INPUTLOGIN%") do move %%I "%OUTPUTLOGIN%"
)
! Logout
if exist %INPUTLOGOUT% (
for %%I in ("%INPUTLOGOUT%") do move %%I "%OUTPUTLOGOUT%"
)
ECHO End of Program
现在我必须处理必须移动并重命名为被拒绝的其他文件。有人能帮我吗?
根据您的回答,我尝试了以下操作:
@echo off & Setlocal EnableExtension
for /f useback^delims^= %%i in (`dir /a:-d /b "\\Fld6filer\brdProduction_SurveyFiles$\Incoming\ORDD\DEV\"`) do
if /i "%%~ni" == "BRSURVEY802DATA" (
move "%%~fi" "\\fld6filer\BRDProduction_SurveyFiles$\Incoming\ORDD\DEV\802\ORDDFILE_20230319215412.802"
)else if /i "%%~ni" == "BRSURVEYLOGINDATA" (
move "%%~fi" "\\fld6filer\BRDProduction_SurveyFiles$\Incoming\ORDD\DEV\ORDDFILE_20230319215412.LOGIN"
)else if /i "%%~ni" == "BRSURVEYLOGOUTDATA" (
move "%%~fi" "\\fld6filer\BRDProduction_SurveyFiles$\Incoming\ORDD\DEV\ORDDFILE_20230319215412.LOGOUT"
)else move "%%~fi" "\\fld6filer\BRDProduction_SurveyFiles$\Incoming\ORDD\DEV\ORDDFILE_20230319215412.UNKNOWN"
endlocal
我收到错误消息:命令的语法不正确。
因此我进行了一些研究,并使用 echos 修改了代码,以跟踪日志中发生的情况:
for /f %%i in ('dir /b "\\Fld6filer\brdProduction_SurveyFiles$\Incoming\ORDD\DEV\"') do (
if "%%~xi" == "BRSURVEY802DATA" do (
echo I am in
move "\\fld6filer\BRDProduction_SurveyFiles$\Incoming\ORDD\DEV\BRSURVEY802DATA" "\\fld6filer\BRDProduction_SurveyFiles$\Incoming\ORDD\DEV\802\ORDDFILE_20230319215412.802"
)
else if "%%~xi" == "BRSURVEYLOGINDATA" do (
echo I am in Login
move "\\fld6filer\BRDProduction_SurveyFiles$\Incoming\ORDD\DEV\BRSURVEYLOGINDATA" "\\fld6filer\BRDProduction_SurveyFiles$\Incoming\ORDD\DEV\LOGIN\ORDDFILE_20230319215412.LOGIN"
)
else if "%%~xi" == "BRSURVEYLOGOUTDATA" do (
echo I am in Logout
move "\\fld6filer\BRDProduction_SurveyFiles$\Incoming\ORDD\DEV\BRSURVEYLOGOUTDATA" "\\fld6filer\BRDProduction_SurveyFiles$\Incoming\ORDD\DEV\LOGOUT\ORDDFILE_20230319215412.LOGOUT"
)
else do (
ECHO I am in Unknown
move "\\fld6filer\BRDProduction_SurveyFiles$\Incoming\ORDD\DEV\*.*" "\\fld6filer\BRDProduction_SurveyFiles$\Incoming\ORDD\DEV\REJECTED\ORDDFILE_20230319215412.UNKNOWN"
)
)
这按预期工作。但是,日志中包含我一点也不喜欢的消息,我不明白为什么会发生这种情况...
I am in
1 file(s) moved.
I am in
The system cannot find the file specified.
I am in
The system cannot find the file specified.
I am in
The system cannot find the file specified.
I am in
The system cannot find the file specified.
I am in
The system cannot find the file specified.
I am in
The system cannot find the file specified.
I am in
The system cannot find the file specified.
'else' is not recognized as an internal or external command,
operable program or batch file.
I am in Login
1 file(s) moved.
'else' is not recognized as an internal or external command,
operable program or batch file.
I am in Logout
1 file(s) moved.
'else' is not recognized as an internal or external command,
operable program or batch file.
I am in Unknown
\\fld6filer\BRDProduction_SurveyFiles$\Incoming\ORDD\DEV\BLABLABLA
1 file(s) moved.
首先,源目录中有 4 个目录和 4 个文件。看起来所有对象都经过了第一个 IF 语句。此外,ELSE 未被识别,但最终还是完成了工作。有什么想法吗?
迈琳
答案1
@echo off & Setlocal EnableExtensions
for /f usebackq^tokens^=4delims^=^>^< %%i in (`
%__AppDir__%wbem\WMIC.exe OS Get LocalDateTime /format:xml ^| find "VALUE"
`)do set "_dt=%%~i"
set "_date_time=%_dt:~0,4%%_dt:~4,2%%_dt:~6,2%%_dt:~8,2%%_dt:~10,4%"
pushd "\\Fld6filer\brdProduction_SurveyFiles$\Incoming\ORDD\DEV\" || endlocal && goto :eOf
set "_login=\\fld6filer\BRDProduction_SurveyFiles$\Incoming\ORDD\DEV\ORDDFILE_%_date_time%.LOGIN"
set "_logout=\\fld6filer\BRDProduction_SurveyFiles$\Incoming\ORDD\DEV\ORDDFILE_%_date_time%.LOGOUT"
set "_802Dir=\\fld6filer\BRDProduction_SurveyFiles$\Incoming\ORDD\DEV\802\ORDDFILE_%_date_time%.802"
set "_unknown=\\fld6filer\BRDProduction_SurveyFiles$\Incoming\ORDD\DEV\ORDDFILE_%_date_time%.UNKNOWN"
for /f useback^delims^= %%i in (`dir /a-d /b
`)do if /i "%%~ni" == "brsurvey802data" (
echo/I am in .802
move "%%~fi" "%_802Dir%"
)else if /i "%%~ni" == "brsurveyLOGINdata" (
echo/I am in .Login
move "%%~fi" "%_login%"
)else if /i "%%~ni" == "brsurveyLOGOUTdata" (
echo/I am in .Logout
move "%%~fi" "%_logout%"
)else echo/I am in .unknowm && move "%%~fi" "%_unknown%"
endlocal & popd
%__AppDir__%timeout.exe /t 05 | echo\End of Program
你可以试试:
for /f Loop (your files)do if 802 (
command ...
)else if Login (
command ...
)else if Logout (
command ...
)else command .. // ! Rejected
观察1:您可以通过添加来测试和验证命令的行为echo\Move Command
什么也不会被移动:
@echo off & Setlocal EnableExtensions
for /f usebackq^tokens^=4delims^=^>^< %%i in (`
%__AppDir__%wbem\WMIC.exe OS Get LocalDateTime /format:xml ^| find "VALUE"
`)do set "_dt=%%~i"
set "_date_time=%_dt:~0,4%%_dt:~4,2%%_dt:~6,2%%_dt:~8,2%%_dt:~10,4%"
pushd "\\Fld6filer\brdProduction_SurveyFiles$\Incoming\ORDD\DEV\" || endlocal && goto :eOf
set "_login=\\fld6filer\BRDProduction_SurveyFiles$\Incoming\ORDD\DEV\ORDDFILE_%_date_time%.LOGIN"
set "_logout=\\fld6filer\BRDProduction_SurveyFiles$\Incoming\ORDD\DEV\ORDDFILE_%_date_time%.LOGOUT"
set "_802Dir=\\fld6filer\BRDProduction_SurveyFiles$\Incoming\ORDD\DEV\802\ORDDFILE_%_date_time%.802"
set "_unknown=\\fld6filer\BRDProduction_SurveyFiles$\Incoming\ORDD\DEV\ORDDFILE_%_date_time%.UNKNOWN"
for /f useback^delims^= %%i in (`dir /a-d /b
`)do if /i "%%~ni" == "brsurvey802data" (
echo/I am in .802
echo\move "%%~fi" "%_802Dir%"
)else if /i "%%~ni" == "brsurveyLOGINdata" (
echo/I am in .Login
echo\move "%%~fi" "%_login%"
)else if /i "%%~ni" == "brsurveyLOGOUTdata" (
echo/I am in .Logout
echo\move "%%~fi" "%_logout%"
)else echo/I am in .unknowm && echo\move "%%~fi" "%_unknown%"
endlocal & popd
%__AppDir__%timeout.exe /t 05 | echo\End of Program
观察2:我不确定我是否理解您的问题,因为它与变量定义和路径组成有关。
然而,日志中包含一些我不喜欢的消息
您可以省略标准输出和/或错误以多种方式输出:stdout = 1 Text output stderr = 2 Error text output ---------------------------------- command >nul command >nul 2>&1 command >nul 2>nul command 1>nul command 2>nul command 2>&1 command 1>nul 2>&1 command 1>nul 2>nul for /f useback^delims^= %%i in (`dir /a-d /b `)do if /i "%%~ni" == "brsurvey802data" 1>nul ( move "%%~fi" "%_802Dir%" )else if /i "%%~ni" == "brsurveyLOGINdata" 1>nul ( move "%%~fi" "%_login%" )else if /i "%%~ni" == "brsurveyLOGOUTdata" 1>nul ( move "%%~fi" "%_logout%" )else move "%%~fi" "%_unknown%" 1>nul
进一步阅读:
-
|
,<
,>
,2>
,2>&1
, ETC。
答案2
恐怕我不能完全理解你剧本中的一些细微差别。
但我相信您可以将其添加到脚本的末尾,以移动被拒绝的文件。
@SET SOURCEDIR=&ORDDFilesFromLocation#
for %%I in ("%SOURCEDIR%") do (
move %%I "%OUTPUTREJECTED%"
: Since name already used - we need to regenerate name with new timestamp
: to guarantie differnce in timestamp - sleep for 1 seccond
timeout /t 1 /nobreak >nul
@SET OUTPUTREJECTED=&ORDDFilesToLocationRejected#ORDDFile_DATE#TIME#.REJECTED
)