批处理条件,以嵌套为循环



我正在尝试编写一个解析文本文件以通过标签识别可移动驱动器的脚本。目标是基于发现的驱动器创建复制脚本。

我能够解析文件,但是如果条件是正确的,我似乎无法获得第二对。

i可能会误解如何在for。

的每个循环中评估变量

想法。

编辑:在原始请求中扩展(我问过的时间很晚),扩展的目标是识别一个或多个目标驱动器,以从SD卡复制数据。由于驱动器的字母分配因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 

感谢所有回应的人。如果@mofi提供了一个答案,我会标记为答案是答案。最后,删除了四个报价标记,而添加彼此给我带来了我想要的答案。

下面的脚本给了我我所追求的。

@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 ))
)

如果您只想将一些变量设置为分配给带有字符串 DRIVE的可移动驱动器的驱动器,则以下可能会为您提供不需要高程的替代方案。<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<</p>

@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

最新更新