简体   繁体   中英

Batch File/Powershell - enumerating files in a folder

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:

@echo off

setlocal EnableDelayedExpansion

for /f "delims=_ tokens=1-4" %%f in ('dir /b *_*_*_*') do (
  set servername=%%f
  set driveletter=%%g
  set base_image=%%h
  set increment=%%i
)

echo !servername!
echo !driveletter!
echo !base_image!
echo !increment!

endlocal

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 quotes
  • delims=_ : fields in the processed content are separated by underscores
  • tokens=1-4 : assign the first four tokens to the parameters %%f through %%i (first parameter is the one given in the for statement)
  • 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 the for loop)
  • setlocal EnableDelayedExpansion : expand variables at run time (otherwise assigning the parameters to variables wouldn't work)

For further details see help for and help dir .

You could always use vbscript or jscript. It is much more powerful than batch files. Also jscript and vbscript hosts are available also on machines that don't have powershell!

Link for enumeration: http://www.techimo.com/forum/webmastering-programming/100453-recursive-javascript-list-all-files-folders-given-folder.html

Jscript string reference: http://msdn.microsoft.com/en-us/library/bxsyt3yc(v=vs.80).aspx

You should be able to combine the two.

you run your jscript (I prefer jscript to vbscript because of its resemblance to javascript)

cscript scriptname

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM