Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 复制\删除批处理无法正常工作_Batch File_Syntax - Fatal编程技术网

Batch file 复制\删除批处理无法正常工作

Batch file 复制\删除批处理无法正常工作,batch-file,syntax,Batch File,Syntax,此批处理所做的是设置错误日志、设置DoW和日期\时间、复制特定文件(维护源和目标之间的目录结构),并根据保留期清理目标。除了错误记录之外,一切都正常。问题在于,它没有按我希望的方式将文本输出到日志中,而是通过像应该的那样转到出口跳过所有内容(例如,它确实输出开始日期\时间,出口语句“进程已完成”,和结束日期\时间-但不是上面的任何错误文本,即使遇到错误代码,它仍会处理清理) 我知道我有点不对劲,但我没办法弄清楚(而且我查到的所有东西都不起作用——包括和我们组织中的专业开发人员交谈)。我希望这里的

此批处理所做的是设置错误日志、设置DoW和日期\时间、复制特定文件(维护源和目标之间的目录结构),并根据保留期清理目标。除了错误记录之外,一切都正常。问题在于,它没有按我希望的方式将文本输出到日志中,而是通过像应该的那样转到出口跳过所有内容(例如,它确实输出开始日期\时间,出口语句“进程已完成”,和结束日期\时间-但不是上面的任何错误文本,即使遇到错误代码,它仍会处理清理)

我知道我有点不对劲,但我没办法弄清楚(而且我查到的所有东西都不起作用——包括和我们组织中的专业开发人员交谈)。我希望这里的人能对这件事有一些想法(是的,我知道可能有更好的方法来实现这一点,但这是我所知道的,也是我从现有批处理文件和在线研究中拼凑出来的,所以我不必重新发明轮子——这是一个快速简单的解决方案,可以满足从一个位置复制文件到另一个位置的特定需要,保持目标位置不变。)到目前为止,我已经能够解决除此之外的其他问题。任何建议都将不胜感激

编辑:所以我发现没有复制文件时返回的errorlevel为0。但是如果现在是这样,那么命令的语法引用不仅不正确,而且我不知道如何让它处理正确的退出(它复制了找到的所有新文件,现在需要完成清理部分并从那里继续)

    @echo off
    SET ERRLOG=c:\backupcopy2.log

    :: ------------------ Date and Time Modifier ------------------------
    :: THIS CODE WILL DISPLAY A 2-DIGIT TIMESTAMP
    :: CREATE VARIABLE %TIMESTAMP%
    for /f "tokens=1-8 delims=.:/-, " %%i in ('echo exit^|cmd /q /k"prompt $D $T"') do (
       for /f "tokens=2-4 delims=/-,() skip=1" %%a in ('echo.^|date') do (
    set dow=%%i
    set mm=%%j
    set dd=%%k
    set yy=%%l
    set hh=%%m
    set min=%%n
    set sec=%%o
    set hsec=%%p
    )
    )
    :: ensure that hour is always 2 digits
    if %hh%==0 set hh=00
    if %hh%==1 set hh=01
    if %hh%==2 set hh=02
    if %hh%==3 set hh=03
    if %hh%==4 set hh=04
    if %hh%==5 set hh=05
    if %hh%==6 set hh=06
    if %hh%==7 set hh=07
    if %hh%==8 set hh=08
    if %hh%==9 set hh=09
    :: assign timeStamp:
    :: Add the date and time parameters as necessary - " yy-mm-dd-dow-min-sec-hsec "

    set timeStamp=%mm%%dd%%yy%_%hh%%min%%sec%


    :: --------- TIME STAMP DIAGNOSTICS -------------------------

    :: Un-comment these lines to test output

    :: echo dayOfWeek = %dow%
    :: echo year = %yy%
    :: echo month = %mm%
    :: echo day = %dd%
    :: echo hour = %hh%
    :: echo minute = %min%
    :: echo second = %sec%
    :: echo hundredthsSecond = %hsec%
    :: echo.
    :: echo Hello! 
    :: echo Today is %dow%, %mm%/%dd%/%yy%. 
    :: echo.
    :: echo Your timestamp will look like this: %timeStamp%
    :: echo. 
    :: echo.
    :: echo.
    :: pause

    :: --------- END TIME STAMP DIAGNOSTICS ----------------------

    :: Log start time
    @echo === %dow% %timestamp% : Start === >> %ERRLOG%


    :: Copy latest BAK files from X to Z and retain folder structure
    xcopy x:\*.pbk z:\ /v /q /d /i /s

    IF %ERRORLEVEL% EQU 1 goto nofiles
    IF %ERRORLEVEL% EQU 4 goto lowmemory
    IF %ERRORLEVEL% EQU 5 goto writeerror
    IF %ERRORLEVEL% EQU 0 goto cleanup

    :nofiles
    echo No files were found to copy >> %ERRLOG%
    goto exit

    :lowmemory 
    echo Insufficient memory to copy files or invalid drive or command-line syntax >> %ERRLOG%
    goto exit 

    :writeerror
    echo A disk write error occurred >> %ERRLOG%
    goto exit

    :: Deletes files older than 30 days - modify the /d to change the retention period
    :cleanup
    for /D %%X IN (z:\0*.) DO forfiles /p %%X /s /m *.pbk /c "cmd /c if @isdir==FALSE del @file" /d -30

    :exit
    echo The process completed >> %ERRLOG%
    :: Log end time
    :: ------------------ Date and Time Modifier ------------------------
    :: THIS CODE WILL DISPLAY A 2-DIGIT TIMESTAMP
    :: CREATE VARIABLE %TIMESTAMP%
    for /f "tokens=1-8 delims=.:/-, " %%i in ('echo exit^|cmd /q /k"prompt $D $T"') do (
       for /f "tokens=2-4 delims=/-,() skip=1" %%a in ('echo.^|date') do (
    set dow=%%i
    set mm=%%j
    set dd=%%k
    set yy=%%l
    set hh=%%m
    set min=%%n
    set sec=%%o
    set hsec=%%p
    )
    )
    :: ensure that hour is always 2 digits
    if %hh%==0 set hh=00
    if %hh%==1 set hh=01
    if %hh%==2 set hh=02
    if %hh%==3 set hh=03
    if %hh%==4 set hh=04
    if %hh%==5 set hh=05
    if %hh%==6 set hh=06
    if %hh%==7 set hh=07
    if %hh%==8 set hh=08
    if %hh%==9 set hh=09
    :: assign timeStamp:
    :: Add the date and time parameters as necessary - " yy-mm-dd-dow-min-sec-hsec "

    set timeStamp=%mm%%dd%%yy%_%hh%%min%%sec%

    @echo === %dow% %timestamp% : End === >> %ERRLOG%