Solved

DOS DIR returns just one file

Posted on 2010-08-26
8
413 Views
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%"
............................................................

0
Comment
Question by:EWHTLC
  • 4
  • 2
  • 2
8 Comments
 
LVL 51

Accepted Solution

by:
Bill Prew earned 350 total points
Comment Utility
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
)
:ExitFor
Start "C:\Program Files\Microsoft Office\OFFICE11 \MSACCESS.EXE" "%concat%"

~bp
0
 
LVL 11

Assisted Solution

by:Ben Personick
Ben Personick earned 150 total points
Comment Utility
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

0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
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.

~bp
0
 
LVL 11

Expert Comment

by:Ben Personick
Comment Utility
Great Summarization of the two Techniques Bill!  =)
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:EWHTLC
Comment Utility
A slight tweak to the first one.
Works an absolute treat.
I will save both versions in my tips folder.
Really useful
0
 
LVL 11

Expert Comment

by:Ben Personick
Comment Utility
Great!  Glad we could be of help!

~Q
0
 

Author Closing Comment

by:EWHTLC
Comment Utility
Thanks guys.
Extremely useful
0
 
LVL 11

Expert Comment

by:Ben Personick
Comment Utility
thanks vMuch for the points & the praise =)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
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…

763 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

7 Experts available now in Live!

Get 1:1 Help Now