Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more


DOS DIR returns just one file

Posted on 2010-08-26
Medium Priority
Last Modified: 2013-11-09
Hi I ma trying to create a little change control update using a dos batch file.

I copy a file from a remote location to a local location if it is newer than the local version.
I then want to run that program using Access.

All works fine except if there is more than 1 file that matches my mask character in the DIR command.

What I would like the DIR to do is to stop looking when it finds the first file.
I have sorted the files so that the newest is at the top

Any ideas. Here's my script. My variable %concat% currently has 2 files in it which caused the start command to obviously fail

set copycmd=/y
xcopy "Q:\Quality System Software Client\*.mdb" "C:\Selection Software\" /d

for /f "delims=" %%a in ('dir "C:\Selection Software\BW Quality*" /b /s' /o-D) do (
       call set concat=%%concat%%%%a

Start "C:\Program Files\Microsoft Office\OFFICE11 \MSACCESS.EXE" "%concat%"

Question by:EWHTLC
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
LVL 57

Accepted Solution

Bill Prew earned 1400 total points
ID: 33533667
This should work, it will exit the FOR loop after the first file name is assigned to concat:

set copycmd=/y
xcopy "Q:\Quality System Software Client\*.mdb" "C:\Selection Software\" /d
for /f "delims=" %%a in ('dir "C:\Selection Software\BW Quality*" /b /s' /o-D) do (
  set concat=%%a
  goto :ExitFor
Start "C:\Program Files\Microsoft Office\OFFICE11 \MSACCESS.EXE" "%concat%"

LVL 14

Assisted Solution

by:Ben Personick (Previously QCubed)
Ben Personick (Previously QCubed) earned 600 total points
ID: 33534253
if it doesn't work, try this code instead (BP I assume you say 'should because you didn't test and are not 100%),

SET "copycmd=/y"
XCopy "Q:\Quality System Software Client\*.mdb" "C:\Selection Software\" /d
FOR /F "delims=" %%a in ('DIR "C:\Selection Software\BW Quality*" /B /S /A:-D /O:D) DO SET "concat=%%a"
START "C:\Program Files\Microsoft Office\OFFICE11 \MSACCESS.EXE" "C:\Selection Software\%concat%"

Open in new window

LVL 57

Expert Comment

by:Bill Prew
ID: 33534814
Hello Q,

For the original poster, just to clarify, there is a subtle difference between the two approaches that is important to understand.  Either should work, but since the code difference if fairly small, I wanted to point it out.

In the first solution that I posted, I stuck with the sort order you were using, and as a result we want to get just the FIRST file and process that in Access.  As a result, on the first time through the FOR loop, we save the filename to a variable, and then exit the FOR loop.

In the second solution, rather than exit the FOR loop on the first file, we actually let ALL files loop through the FOR, and since we re assign the concat variable for each file name, it will have the value of the LAST file processed by the for loop.  As a result, Q reversed the sort order so that the file you wanted is now the last file in the list, not the first.

Either approach is valid, and you'll see both approaches used.  The first approach is a bit more efficient if you have a large number of files that you loop over, since it only processes the first one.  But it does use a goto, not highly desirable in most languages, and it jumps out of a FOR loop, again not best practice but acceptable.  Unfortunately there is no other way to gracefully exit the FOR loop before it cycles through all files.  In the second approach the code is a bit simpler, but you do the SET for each file processed until you get to the last one, so that consumes a bit of extra processing.

Hope that helps you understand the options better, let us know your questions.

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

LVL 14
ID: 33534900
Great Summarization of the two Techniques Bill!  =)

Author Comment

ID: 33539593
A slight tweak to the first one.
Works an absolute treat.
I will save both versions in my tips folder.
Really useful
LVL 14
ID: 33541435
Great!  Glad we could be of help!


Author Closing Comment

ID: 33562772
Thanks guys.
Extremely useful
LVL 14
ID: 33562836
thanks vMuch for the points & the praise =)

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Making a simple AJAX shopping cart Couple years ago I made my first shopping cart, I used iframe and JavaScript, it was very good at that time, there were no sessions or AJAX, I used cookies on clients machine. Today we have more advanced techno…
In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Suggested Courses

649 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