Solved

Dos FOR loop question

Posted on 2011-03-04
10
919 Views
Last Modified: 2012-06-21
Here is my batch file.

For /f %%a IN (‘R:\ /b *.mdb’) do call "C:\Program Files\Microsoft Office\Office14\msaccess.exe" %%a /compact


I am trying to run through the R drive and grab all the .mdb's and do compact from msaccess but when I run this through command prompt I get this error:

%%a was unexpected at this time

Any suggestions on how to get this For loop command to work properly.
0
Comment
Question by:goretailpro
  • 3
  • 2
  • 2
  • +2
10 Comments
 

Author Comment

by:goretailpro
ID: 35040589
0
 
LVL 95

Expert Comment

by:Lee W, MVP
ID: 35040599
you cut and paste, didn't you?  

:-)

You have:
For /f %%a IN (‘R:\ /b *.mdb’) do call "C:\Program Files\Microsoft Office\Office14\msaccess.exe" %%a /compact
try
For /f %%a IN ('R:\ /b *.mdb') do call "C:\Program Files\Microsoft Office\Office14\msaccess.exe" %%a /compact

(Note the TYPE of quote surrounding R:\ /b *.mdb)
0
 

Author Comment

by:goretailpro
ID: 35040619
I did but I still get the same message...
3-4-2011-4-40-03-PM.jpg
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 35040622
I think he means:

  IN ('dir/s/b R:\*.mdb')
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 35040630
... and if any of your .mdb files have spaces in the name, put quotes here:  "%%a" /compact
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 43

Expert Comment

by:Steve Knight
ID: 35040702
Oh and the obvious... if you are typing that at the command line use %a instead of %%a (which is for batch files).

Steve
0
 
LVL 26

Assisted Solution

by:farhankazi
farhankazi earned 250 total points
ID: 35040712
@goretailpro:
Are you executing above statement from command line? if yes then you need to remove one % sign from FOR statement.

FOR /F "delims=*" %a IN ('DIR /b /s R:\*.mdb') do call "C:\Program Files\Microsoft Office\Office14\msaccess.exe" "%a" /compact

in batch file, following should work:

FOR /F "delims=*" %%a IN ('DIR /b /s R:\*.mdb') do call "C:\Program Files\Microsoft Office\Office14\msaccess.exe" "%%a" /compact

Hope this helps!
Farhan
0
 
LVL 26

Expert Comment

by:farhankazi
ID: 35040728
Sorry Steve, didn't refresh the page :(
0
 

Author Comment

by:goretailpro
ID: 35056650
Is there a way to exclude specific MDB's?
0
 
LVL 43

Accepted Solution

by:
Steve Knight earned 250 total points
ID: 35056848
You could have a list of entries to exclude in a text file perhaps, e.g.

somefile.mdb
another.mdb
c:\someexcludedir

then use a find /v command as part of the dir command to exclude those lines, they would only need to be a partial match (but not wildcards), e.g. if you want to exclude a whole subdir worth then include that path in the file

@echo off
set exclude="r:\exclude.txt"
FOR /F "delims=*" %%a IN ('DIR /b /s R:\*.mdb ^|findstr /i /v /g:%exclude%') do call "C:\Program Files\Microsoft Office\Office14\msaccess.exe" "%%a" /compact

That says to take the dir listing of all MDB files in the R: drive and all subdirs then pass that to findstr to find all lines that do NOT match any of the search strings in the r:\exclude.txt file.



Steve
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now