|
Answer» I need help. I could work at this for days but I know there are some DOS GURU's in here that can figure this out in minutes.
I have a batch file that is almost COMPLETE but I need the final piece of code to make it unattended which I cannot figure out without pulling my hair out
What I want to do is: 1) Read the first file in a directory - variable that I already have 2) Get the 1st filename 3) Extract the digits between the parentheses of the filename 4) Pass them forward to the rest of the routine in a variable
Filenames look like: Filename(55).tib Filename(55)2.tib Filename(55)3.tib ... ... NOTE 55 is only an example. It could be 1-? between the parentheses but the parentheses are always there. (Yes, these are Acronis backup files)
I think this involves a FOR /F using tokens and delims. I got the following,primative routine, to work but I can't pass a variable into it let alone try and read a filename in. I've seen other examples of dir piped into other commands but I'm lost at this point and could do trial and error for days till I find something that works.
Help me PuhLeeez
@ECHO OFF CLS
for /f "tokens=2 delims=(+)" %%a in ("Filename(55)2.tib") do ( set /a setnum = %%a ) echo setnum = %setnum% pause
Code: [Select]set MyFileName=an arbitrary string(557).tib for /f "tokens=2 delims=()" %A in ("%MyFileName%") do set MySetNumber=%A
MySetNumber equals 557 In the above, the for /f command is used at the prompt, where the variables have only one percent sign (e.g. %A). In a batch script you use two percent signs (e.g. %%A)
Like this...
Code: [Select]set MyFileName=an arbitrary string(557)another arbitrary string.tib for /f "tokens=2 delims=()" %%A in ("%MyFileName%") do set MySetNumber=%%A echo MySetNumber is %MySetNumber% Code: [Select]MySetNumber is 557Thanks to everyone for your great examples and taking the time to come up with them!! From all the examples I was able to piece together a small routine that does just what I want it to do. Returns the number between the parentheses of the first file in a directory. Here's what I came up with:
@echo off :: setLocal EnableDelayedExpansion set targetdir=Where my .tib files are set setnum=0
for /f "tokens=2 delims=()" %%a in ('dir /b %targetdir%\*.tib') do ( set setnum=%%a goto loopexit ) :loopexit echo %setnum% pause
From all the suggestions I have a better grasp on the FOR /F command and a couple other tricks that were shown as well. I don't see the need for the need for the DelayedExpansion, which I now understand better, in what I'm doing here but, if it's considered good programming practice to always include it then maybe I should I leave it in there. I saw a suggestion somewhere else on using a GOTO to exit a FOR loop early and it works just fine for me since I only want the 1st .tib file in the directory.
If you see any loopholes or downfalls in what I have here, I'm open for suggestions.
Again, KUDOS to all! You've been "thanked" appropriately too...Quote from: pcjunky on April 22, 2011, 03:37:53 PM the first file in a directory. Before I read your LATEST post, I had been thinking about this. About how you want to define the "first" file in a directory. I am guessing that you mean what you think is going to be the first file listed by a plain dir command ["plain" because it is without any /o (order) switches.] That is to say, the files sorted by NAME and in ascending order. In other words /on is implied. This is all very well, but there is an environment variable which can be present or absent in any PARTICULAR system called %dircmd% which sets default dir switches which are then used when just dir without switches is issued. e.g. it might be /od /a-d or whatever. If you are sure it is always going to be null, all well and good. Otherwise it is good practice and safer to specify. See dir /? for all the switches.
Which BRINGS me to a way of avoiding the clumsy GOTO and the label. If you use dir /o-n then the sort is reversed so that the "first" file in order of name will be the last listed and therefore in your loop will be the last value held by %%a so you can just have a one line FOR statement.
Code: [Select]for /f "tokens=2 delims=()" %%a in ('dir /b /o-n %targetdir%\*.tib') do set setnum=%%a Awesome Trout! That's even better! I'm an old COBOL programmer and never, ever left a loop so abruptly as a GOTO (unless absolutely necessary) but, I was at a loss and didn't know a graceful way out of the loop to preserve that 1st %setnum%. I never thought of a descending dir as you pointed out. I tested it and I'm putting that change in my routine now. I don't expect more than 8 files in the directory at any given time so the extra cycles are negligible to me. You are correct, my only concern IS the first file name in an ascending dir listing. One line of code to do all of this? Awesome.
Thanks again!
|