Reputation: 61
I'm missing something here but I just can't see it. I'm sure it will have something to way the variable are being called but I'm not getting it.
My goal is to build a universal script to copy files from an SD card to a pair removable drives. Key is making sure the backup is consistent regardless of drives connected and drive letters.
I cobbled together some scripts that were kind of doing what I wanted but I'm missing a detail.
Below the code does a dump of data using DiskPart to create a file of attached drives. I then walk through the file and use the Drive Label as the target. Depending on the drive label, it's marked as the source or the target.
The piece below works up to the point of the IF where the string comparison (looking for a drive with partial label "CAMX")
I'm using the DOS "string replace, and if different that's the one you want" approach hence the "NEQ" operator.
Currently the NEQ is failing in that it sees all the results and NOT EQUAL. If you change the operator to EQU there are no returns.
The two "echoes" for the strings being compared are debug. If you look at these values they are what I expect them to be and the !matchvar:CAMX=! is working.
Here's the code
@echo off
cls
title "Detecting device drive letters..."
setlocal enabledelayedexpansion
echo Detecting device drive letters...
:: Create a script file to be used by diskpart and then dump all volumes to a temp file
echo list volume > %systemdrive%\ListDrives.tmp
diskpart /s %systemdrive%\ListDrives.tmp > %systemdrive%\CurrentDrives.tmp
:: Parse the output from 'Diskpart> list volume' for available volumes
echo Checking drive IDs
FOR /F "tokens=2-4" %%a IN (%systemdrive%\CurrentDrives.tmp) DO ( set matchvar="%%c" & set comparevar=!matchvar:CAMX=! & IF /I !comparevar! NEQ !matchvar! @echo Drive %%b was found to be %%c and will SOURCE...%%a & @echo !matchvar! & @echo !comparevar!)
Here's the Diskpart file I'm getting
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 TEAMXCAMX FAT Removable 1937 MB Healthy
Volume 4 G TEAM1DRIVE3 NTFS Partition 3725 GB Healthy
Thoughts and advice appreciated.
EDIT: There is something fundamentally wrong. If I alter the code using the correct syntax for string comparison suggested by @abelenky and put in a matching string using and equals operator it still returns all. The "IF" isn't being ignored but the variables value appear to not be evaluated.
IF /I "TEAMXCAMX" == !matchvar!
Upvotes: 0
Views: 286
Reputation: 14320
Not a big fan of the run on commands. It is kind of like a run on sentence in the English Language.
Edited your code a bit to use my best practices. Just used a text file of your input.
@echo off
cls
title "Detecting device drive letters..."
setlocal enabledelayedexpansion
echo Detecting device drive letters...
:: Create a script file to be used by diskpart and then dump all volumes to a temp file
REM echo list volume > %systemdrive%\ListDrives.tmp
REM diskpart /s %systemdrive%\ListDrives.tmp > %systemdrive%\CurrentDrives.tmp
REM Parse the output from 'Diskpart> list volume' for available volumes
echo Checking drive IDs
FOR /F "skip=7 tokens=2-4" %%a IN (CurrentDrives.txt) DO (
set "matchvar=%%c"
set "comparevar=!matchvar:CAMX=!"
IF /I NOT "!comparevar!"=="%%c" echo Drive %%b was found to be %%c and will SOURCE...%%a
echo %%c !comparevar!
)
pause
Output
Detecting device drive letters...
Checking drive IDs
DVD-ROM DVD-ROM
Macintosh Macintosh
BOOTCAMP BOOTCAMP
Drive F was found to be TEAMXCAMX and will SOURCE...3
TEAMXCAMX TEAMX
TEAM1DRIVE3 TEAM1DRIVE3
Press any key to continue . . .
Upvotes: 1