1.

Solve : Count on file name?

Answer»

Hi

I have written some code to move some files around and to ultimately receive data from a 3rd party via FTP and format that data by calling a couple of external processes ready for use by the user.

I guess as a validation check I would like to count the number of instances of *.tab (filename) in a given directory, and if the count = 1 then go and complete the rest of the script otherwise (i.e. count of *.tab>1) to end so that the user can select a different file to run on the received data.

Any ideas?
Thanks The simplest thing would be to do something like :

Do a directory listing of all the *.TAB files
Count the number of occurrences
Stick the result into an environment variable and test it

Code: [Select]Dir /b *.tab | Find /c /i ".tab"> $temp$
Set /P TabCount=<$temp$
Del $temp$

GrahamSorry, I'm a rookie with this....

So would I use errorlevels to get DOS to select whether it should continue through my script or go to :End? If so how would this look?

at the moment I have :-

Quote

rem there's numerous basic DOS routines above this line
cd C:\New_Programs\InComing\
FTP -s:C:\New_Programs\Receive\GetFtp.txt ftp.someorg.org
Rem
Ren C:\New_Programs\InComing\*.tab SingleReturnFile.tab
rem the script goes on to tranbslate the tab file to csv, and import to access

I am THINKING that I need something where I have put the blank remark line to perform the action that I want.

TabCount holds the number of files with a tab extension, so you would do something like

If TabCount NEQ 1 GoTo ErrorLabel
Rem else continue, only 1 present

GrahamThanks Graham. Almost got it I think. Scratching my head over these following SCENARIO though:-

Quote
dir /b *.tab | find /c /i ".tab"> $test$
Set /P TabCount =< $test$
Del $test$

Ok these commands result in the variable having a value of 1 (I have checked using Set)

But with the following code:-

Quote
If TabCount EQU 1 Goto OneReturned
IF TabCount NEQ 1 Goto TooMany

The result is always NEQ 1 - have I MISSED something?

Thanks

Whoops, my fault

I forgot to wrap the TabCount in % symbols

It should have read :

If %TabCount% EQU 1 Goto OneReturned
If %TabCount% NEQ 1 Goto TooMany

If you really want to test that the condition, try this

REM test line below
Set TabCount=2
If %TabCount% EQU 1 Goto OneReturned
If %TabCount% NEQ 1 Goto TooMany

GrahamSpot on!

Edit:
ANYBODY reading this and wanting to use similar syntax, it is worth checking the name of your variable - as the set up above requires checking the condition against %TabCount % and not %TabCount%. To check this, once you have done this bit:-
Quote
dir /b *.tab | find /c /i ".tab"> $test$
Set /P TabCount =< $test$
Del $test$

Issue a Set command to see how the variable is held. Mine came out as
TabCount =1

Therefore when I issued the If %TabCount% EQU 1 go do something - it ignored the line as the condition was not met. Changing to If %TabCount % EQU 1 go do something worked.

ANyway, thanks for the solution Graham!


Discussion

No Comment Found