2017-02-19 234 views
0

我正試圖編寫一個腳本來解析一個文本文件,以標識可移動驅動器。目標是根據找到的驅動器創建一個副本腳本。批量條件FOR循環嵌套IF

我能夠解析文件,但我似乎無法得到第二對IF條件爲真。

我可能會誤解在FOR的每個循環中如何評估變量。

想法感激。

編輯:爲了清晰起見擴展原始請求(我問的時候已經很晚了)擴展目標是確定一個或多個目標驅動器從SD卡複製數據。由於驅動器字母分配從PC改變PC和依賴於它在第一插入我的想法是使用標籤作爲有些身份價值,同時

XCOPY SourceDrive\*.jpg DestinationDrive1\<GetDate>\ 
XCOPY SourceDrive\*.jpg DestinationDrive2\<GetDate>\ 

結束的行創建一個簡單的複製串編輯

@echo off 
cls 
title "Detecting device drive letters..." 
setlocal enabledelayedexpansion 
set DestinationDrive1Found= 
set DestinationDrive2Found= 
set SourceDriveFound= 
set SourceDrive= 
set DestinationDrive1= 
set DestinationDrive2= 

echo Detecting device drive letters... 

echo list volume > %systemdrive%\ListDrives.tmp 
diskpart /s %systemdrive%\ListDrives.tmp > %systemdrive%\CurrentDrives.tmp 

echo Checking drive IDs 
FOR /F "skip=7 tokens=2-4" %%a IN (%systemdrive%\CurrentDrives.tmp) DO ( 
set "matchvar=%%c" 
set "comparevar=!matchvar:DRIVE=!" 
IF /I NOT "!comparevar!" == "%%c" IF "!DestinationDrive1Found!" NEQ 1 (
echo Drive %%b was found to be %%c and will be DESTINATION1 
set DestinationDrive1Found=1 
set DestinationDrive1=%%b) 
IF /I NOT "!comparevar!" == "%%c" IF "!DestinationDrive1Found!" EQU 1 (echo Drive %%b was found to be %%c and will be DESTINATION2 
set DestinationDrive2Found=1 
set DestinationDrive2=%%b) 
)  

文件我解析

Microsoft DiskPart version 10.0.10586 

Copyright (C) 1999-2013 Microsoft Corporation. 
On computer: NEWGLOOMWIN10 

    Volume ### Ltr Label  Fs  Type  Size  Status  Info 
    ---------- --- ----------- ----- ---------- ------- --------- -------- 
    Volume 0  D      DVD-ROM   0 B No Media   
    Volume 1  E Macintosh H HFS Partition 231 GB Healthy    
    Volume 2  C BOOTCAMP  NTFS Partition 721 GB Healthy System 
    Volume 3  F TEAMXCAMRAX FAT Removable 1937 MB Healthy    
    Volume 4  G TEAMXDRIVEX NTFS Partition 465 GB Healthy    
    Volume 5  H TEAMYDRIVEY NTFS Partition 931 GB Healthy 
+4

命令__IF__將比較運算符與字符串右對齊的字符串進行比較__with包含雙引號__。因此,條件「IF」!DestinationDrive1Found!「 NEQ 1'始終爲真,因爲'NEQ'左邊的字符串總是帶雙引號,'NEQ'右邊的字符串永遠不帶雙引號。所以比較的字符串總是不相等的。解決方法是在'NEQ'和'EQU'的右側使用''1''兩次。 – Mofi

+1

有什麼機會可以告訴我們你想從輸入文件中產生什麼信息?你提到「可移動驅動器」,但不要說你期望得到什麼信息。 – Magoo

+0

感謝您對Mofi和@Magoo的投入。我編輯了原件以擴展請求。但是,當我寫這個編輯時,我可能已經回答了我自己的問題。我相信我可以按照現在的代碼來製作自己的代碼。我會告訴你。 – Andye

回答

0

感謝所有回答。如果@Mofi提供了答案,我會將該答覆標記爲答案。最後,刪除了四個引號,並添加了一個ELSE,讓我得到了我正在尋找的答案。

下面的腳本給了我完全一樣的東西。

@echo off 
cls 
title "Detecting device drive letters..." 
setlocal enabledelayedexpansion 
set DestinationDrive1Found= 
set DestinationDrive2Found= 
set SourceDriveFound= 
set SourceDrive= 
set DestinationDrive1= 
set DestinationDrive2= 

echo Detecting device drive letters... 

echo list volume > %systemdrive%\ListDrives.tmp 
diskpart /s %systemdrive%\ListDrives.tmp > %systemdrive%\CurrentDrives.tmp 


echo Checking drive IDs 
FOR /F "skip=7 tokens=2-4" %%a IN (%systemdrive%\CurrentDrives.tmp) DO ( 
set "matchvar=%%c" 
set "comparevar=!matchvar:CAMRA=!" 
IF /I NOT "!comparevar!" == "%%c" (@echo Drive %%b was found to be %%c and will be SOURCE 
set SourceDriveFound=1 
set SourceDrive=%%b) 
) 

echo Checking drive IDs 
FOR /F "skip=7 tokens=2-4" %%a IN (%systemdrive%\CurrentDrives.tmp) DO ( 
set "matchvar=%%c" 
set "comparevar=!matchvar:DRIVE=!" 
IF /I NOT "!comparevar!" == "%%c" IF !DestinationDrive1Found! NEQ 1 (
echo Drive %%b was found to be %%c and will be DESTINATION1 
set DestinationDrive1Found=1 
set DestinationDrive1=%%b 
) ELSE ( 
IF /I NOT "!comparevar!" == "%%c" IF !DestinationDrive1Found! EQU 1 (echo Drive %%b was found to be %%c and will be DESTINATION2 
set DestinationDrive2Found=1 
set DestinationDrive2=%%b)) 
) 
0

如果你只是想將一些變量分配給驅動器任何可拆卸驅動器的字符串DRIVE在它的'標籤'中,那麼以下內容可能會爲您提供一個不需要提升的替代方案。

@Echo Off 
For /F "UseBackQ Skip=1 Delims=" %%A In (`WMIC LogicalDisk Where^ 
"DriveType='2' And VolumeName Like '%%DRIVE%%'" Get Name` 
) Do For /F %%B In ("%%A") Do Set/A "i+=1"&Call Set "_drv%%i%%=%%B" 
Set _drv 
Timeout -1 

[編輯] - 一個例子正確規定固定的驅動

@Echo Off 
For /F "UseBackQ Skip=1 Delims=" %%A In (`WMIC LogicalDisk Where^ 
"DriveType='3' And VolumeName Like '%%DRIVE%%'" Get Name` 
) Do For /F %%B In ("%%A") Do Set/A "i+=1"&Call Set "_drv%%i%%=%%B" 
Set _drv 
Timeout -1 

一個例子而未規定驅動,的類型(基於評論推薦)

@Echo Off 
For /F "UseBackQ Skip=1 Delims=" %%A In (
    `WMIC LogicalDisk Where "VolumeName Like '%%DRIVE%%'" Get Name` 
) Do For /F %%B In ("%%A") Do Set/A "i+=1"&Call Set "_drv%%i%%=%%B" 
Set _drv 
Timeout -1 
+0

感謝回覆@Compo。然而,我得到了「環境變量_drv未定義」 – Andye

+0

這意味着你還沒有一個帶有包含字符串DRIVE的標籤的可移動驅動器。這很有意義,因爲您的磁盤部件輸出表明這些驅動器是465 GB和931 GB,而不是像F:所示的驅動器那樣可移動。如果您正在測試的驅動器是固定驅動器,我希望將'DriveType ='2''更改爲'DriveType ='3''並重試。 – Compo

+0

其實@Compo它們是可拆卸的,也就是說,它們是USB 3的外部驅動器,只是非常大。事實上,實際的目標是4TB USB 3.我不確定這是否有所作爲,但我會嘗試使用您所建議的修改代碼。 – Andye