Find a String Varible

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Find a String Varible

#1 Post by booga73 » 26 Jan 2014 21:34

I"m trying to expose a shadow copy after creating one. I'm trying to implement a way to do a snapshot of my drive when files are locked and back up all the files to a specific external drive. I need though to get the snapshot ID correctly recognized to mount the shadow copy.
if I can get some help, please and thank you, Booga73

my script:

Code: Select all

c:\temp1\vshadow64.exe -p c: > %tmp%\vsha.txt 
for /f "skip=52 tokens=5 delims={,} " %%a in (%tmp%\vsha.txt) do @echo %%a >> %tmp%\vsha1.txt
set /p txt=< %tmp%\vsha1.txt
vshadow -el={%txt%},B:


when I step through and do the commands manually at the command prompt, I am able to:
1. Create the Shadow copy.
2. map the shadow copy to a specific drive letter which is B drive
3. access the shadow copy like a regular drive


I think when this line executes, vshadow -el={%txt%},B:, it's not recognizing the snapshot ID correctly.

this is the output of my script:

VSHADOW.EXE 3.0 - Volume Shadow Copy sample client.
Copyright (C) 2005 Microsoft Corporation. All rights reserved.


(Option: Expose a shadow copy)
ERROR: the -el arguments must contain a GUID and a local path separated by a comma.


versus what should happen:

Code: Select all

C:\>vshadow64.exe -el={cc0e56c8-cbf9-436e-bbdf-ae269f7fc185},B:

VSHADOW.EXE 3.0 - Volume Shadow Copy sample client.
Copyright (C) 2005 Microsoft Corporation. All rights reserved.


(Option: Expose a shadow copy)
- Setting the VSS context to: 0xffffffff
- Exposing shadow copy {cc0e56c8-cbf9-436e-bbdf-ae269f7fc185} under the path 'B:
'
- Checking if 'B:' is a valid drive letter ...
- Shadow copy exposed as 'B:\'






the vshadow tools which I use is found here: http://ithelp.cveg.uark.edu/backup/backup.zip
website where I got my tools from, which I hadn't a problem either.
also, I am using Win7 x64 bit


this is the output of vsha.txt

    VSHADOW.EXE 3.0 - Volume Shadow Copy sample client.
    Copyright (C) 2005 Microsoft Corporation. All rights reserved.


    (Option: Persistent shadow copy)
    (Option: Create shadow copy set)
    - Setting the VSS context to: 0x00000009
    (Gathering writer metadata...)
    (Waiting for the asynchronous operation to finish...)
    Initialize writer metadata ...
    Discover directly excluded components ...
    - Excluding writer 'Shadow Copy Optimization Writer' since it has no selected components for restore.
    - Excluding writer 'MSSearch Service Writer' since it has no selected components for restore.
    Discover components that reside outside the shadow set ...
    - Component '\BCD\BCD' from writer 'ASR Writer' is excluded from backup (it requires in the shadow set)
    Discover all excluded components ...
    Discover excluded writers ...
    - The writer 'ASR Writer' is now entirely excluded from the backup:
    (the top-level non-selectable component '\BCD\BCD' is an excluded component)
    Discover explicitly included components ...
    Verifying explicitly specified writers/components ...
    Select explicitly included components ...
    * Writer 'Task Scheduler Writer':
    - Add component \TasksStore
    * Writer 'VSS Metadata Store Writer':
    - Add component \WriterMetadataStore
    * Writer 'Performance Counters Writer':
    - Add component \PerformanceCounters
    * Writer 'System Writer':
    - Add component \System Files
    * Writer 'Registry Writer':
    - Add component \Registry
    * Writer 'COM+ REGDB Writer':
    - Add component \COM+ REGDB
    * Writer 'WMI Writer':
    - Add component \WMI
    Creating shadow set {7e0ef1bf-aecb-47dc-891a-fa0c58c000a9} ...
    - Adding volume \\?\Volume{a67e33e9-40f6-11e3-9e8f-806e6f6e6963}\ [C:\] to the shadow set...
    Preparing for backup ...
    (Waiting for the asynchronous operation to finish...)
    (Waiting for the asynchronous operation to finish...)
    Creating the shadow (DoSnapshotSet) ...
    (Waiting for the asynchronous operation to finish...)
    (Waiting for the asynchronous operation to finish...)
    Shadow copy set succesfully created.

    List of created shadow copies:


    Querying all shadow copies with the SnapshotSetID {7e0ef1bf-aecb-47dc-891a-fa0c58c000a9} ...

    * SNAPSHOT ID = {cc0e56c8-cbf9-436e-bbdf-ae269f7fc185} ...
    - Shadow copy Set: {7e0ef1bf-aecb-47dc-891a-fa0c58c000a9}
    - Original count of shadow copies = 1
    - Original Volume name: \\?\Volume{a67e33e9-40f6-11e3-9e8f-806e6f6e6963}\ [C:\]
    - Creation Time: 1/26/2014 9:05:56 PM
    - Shadow copy device name: \\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy61
    - Originating machine: NeuberTek.irt123.net
    - Service machine: NeuberTek.irt123.net
    - Not Exposed
    - Provider id: {b5946137-7b9f-4925-af80-51abd60b20d5}
    - Attributes: No_Auto_Release Persistent Differential

    - Mark all writers as succesfully backed up...
    Completing the backup (BackupComplete) ...
    (Waiting for the asynchronous operation to finish...)
    (Waiting for the asynchronous operation to finish...)

    Snapshot creation done.


booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Re: Find a String Varible

#2 Post by booga73 » 26 Jan 2014 21:49

also . .. .

wow, interesting, I wrote the script this way:

for /f "skip=52 tokens=5 delims={,} " %a in ('vshadow -p c:') do vshadow -el={%a},B:

and I was then successfully able to mount my vshadow to B drive!!!!

. . . and the world continues to rotate. . . . :shock:

but what followed in the script after B was mounted was the following:

is there a way to simply ignore that remaining rambling text?


Code: Select all


C:\>vshadow -el={2f539870-0da4-4bb9-ae79-014a4a6554be},B:

VSHADOW.EXE 3.0 - Volume Shadow Copy sample client.
Copyright (C) 2005 Microsoft Corporation. All rights reserved.


(Option: Expose a shadow copy)
- Setting the VSS context to: 0xffffffff
- Exposing shadow copy {2f539870-0da4-4bb9-ae79-014a4a6554be} under the path 'B:
'
- Checking if 'B:' is a valid drive letter ...
- Shadow copy exposed as 'B:\'

C:\>vshadow -el={37f9b98f-bc5c-4aab-b4ef-42c45267bb09},B:

VSHADOW.EXE 3.0 - Volume Shadow Copy sample client.
Copyright (C) 2005 Microsoft Corporation. All rights reserved.


(Option: Expose a shadow copy)
- Setting the VSS context to: 0xffffffff
- Exposing shadow copy {37f9b98f-bc5c-4aab-b4ef-42c45267bb09} under the path 'B:
'

ERROR: there is no snapshot with the given ID

C:\>vshadow -el={shadow},B:

VSHADOW.EXE 3.0 - Volume Shadow Copy sample client.
Copyright (C) 2005 Microsoft Corporation. All rights reserved.


(Option: Expose a shadow copy)
ERROR: The string '{shadow}' is not formatted as a GUID!

C:\>vshadow -el={\\?\Volume},B:

VSHADOW.EXE 3.0 - Volume Shadow Copy sample client.
Copyright (C) 2005 Microsoft Corporation. All rights reserved.


(Option: Expose a shadow copy)
ERROR: The string '{\\?\Volume}' is not formatted as a GUID!

C:\>vshadow -el={9:47:29},B:

VSHADOW.EXE 3.0 - Volume Shadow Copy sample client.
Copyright (C) 2005 Microsoft Corporation. All rights reserved.


(Option: Expose a shadow copy)
ERROR: The string '{9:47:29}' is not formatted as a GUID!

C:\>vshadow -el={name:},B:

VSHADOW.EXE 3.0 - Volume Shadow Copy sample client.
Copyright (C) 2005 Microsoft Corporation. All rights reserved.


(Option: Expose a shadow copy)
ERROR: The string '{name:}' is not formatted as a GUID!

C:\>vshadow -el={Differential},B:

VSHADOW.EXE 3.0 - Volume Shadow Copy sample client.
Copyright (C) 2005 Microsoft Corporation. All rights reserved.


(Option: Expose a shadow copy)
ERROR: The string '{Differential}' is not formatted as a GUID!

C:\>vshadow -el={as},B:

VSHADOW.EXE 3.0 - Volume Shadow Copy sample client.
Copyright (C) 2005 Microsoft Corporation. All rights reserved.


(Option: Expose a shadow copy)
ERROR: The string '{as}' is not formatted as a GUID!

C:\>vshadow -el={...},B:

VSHADOW.EXE 3.0 - Volume Shadow Copy sample client.
Copyright (C) 2005 Microsoft Corporation. All rights reserved.


(Option: Expose a shadow copy)
ERROR: The string '{...}' is not formatted as a GUID!

C:\>vshadow -el={operation},B:

VSHADOW.EXE 3.0 - Volume Shadow Copy sample client.
Copyright (C) 2005 Microsoft Corporation. All rights reserved.


(Option: Expose a shadow copy)
ERROR: The string '{operation}' is not formatted as a GUID!

C:\>vshadow -el={operation},B:

VSHADOW.EXE 3.0 - Volume Shadow Copy sample client.
Copyright (C) 2005 Microsoft Corporation. All rights reserved.


(Option: Expose a shadow copy)
ERROR: The string '{operation}' is not formatted as a GUID!

C:\>


penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Find a String Varible

#3 Post by penpen » 27 Jan 2014 04:50

If you wanted to know where the issue in your first code was, although you have fixed it yourself :D ,
then it seems that you have used "c:\temp1\vshadow64.exe" instead of "vshadow", as replacing seems to have fixed your problem:
booga73 wrote:

Code: Select all

c:\temp1\vshadow64.exe -p c: > %tmp%\vsha.txt 
for /f "skip=52 tokens=5 delims={,} " %%a in (%tmp%\vsha.txt) do @echo %%a >> %tmp%\vsha1.txt
set /p txt=< %tmp%\vsha1.txt
vshadow -el={%txt%},B:
The one-liner equivalent:

Code: Select all

for /f "skip=52 tokens=5 delims={,} " %%a in ('c:\temp1\vshadow64.exe -p c:') do vshadow -el={%%a},B:
Compared to:
booga73 wrote:

Code: Select all

for /f "skip=52 tokens=5 delims={,} " %a in ('vshadow -p c:') do vshadow -el={%a},B:


So the following is left only, if i haven't missed something:
booga73 wrote:is there a way to simply ignore that remaining rambling text?
I'm not sure how you mean that... .
It may be interpreted as: You don't want to display the output.
Then this should help you:

Code: Select all

for /f "skip=52 tokens=5 delims={,} " %%a in ('vshadow -p c:') do (vshadow -el={%%a},B: >nul 2>nul)
If you wanted to see the errors, the following may help:
- if "vshadow" uses the errorstream: just remove the '2>nul' in the above script, else
- if "vshadow" uses standardstream for errors and always displays an 'ERROR' in front , just add an ' | findstr "ERROR" '

Or do you want not to execute:
booga73 wrote:

Code: Select all

C:\vshadow -el={37f9b98f-bc5c-4aab-b4ef-42c45267bb09},B:
C:\vshadow -el={shadow},B:
C:\vshadow -el={\\?\Volume},B:
C:\vshadow -el={9:47:29},B:
...

In that case you may do it in this way:

Code: Select all

for /f "skip=52 tokens=5 delims={,} " %%a in ('vshadow -p c:') do (
   vshadow -el={%%a},B: >nul 2>nul
   goto :eof
)
This only works from a batch file, as goto has no effect on the command prompt.

penpen

booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Re: Find a String Varible

#4 Post by booga73 » 27 Jan 2014 09:47

Hi PenPen,

I guess I am only being picky, but I went with a solution to clear out that rambling though.


the command which I used still is 1 line of code . . using the cls to clear the screen, there's still a moment the rambling is shown but is cleared.

Code: Select all

for /f "skip=52 tokens=5 delims={,} " %%a in ('vshadow -p c:') do vshadow -el={%%a},B: & set ID=%%a & cls


I didn't get, >nul 2>nul nore 2>&1 > Nul to work for me, but I then used clear screen command, cls.

Also, I renamed vshadow64.exe to vshadow.exe and placed it into my windows directory, so now my script isn't so locked into a folder to operate too.


When I manually completed a VSS (volume shadow Snapshot) of my C: drive with vshadow -p c: my 52rd line of code in the output is the ID of my VSS snap shot.

52nd line of output:

Code: Select all

* SNAPSHOT ID = {765425a7-326b-4599-aebd-fe8bee3017ec} ...


and. . . 53rd through 63rd actually causes that rambling, shows this trying to mount but errors with the rambling:

Code: Select all

   - Shadow copy Set: {38a65831-1ac9-430e-a68e-16b81f0258db}
   - Original count of shadow copies = 1
   - Original Volume name: \\?\Volume{fd323724-3271-11e2-8047-806e6f6e6963}\ [C:\]
   - Creation Time: 1/27/2014 9:36:20 AM
   - Shadow copy device name: \\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy20
   - Originating machine: %computername%
   - Service machine: %computername%
   - Not Exposed
   - Provider id: {b5946137-7b9f-4925-af80-51abd60b20d5}
   - Attributes:  No_Auto_Release Persistent Differential


I am able to capture the ID that I need to expose the VSS:

Code: Select all

"skip=52 tokens=5 delims={,} "


I am able to capture this:

Code: Select all

  765425a7-326b-4599-aebd-fe8bee3017ec  


awesome, thank you for your script illustrations too, thank you Dos Tips!

best regards, Booga73

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Find a String Varible

#5 Post by penpen » 27 Jan 2014 13:04

booga73 wrote:

Code: Select all

for /f "skip=52 tokens=5 delims={,} " %%a in ('vshadow -p c:') do vshadow -el={%%a},B: & set ID=%%a & cls


When I manually completed a VSS (volume shadow Snapshot) of my C: drive with vshadow -p c: my 52rd line of code in the output is the ID of my VSS snap shot.

52nd line of output:

Code: Select all

* SNAPSHOT ID = {765425a7-326b-4599-aebd-fe8bee3017ec} ...

It should be the 53rd line as "for /F" skips the first 52 lines;
or my (to german translated) for loop help is buggy (wouldn't be the first time).
In addition the environment variable "ID" is corrupted, as the for loop overwrites it in each iteration.

booga73 wrote:the command which I used still is 1 line of code . . using the cls to clear the screen, there's still a moment the rambling is shown but is cleared.
(...)

and. . . 53rd through 63rd actually causes that rambling, shows this trying to mount but errors with the rambling:

Code: Select all

   - Shadow copy Set: {38a65831-1ac9-430e-a68e-16b81f0258db}
...

So you only want the for loop not to perform "vshadow -el={%%a},B: & set ID=%%a" with lines 53+.
Note: In my last post i wanted to make the (batch) code more readable; it is no problem to use only one-liner.

As you use a shell script, then you may use "findstr" to filter the output:

Code: Select all

@for /f "tokens=5 delims={,} " %a in ('type vsh.txt  ^| findstr /n "^" ^| findstr "^52:"') do @(vshadow -el={%a},B: & set ID=%a)
(Mabe change 52 to 53.)

penpen

booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Re: Find a String Varible

#6 Post by booga73 » 27 Jan 2014 20:50

Hi PenPen,

I tried running the code script you provided, but received an error:


Code: Select all

@for /f "tokens=5 delims={,} " %a in ('type vsh.txt  ^| findstr /n "^" ^| findstr "^52:"') do @(vshadow -el={%a},B: & set ID=%a)


The system cannot find the file specified.


I'm lost at this point :(

But yes, basically, I simply just want the 52nd line,

Code: Select all

* SNAPSHOT ID = {765425a7-326b-4599-aebd-fe8bee3017ec} ...
to extract the value,
765425a7-326b-4599-aebd-fe8bee3017ec


v/r Booga73

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Find a String Varible

#7 Post by Squashman » 27 Jan 2014 21:08

Does the vsh.txt file exist?

booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Re: Find a String Varible

#8 Post by booga73 » 27 Jan 2014 21:29

I did a complete search and I don't see a vsh.txt file on my computer.

sorry, drawing a blank, but in the line of code, I don't see where the vsh.txt would be created.

Code: Select all

@for /f "skip=52 tokens=5 delims={,} " %a in ('type vsh.txt  ^| findstr /n "^" ^| findstr ":"') do @(vshadow -el={%a},B: & set ID=%a)

running Win7 x64

v/r Booga

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Find a String Varible

#9 Post by penpen » 28 Jan 2014 02:28

Sorry, i've forgotton to replace the "type vsh.txt" with "'vshadow -p c:":
As i haven't installed vshadow, i have used the sample output you gave in your first post as the content of vsh.txt.
So the shell script should be:

Code: Select all

@for /f "tokens=5 delims={,} " %a in ('vshadow -p c: ^| findstr /n "^" ^| findstr "^52:"') do @(vshadow -el={%a},B: & set ID=%a)

penpen

booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Re: Find a String Varible

#10 Post by booga73 » 28 Jan 2014 10:36

Amazing. That is the word.

To clarify which line that I need picked up for capturing and mounting a VSS snap shot, it's line #54. I imported the sha.txt file into word and completed a line number, the snapshot ID found was #54.

Line # 54 --->
* SNAPSHOT ID = {a638a1df-d287-4ac0-9d3a-875bf1a0b8ea} ...


The line of code expanded upon PenPen did literally clear out the rambling text to capture & mount the snap shot.

Initially when I looked hard and penned ( no pun intended :o ) the initial code to capture the snap shot, I had no idea why I had the rambling. . . . BUT to move right along in capturing the SnapShot ID#, the code had to be a single instance to effectively hold the snap shot in place and mount a drive letter so that files that are locked can be backed up using this line.

initial code --->

Code: Select all

for /f "skip=52 tokens=5 delims={,} " %%a in ('vshadow -p c:') do vshadow -el={%%a},B: & set ID=%%a & cls


the rambling:

    - Shadow copy Set: {38a65831-1ac9-430e-a68e-16b81f0258db}
    - Original count of shadow copies = 1
    - Original Volume name: \\?\Volume{fd323724-3271-11e2-8047-806e6f6e6963}\ [C:\]
    - Creation Time: 1/27/2014 9:36:20 AM
    - Shadow copy device name: \\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy20
    - Originating machine: %computername%
    - Service machine: %computername%
    - Not Exposed
    - Provider id: {b5946137-7b9f-4925-af80-51abd60b20d5}
    - Attributes: No_Auto_Release Persistent Differential


I'm not sure why that rambing kept trying to be picked up by my initial code snippet. The For loop just kept reading but captured the one line that I needed. But figuring that the initial code did "grab" the snapshot ID was all that matters; but PenPen did go further with the code, expanded to clear out the rambling.

During my research to find a good method to do a VSS SnapShot, expose the SnapShot ID and mount it was my goal; that's where I found vshadow from Microsoft Windows Software Development Kit; found the needed files there.

anyway! that's good PenPen got it, thank you !

10 pints awarded --->

Code: Select all

@for /f "tokens=5 delims={,} " %%a in ('vshadow -p c: ^| findstr /n "^" ^| findstr "^54:"') do @(vshadow -el={%%a},B: & set ID=%%a)


v/r Booga73

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Find a String Varible

#11 Post by Squashman » 28 Jan 2014 18:04

Penpen, I just assumed he was still running the command to create the vsh.txt file and using your code to parse it. I kind of thought that was obvious.

Booga, not sure why you would search for the file on your entire drive when your original code was doing it that way and the file would have to exist within the working directory for the script to use it. The script is not going to search your entire drive looking for it too use it.

booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Re: Find a String Varible

#12 Post by booga73 » 29 Jan 2014 08:46

absolutely right on that Squashman; when I was initially tooling around with a code snippet to capture an shadow copy and mount it; my original snippet was based on looking for a txt file to grab the nth line from it while residing in a tmp location; but it dawned on me how to stream line the snippet of code and PenPen ultimately put the precision touch to it. That's why this forum is awesome.

have a Great week, v/r Booga

Post Reply