2016-07-15 3 views
1

バッチは古い形式で、PowerShellと比べて非常に複雑ですが、バッチを使用して多くの古いWindowsマシンと互換性があることを確認する必要があります。ディレクトリ作成日をBATCHの変数に出力する

私は、バックアップドライブ上のストレージ可用性がコンピュータドライブ上のデータを保持するのに十分であることを確認するバックアップスクリプトを作成しています。

スペースが不足している場合は、最初に見つかったバックアップと作成日を見つけておき、次の日付を削除してバックアップが実行可能かどうかを確認する必要があります。

私が抱えている問題は、最も古い日付のディレクトリを見つけて変数に入れてから、次に古いディレクトリを見つけてそれを削除し、再度領域を確認することです。

これは、私は、スクリプトの検証のために持っているものです。

:spaceAvailable 
ECHO Checking disk space availablility... 
::Sets Free variable to available space (approximate) of external drive 
FOR /F "tokens=3" %%a IN ('dir /-c %usb%\ ^| FIND /i "bytes free"') DO SET Free=%%a 
SET Free=%Free:~0,-9% 
IF "%Free%"=="" SET Free=0 
::Sets Used variable to size of Users directory 
FOR /F "tokens=3-4" %%v IN ('dir "C:\Users\" /s ^| FIND /i "file(s)"') DO SET Used=%%v 
SET Used=%Used:~0,-12% 
IF "%Used%"=="" SET Used=0 
::Compares the variables Free and Used 
::If Free is more than Used, the backup will continue 
::if not, system will prompt to free up space 
IF %Free% GTR %Used% (
    ECHO Enough disk space is available 
    goto backUp 
) 
IF %Used% GTR %Free% (
    ::INSERT CODE TO LOCATE SECOND OLDEST DIRECTORY HERE 
    ECHO There is not enough space, press any key to erase %oldDir% 
    PAUSE 
    goto spaceAvailable 
) 

私は、ディレクトリの日付を取得するために多くのことを利用してきた、そしてそれはバッチのために「不可能」と思われます。私は仕事のためにこれを行う必要があり、数日間それに固執しています。 助けてくれてありがとう!

EDIT: スティーブンが答えてくれてありがとう、それはまさに私が探していたものでした。 完成したコードが何であるか不思議だったあなたのために、私はここに投稿します!助けてくれてありがとう。

@ECHO OFF 

::xCopy for data transfer of all user information to an external drive. 
::Created by Benjamin Chaney 
::Version notes- 
::V1.0 - Initial release 
::V1.1 - Replaced XCOPY commands with updated ROBOCOPY commands 
::V1.2 - Created script to verify date and create a directory of todays date 
:: in YYYY_MM_DD format on backup drive. 
:: Also created checks for administrative rights, outlook being opened 
:: and if the backup drive is plugged in. 
::V1.3 - Fixed bugs where it would give an error giving an invalid destination 
::V1.4 - Enhanced the ROBOCOPY command to be more efficient 
::V1.5 - Created an audio queue when backup is finished. 
::V1.6 - Script checks for if there is enough disk space on the drive before the backup 
::V1.7 - Updated to remove second oldest directory in backup drive if no space is available 
ECHO *********************************************************************** 
ECHO *************************HelpDesk Backup V1.7************************** 
ECHO *********************************************************************** 
ECHO **Welcome to the HelpDesk Backup assistant! This will copy all of the** 
ECHO **important information on your computer into your external Backup ** 
ECHO **Drive. Before you begin, there are a few steps to complete:  ** 
ECHO ** 1. Plug in your external backup drive into your computer   ** 
ECHO ** 2. Be sure the name of your Drive is BACKUP.      ** 
ECHO ** -Click on the File Explorer folder (Yellow folder with a blue ** 
ECHO **  holder, usually next to the start icon)      ** 
ECHO ** -A windows will open, left-click on This PC on the left panel ** 
ECHO ** -You should see a number of folders and drives. Locate your ** 
ECHO **  external drive (usually either D: or E:), and right-click on ** 
ECHO **  the drive for more options, then left-click on Rename.  ** 
ECHO ** -Type in BACKUP and press ENTER        ** 
ECHO ** 3. Please close all programs, documents, and web pages before the ** 
ECHO ** process begins.            ** 
ECHO *********************************************************************** 
ECHO *********************************************************************** 
PAUSE 

:adminTEST 

::Verify script has administrative rights. If not, close out of program. 
echo Checking administrative rights... 

net session >nul 2>&1 
    IF %errorLevel% == 0 (
     ECHO Success: Administrative permissions confirmed. 
    goto outlookTEST 
    ) ELSE (
     ECHO Failure: Please run the program in Administrative mode. 
     PAUSE 
     EXIT 
    ) 

ECHO Verifying programs are closed... 

:outlookTEST 

::Verifies Outlook is closed before backup begins. If it is still open, prompts user to close the program, then re-run the program 
    tasklist /fi "imagename eq OUTLOOK.EXE" |find ":" >nul 
    if errorlevel 1 (
     ECHO Your Outlook is still open, please close out of Outlook and try again. 
     PAUSE 
     GOTO outlookTEST 
    ) 
    goto driveLookup 

:driveLookup 

ECHO Finding HDD labeled "BACKUP" 

::Obtain drive letter for BACKUP (the name of the external drive) 
    FOR /f %%D in ('wmic volume get DriveLetter^, Label ^| find "BACKUP"') DO set usb=%%D 
    IF [%usb%]==[] (
     ECHO BACKUP drive is not plugged into the system! Please connect the drive now and try again. 
     PAUSE 
     GOTO driveLookup 
    ) ELSE ( 
     ECHO Found Backup drive! Located at %usb% 
    ) 

:spaceAvailable 

ECHO Checking disk space availablility... 

::Sets Free variable to available space (approximate) of external drive 
FOR /F "tokens=3" %%a IN ('dir /-c %usb%\ ^| FIND /i "bytes free"') DO SET Free=%%a 
SET Free=%Free:~0,-9% 
IF "%Free%"=="" SET Free=0 

::Sets Used variable to size of Users directory 
FOR /F "tokens=3-4" %%v IN ('dir "C:\Users\" /s ^| FIND /i "file(s)"') DO SET Used=%%v 
SET Used=%Used:~0,-12% 
IF "%Used%"=="" SET Used=0 

::Compares the variables Free and Used 
::If Free is more than Used, the backup will continue 
::if not, system will prompt to free up space 
IF %Free% GTR %Used% (
    ECHO Enough disk space is available 
    goto backUp 
) 
IF %Used% GTR %Free% (
    SET /P c=Would you like us to clear some space[Y/N]? 
) 
IF /I "%c%"=="Y" GOTO removeDirectory 
IF /I "%c%"=="N" GOTO clearSpace 
ECHO Didnt work 
PAUSE 
) 

:removeDirectory 

::Sets variables for oldest and second oldest directory, then removes second 
::oldest directory. 
SET "dir1=" 
SET "dir2=" 

FOR /F "delims=" %%a IN ('dir %usb%\Backup\ /tc /ad /od /b') DO (
    IF defined dir2 goto removeNext 
    IF defined dir1 SET "dir2=%%a" & goto removeNext 
    SET "dir1=%%a" 
) 

:removeNext 

::Removes the second oldest directory in the Backup drive 
ECHO Removing directory "%usb%\Backup\%dir2% 
ECHO Please wait while we clear some space... 
RD /S /Q "%usb%\Backup\%dir2% 
    IF errorlevel 0 (
     GOTO clearSpace 
    ) 

GOTO spaceAvailable 

:clearSpace 

ECHO Please manually remove files in your backup drive (%usb%) 
ECHO to clear up at least %Used%GB of space before backing up! 
PAUSE 
EXIT 

:backUp 

ECHO Starting backup process 

::Sets string "today" with todays date in YYYY_MM_DD format 

    SET today=%Date:~10,4%_%Date:~7,2%_%Date:~4,2% 

::Script to copy all contents out of the c:/users into external drive 

    IF EXIST "%usb%\Backup\%today%" (
     GOTO roboCOPY 
    ) ELSE (
     MKDIR %usb%\Backup\%today% 
     ECHO Created backup folder labeled "%today%" 
    ) 

:roboCOPY 

ECHO *************************************************************************** 
ECHO *************************************************************************** 
ECHO **Your computer is now being backed up. Please wait, this may take a  ** 
ECHO **while depending on how much information needs to be backed up.   ** 
ECHO **Once finished, you should hear the notification sound. Please wait. ** 
ECHO *************************************************************************** 
ECHO *************************************************************************** 
ROBOCOPY C:\Users\ %usb%\Backup\%today%\Users\ /MIR /XA:SH /W:0 /R:1 /REG /XJ /LOG:%usb%\Backup\BackupLog.txt 
ECHO 

ECHO **************************************************************************** 
ECHO **************************************************************************** 
ECHO **Your backup is complete! Your backup is located in %usb%\Backup\%today% ** 
ECHO **You can view the log of your backup at %usb%\Backup\BackupLog.txt.   ** 
ECHO **Thank you for using the Help Desk Backup Assistant. Please contact us ** 
ECHO **if you have any questions at ********** or at *******************  ** 
ECHO **************************************************************************** 
ECHO **************************************************************************** 

PAUSE 
EXIT 
+0

は、多分あなたは道にに興味があります[あなたの日付は、ローカライズされた日付形式に依存しません](http://stackoverflow.com/a/18024049/2152082)。 – Stephan

答えて

0

あなたの説明によれば、最も古いものと最も古いものの1つのディレクトリを探します。 dirは、いくつかの有用なオプション(dir /?を参照)があります:あなたは完全なパスが必要な場合は

@echo off 
set "dir1=" 
set "dir2=" 
for /f "delims=" %%a in ('dir /tc /ad /od /b') do (
    if defined dir2 goto :finish 
    if defined dir1 set "dir2=%%a" & goto :finish 
    set "dir1=%%a" 
) 
:finish 
echo oldest: %dir1% 
echo oldest but one: %dir2% 

、使用%%~fa

(注:NTFSのための/tc作品のみ)

+0

それは完璧に機能しました!どうもありがとうございます。 – BChaney

関連する問題