Note: There is no need to use batch per say, but I am just familiar with batch, Powershell would be better I imagine, so if there are easier solutions for this problem in powershell, please shout!
I have the arduous task of testing our DR backups for all our clients, that is, mounting ShadowProtect Snapshots latest incremental, writing and reading a file, them unmounting the image. The actual ShadowProtect part of batch is fairly simple but I would like to design a batch that can automate this.
Essentially my question is:
How in a batch file can I firstly, enumerate files in a folder, and then place a specific part of a given filename into a variable?
Reason being ShadowProtect incrementals have a naming convention such like:
SERVERNAME_DRIVELETTER_b00X_i000x - whereby b = base image, i = incremental number
I need to mount the latest incremental image, therefore need to parse the folder and find the latest incremental image, based on the number following the i in the filename.
Is this possible in batch?
Thanks!
Something like this should work:
If you have several matching files and want to do something with all of them, you need to put the processing code inside the loop.
Edit:
for /f: process either a file or the output of a command enclosed in single quotesdelims=_: fields in the processed content are separated by underscorestokens=1-4: assign the first four tokens to the parameters%%fthrough%%i(first parameter is the one given in theforstatement)dir /b *_*_*_*: list all file where the name contains at least 3 underscores with just their file name (the output of this command is processed by theforloop)setlocal EnableDelayedExpansion: expand variables at run time (otherwise assigning the parameters to variables wouldn’t work)For further details see
help forandhelp dir.