?
Solved

Problem using asterisk w/ filename in batch file

Posted on 2006-05-29
21
Medium Priority
?
830 Views
Last Modified: 2012-08-14
Hello ~

Using the following command in a batch file to copy and rename a file I experience the new file being only 9 Kb and inoperable, when the file is actually about 14 Mb.  However, when I remove the asterisk (and modify the original filename accordingly), the command works just fine.  I'd like to be able to use an asterisk - or equivalent - in order for the original filename to contain some preceeding characters.

copy K:\"Access Database"\*update2003_FE.bak C:\"Database"\Forms2003_FE.mdb

Looking forward to the answer to this one!

Many Thanks
0
Comment
Question by:Chi Is Current
  • 9
  • 9
  • 3
21 Comments
 
LVL 97

Expert Comment

by:Lee W, MVP
ID: 16787413
Not sure where your error is specifically, you do realize your copying potentially multiple files to a single file, repeatedly overwriting things - unless Forms2003_FE.mdb is a directory...

copy K:\"Access Database"\*update2003_FE.bak C:\"Database"\Forms2003_FE.mdb


If * is not working, you might try this...


Single command on the command - if you use this in a batch file then double the % signs.

for /f "tokens=*" %a in ('dir /b *.bak ^| find /i "update2003_FE"') Do @copy "k:\access database\%a" "c:\database\"
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 16787421
Not sure why that would be but have you tried the quotes around the whole name like this:

copy "K:\Access Database\*update2003_FE.bak" "C:\"Database\Forms2003_FE.mdb"

or possibly add -b (binary) switch before the first filename.

or use xcopy instead?

Is there any chance of more than one file of that specification in the directory as if there is it will combine files into one and produce one corrupt database file.

Steve
0
 
LVL 2

Author Comment

by:Chi Is Current
ID: 16787461
Thank you both for your prompt replies.  Indeed, it has me stumped too!

I do realize the potential for '*update2003_FE.bak' to refer to multiple files, and make certain it does not - it's a filename those leading six chanracters change, ex. 053006update2003_FE.bak, etc.  Old ones are deleted.  Forms2003_FE.mdb is a filename.

Quotes areound the entire name: copy "K:\Access Database\*update2003_FE.bak" results in no fle copy.

???  I don't understand your suggestion here: for /f "tokens=*" %a in ('dir /b *.bak ^| find /i "update2003_FE"') Do @copy "k:\access database\%a" "c:\database\"

Do you experience this same issue when you replicate this copy on your machine?
0
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 
LVL 97

Expert Comment

by:Lee W, MVP
ID: 16787487
The problem is, what you're trying to do as I read it doesn't make sense.  You're copying every file with "update2003_FE.bak" in its name to the file name Forms2003_FE.mdb - but copy doesn't work that way - you end up with a Forms2003_FE.mdb file that is identical to the LAST file copied that matches that spec - assuming it does match that spec.

The for command basically does a dir on the folder and "finds" all the files that match the "update2003_fe.bak" file name - it's kinda a manual asterisk.
0
 
LVL 2

Author Comment

by:Chi Is Current
ID: 16787517
Ahhh  Haaaaaa!

'Might be (most likely is) my syntax. With: copy K:\"Access Database"\*update2003_FE.bak C:\"Database"\Forms2003_FE.mdb - I'd like to copy any file located in K:\"Access Database"\ directory with a filename ending in 'update2003_FE.bak' to the 'C:\"Database"\' directory and re-name it: Forms2003_FE.mdb

Using xcopy does work, once I respond to a prompt asking: Is 'Forms2003_FE.mdb' a filename or directory?  -  When I specify filename = Voilá!

So, this question becomes: how can I write this statement to specify 'Forms2003_FE.mdb' as a filename.  I can go with xcopy - thouugh it would seem that copy should work as well.
0
 
LVL 2

Author Comment

by:Chi Is Current
ID: 16787567
P.S. ~  There will be only one file ending in 'update2003_FE.bak' in the K:\"Access Database"\ directory  at a time.
0
 
LVL 97

Expert Comment

by:Lee W, MVP
ID: 16787589
So what happens if you have TWO files with "update2003_FE.bak" in the name?  Or will that circumstance simply not happen?
0
 
LVL 97

Expert Comment

by:Lee W, MVP
ID: 16787636
Ok, then this should work:

for /f "tokens=*" %a in ('dir /b *.bak ^| find /i "update2003_FE"') Do @copy "k:\access database\%a" "c:\database\Forms2003_FE.mdb"


0
 
LVL 2

Author Comment

by:Chi Is Current
ID: 16787658
It won't happen. -  Really.

This batch file is simply a way for me to propagate a series of updates to workstations on a LAN.  Each update filename begins with a date string like: 053006update2003_FE.bak.  Each workstation has a shortcut to the batch file on the server.  When I put a new update up, I'll discard the old one.  It's just nice to give them different filenames, so I know which one I'm looking at.  I c-o-u-l-d give them the same file name, when I replace the old one,  but it's not quite as handy.  -  A great application for a batch file, if I do say so myself.

If I need something else to happen on the workstations, I just rewrite the batch file and replace it on the server.

0
 
LVL 2

Author Comment

by:Chi Is Current
ID: 16787688

for /f "tokens=*" %a in ('dir /b *.bak ^| find /i "update2003_FE"') Do @copy "k:\access database\%a" "c:\database\Forms2003_FE.mdb"

Results in nothing being copied.......
0
 
LVL 97

Accepted Solution

by:
Lee W, MVP earned 2000 total points
ID: 16787705
Error message?  Anything?  Take out the "@" symbol and run it again, note any error message.
0
 
LVL 97

Expert Comment

by:Lee W, MVP
ID: 16787706
Copy and paste the results here.
0
 
LVL 2

Author Comment

by:Chi Is Current
ID: 16787718
No error messages w/ the first suggestion.  However, removing the "@" works perfectly.  So, it's a 10.  I'll need to figure out the code though.  I'm still at DOS level 101.

I appreciate your willingness to hang in here.

Many Thanks, Jacob
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 16787726
Use echo F|xcopy .... to fudge the pressing F for file if you want to do it that way...

Steve
0
 
LVL 97

Expert Comment

by:Lee W, MVP
ID: 16787730
The @ only hides the command - prevents it from echoing back to the screen so run a few more tests and make sure.

Great place to learn batch scripting:
www.robvanderwoude.com
0
 
LVL 2

Author Comment

by:Chi Is Current
ID: 16787754
Nothing quite like a few more tests.....

Upon retesting w/ a fifferent file name, the cmd window just flashes breifly and no copy.  Bah!

I didn't mention I'm running this as a .bat file in on Windows 2000 desktop.
0
 
LVL 2

Author Comment

by:Chi Is Current
ID: 16787784
Steve ~

> Use echo F|xcopy .... to fudge the pressing F for file if you want to do it that way...

Works great - reliably, w/ different file names.  Its simplicity appeals to my rudimentary facility w/ DOS.

Well, I already awarded the points.  So, it'll have to be karma points for your assistance.  Thank you!

Jacob
0
 
LVL 97

Expert Comment

by:Lee W, MVP
ID: 16787794
Did you double the percent signs?  In a batch file, they must be doubled or they will error out.
0
 
LVL 2

Author Comment

by:Chi Is Current
ID: 16787809
I copied it directly as: for /f "tokens=*" %a in ('dir /b *.bak ^| find /i "update2003_FE"') Do @copy "k:\access database\%a" "c:\database\Forms2003_FE.mdb"
0
 
LVL 97

Expert Comment

by:Lee W, MVP
ID: 16787818
In my first comment, I said you'd have to double the percent signs in a batch file - try it with them doubled - And don't double click on the batch file right now - test this with a command prompt - when it's working, you can use the batch file, but until it's working, your making things more difficult as you won't be able to see error messages and the like.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 16788014
Never mind... I was a bit slow off the mark with this one, what with driving to work and things... points are hard to come by around here mind :-)

Steve
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Screencast - Getting to Know the Pipeline
Suggested Courses

850 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