2015-08-13 52 views
0

我想爲我的辦公室製作自動備份腳本。我已經做了一些研究並最終得到了一些燈泡,但問題是我的代碼只能在Windows 7中運行,並且在Windows XP中無法運行。那麼我如何解決這個問題?
這裏是我的代碼
批處理文件腳本在xp中不起作用

echo off 
setlocal enabledelayedexpansion 
:FIND.BACKUP.DRIVE.LETTER 
set start.dir=%systemdrive% 
J: 
%cls% 
if not "%cd:~0,2%"=="%start.dir%" goto next 
I: 
%cls% 
if not "%cd:~0,2%"=="%start.dir%" goto next 
H: 
%cls% 
if not "%cd:~0,2%"=="%start.dir%" goto next 
G: 
%cls% 
if not "%cd:~0,2%"=="%start.dir%" goto next 


if "%cd:~0,2%"=="%start.dir%" (
echo WAITING FOR FLASH MEDIUM TO BE CONNECTED . . . 
if exist "%windir%\system32\timeout.exe" timeout /t 1 /nobreak >nul 
if not exist "%windir%\system32\timeout.exe" pause 
goto FIND.FLASH.DRIVE.LETTER 
) 

:next 
set start.hour=%time:~0,2% 
set start.min=%time:~3,2% 

REM FLASH.DRIVE IS THE FLASH DRIVE. 
set flash.drive=%cd:~0,2% 

set date="%date:~7,2%-%date:~4,2%-%date:~10,4%" 
mkdir "%userprofile%\desktop\a\%date%" 

xcopy "%flash.drive%\*.*" /s /c /d /e /h /i /r /y %userprofile%\desktop\a\%date% 

if not exist %flash.drive% set /a total.time=((((%time:~0,2%-%start.hour%)... && echo Flash Drive has been in for !total.time! minutes. && Pause>nul 
@pause 

感謝

+0

1.「不會工作」的短語沒有診斷價值:請編輯您的問題並指定出現了什麼問題; 2.使用'echo ON'來查看究竟發生了什麼; 3.對[_volatile_環境變量](http://ss64.com/nt/syntax-variables.html)不要使用'set date = ...'等:使用其他名稱,例如'set _date = ...' – JosefZ

+1

在批處理文件的頂部有一個標籤'FIND.BACKUP.DRIVE.LETTER',但是做一個__goto__到'FIND.FLASH.DRIVE.LETTER',即'BACKUP'與'FLASH '。此外,我建議避免在標籤和環境變量中使用駝峯,並使用CamelCase符號,即「FindFlashDriveLetter」和「StartDir」。還有在批處理代碼中沒有定義的環境變量'cls'的引用。不要使用'cls'作爲變量名稱以避免與命令__cls__混淆(清除屏幕)。替換'timeout'正在使用__ping__,請參閱[如何在批處理腳本中等待](http://stackoverflow.com/questions/735285/) – Mofi

回答

1

這應該是可靠的,做同樣的工作。

您確定您需要xcopy中的/d開關嗎?

@echo off 
:loop 
echo waiting... Please insert the flash drive... 
ping -n 3 "" >nul 
set "drv=" 
for %%a in (g h i j) do if exist "%%a:\" set "drv=%%a:" 
if not defined drv goto :loop 

REM drv IS THE FLASH DRIVE. 

set start.hour=%time:~0,2% 
set start.min=%time:~3,2% 
set "d8=%date:~7,2%-%date:~4,2%-%date:~10,4%" 

xcopy "%drv%\*.*" /s /c /d /e /h /r /y "%userprofile%\desktop\a\%d8%\" 

set end.hour=%time:~0,2% 
set end.min=%time:~3,2% 
pause 

我相信你對總時間的計算是打破的,而且是你可以處理的東西。

+0

謝謝你,它的工作原理就像我想要的 – uniks

相關問題