Ad-Man-Gamer
Ad-Man-Gamer

Reputation: 23

Can't get a variable to set using for /f in CMD

This script is baffling me. I'm trying to write a script that takes the most recent shadow copy dir and put it in a var. This is what I have so far.

@ECHO off
set ShadowDir=
vssadmin list shadows|for /f "tokens=4 delims=: " %%A IN ('FINDSTR "GLOBALROOT"') do set ShadowDir=%%A
ECHO The dir is %ShadowDir%
pause

And this is the output.

C:\WINDOWS\system32>set ShadowDir=\\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy14

C:\WINDOWS\system32>set ShadowDir=\\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy15

C:\WINDOWS\system32>set ShadowDir=\\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy16

C:\WINDOWS\system32>set ShadowDir=\\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy17

C:\WINDOWS\system32>set ShadowDir=\\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy18
The dir is
Press any key to continue . . .

As you can see, it is parsing correctly and giving me the desired output. But the ShadowDir var remains empty even with the set command being issued.

Any idea what's going on?

Upvotes: 1

Views: 175

Answers (1)

Magoo
Magoo

Reputation: 80023

I'd use

for /f "tokens=4 delims=: " %%A IN ('vssadmin list shadows^|FINDSTR "GLOBALROOT"') do set ShadowDir=%%A

passing vssadmin output to findstr.

Upvotes: 3

Related Questions