如何在没有用户输入的情况下运行此脚本

时间:2014-08-20 13:57:04

标签: windows batch-file backup

我正在尝试自动生成此批处理脚本。驱动器号改变了,我无法在窗口中找到一种方法来强制它每次使用相同的一个。我不使用%date%环境变量因为我需要日期格式如下:" YYYY_MM_DD"

我有什么方法可以让这个脚本在没有用户输入的情况下运行?

    @echo off
    set /p "Drive_Letter=Enter Drive letter.>"
    set /p "BKDate=Enter date.>"
    cd\
    %Drive_Letter%:
    cd %Drive_Letter%:\Backup\
    md BK_%BKDate%
    cd\
    Robocopy /E c:\users\%username%\Dropbox\ %Drive_Letter%:\Backup\BK_%BKDate% *
    cd %Drive_Letter%:\Backup
    dir /s %Drive_Letter%:\Backup\BK_%BKDate%% >> LOG_%BKDate%.txt

2 个答案:

答案 0 :(得分:0)

这应该是你想要的。

@echo off
Z:
cd Z:\Backup\
Rem Get Day,Mth & Year from %Date%
set Day=%Date:~0,2%
set Mth=%Date:~3,2%
set Yr=%Date:~6,4%
md BK_%Yr%_%Mth%_%Day%

Robocopy /E %userprofile%\Dropbox\ Z:\Backup\BK_%Day%_%Mth%_%Yr% *

dir /s Z:\Backup\BK_%Yr%_%Mth%_%Day%% >> LOG_%Yr%_%Mth%_%Day%.txt

Link将向您展示如何在每次连接时强制USB使用相同的驱动器号

答案 1 :(得分:0)

这使用:

获取当前日期的子程序中的

robocopy(强制错误以yyyy/mm/dd格式获取错误的时间戳)

mountvol枚举已定义的驱动器

vol来测试广告资源的可访问性

它将搜索"标志"驱动器中的文件夹(备份文件夹)以确定使用哪个

重新获取所有信息后,将使用相应的robocopy命令。

@echo off
    setlocal enableextensions disabledelayedexpansion

    call :getDriveLetter "\Backup\bk*" drive
    if errorlevel 1 (
        echo Drive not found
        goto :eof
    )

    call :getTodayDate   today
    if errorlevel 1 (
        echo Date retrieval error
        goto :eof
    )

    set "BKDate=%today:/=_%"
    set "source=c:\users\%username%\Dropbox"
    set "target=%drive%:\Backup\BK_%BKDate%"

    robocopy "%source%" "%target%" /e
    dir /s "%target%" > "%drive%:\Backup\LOG_%BKDate%.txt"

    endlocal
    exit /b

:getTodayDate returnVar
    set "%~1=" & for /f %%a in (
        'robocopy "|" "." "%~nx0" /njh /r:0 /nocopy /l'
    ) do set "%~1=%%a" & exit /b 0
    exit /b 1

:getDriveLetter folderToTest returnVar
    set "%~2=" & for /f "tokens=1 delims=: " %%a in (
        'mountvol ^| find ":\"' 
    ) do vol %%a: >nul 2>&1 && (if exist "%%a:%~1" set "%~2=%%a" & exit /b 0)
    exit /b 1
相关问题