?
Solved

Read files and copy command prompt .bat

Posted on 2011-03-22
15
Medium Priority
?
718 Views
Last Modified: 2012-05-11
I want to make a .bat file that reads all the files in one dir then according the file rename look I want to make this:
1.-On one dir I have 2 key files called "picture.gif" and other "picture.png" suppose the dir where that pictures are is c:\temp\
2.-On other dir suppose c:\work I have mixed files with different names on .gif and .png,

Then I want an script that read names of the file of point 2 and if is a .gif file the bat copy the picture.gif from point 1 but with the name taken on point 2 to c:\destination\ is is a .png the same issue. Look more visual here is the interaction:

Read first file from point 2 dir the file is called 12345.gif then the .bat check if is .gif then make a copy of picture.gif of point 1 to c:\destination\ but with the name 12345.gif
Suppose exist now a .png inside point 2 called shot.png then the .bat check if is .png then make a copy of picture.png of point 1 to c:\destination\ but with the name shot.png

In the last example we had 2 pictures inside the dir c:\work one called 12345.gif and other called shot.png, on point 1 we have a permanent files I call reference that never change files called picture.gif and picture.png
Then the .bat detect makes a copy of picture.gif to c:\destination but with the name 12345.gif and picture.png copied to c:\destination too but with the name shot.png, in other words the pictures on point 1 always will be copied but with the names of point 2 directory files to c:\destination. The goal here is if on point 2 I have tons of file .gif or .png process all  the files and process like I mention to c:\destination

If somene can help with the full source code on .bat because really command prompt commands I don´t understand very well. Thank you
0
Comment
Question by:coerrace
  • 7
  • 4
  • 2
  • +1
15 Comments
 
LVL 59

Expert Comment

by:Bill Prew
ID: 35191061
Will there be other files beside GIF and PNG in location 2?

And if there are 20 different GIF files in location 2, after this script runs, they will all have the same content, being a copy of whatever is currently in file PICTURE.GIF from location 1, right?

~bp
0
 

Author Comment

by:coerrace
ID: 35191135
On location 2 just .gif and .png files no other files extension.

Regarding: And if there are 20 different GIF files in location 2, after this script runs, they will all have the same content, being a copy of whatever is currently in file PICTURE.GIF from location 1, right?
Yes If there are 20 files on .gif on point 2 we only take the reference of the name of the file on point 2 and copy to c:\destination the same picture.gif but with the names of point 2 the same as .png. In other words point 2 is only to take the file names reference and files of point 1 must be copied the same with the different names to c:\destination that names are of point 2 files.
Thank you
0
 
LVL 84

Expert Comment

by:David Johnson, CD, MVP
ID: 35191232
from the first part of the question I understand it to be the following
fixed filenames list in directory 1
unmatched filenames in directory 2 to be copied to directory 3 i.e.
picture.jpg (directory1) cd directory2 for %i in (*.jpg) do copy %i \directory3\picture.jpg

note that if there is more than 1 .jpg in directory 2 it will overwrite directory3\picture.jpg


 is this sort of what u want

copy the files from directory2 to directory3 with the filename from directory 1
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:coerrace
ID: 35191265
copy the files from directory2 to directory3 with the filename from directory 1 no just the opposite must be:

copy the files from directory1 to directory3 with the filename from directory 2



Thank you
0
 

Author Comment

by:coerrace
ID: 35191272
Another thing I note from your post there are only .gif and .png no .jpg
0
 
LVL 84

Expert Comment

by:David Johnson, CD, MVP
ID: 35191389
is there only 2 filenames that are required? and all going to a single destination directory?

i.e. directory1  file1.png file1.gif file2.png file2.gif file3.png (list of files)
directory 2 - destinationfilename.png destinationfilename.gif
directory 3 (after copy) destinationfilename.png destinationfilename.gif
(contents of destinationfilename.png will be file3.png)
destinationfilename.gif (contents of last *.gif)
0
 

Author Comment

by:coerrace
ID: 35191437
Yeah exactly is on that way, if you see directory 2 - destinationfilename.png destinationfilename.gif both files will be copied with different names the names of  directory1  file1.png file1.gif file2.png file2.gif file3.png (list of files
0
 
LVL 59

Accepted Solution

by:
Bill Prew earned 2000 total points
ID: 35195470
If I understand you correctly, this should do the job.  Save as a BAT file and adjust the folder names if needed.  Run from a command prompt.

@echo off
set Dir1=c:\temp
set Dir2=c:\work
for %%A in ("%Dir1%\*") do (
  echo Copying "%Dir2%\picture%%~xA" to "%%~A"
  copy /Y "%Dir2%\picture%%~xA" "%%~A">NUL
)

Open in new window

~bp
0
 
LVL 39

Expert Comment

by:BillDL
ID: 35196516
The question seems to have been answered above, but I'm really curious to know why you need lots of files in one folder with different names, but the GIF files are all the same image and the PNG files are all the same image.

Do you mind telling us what this is all about, or am I just being a very nosey person?  ;-)
0
 

Author Comment

by:coerrace
ID: 35198631
Works like a charm!!! Thank you man. I´m using this because we make elearn programs we have hundreds we made during the years now the teachers want to disapear some elements on gif and png from each course sometimes for each course are 150 elements mixed png and gif now imagine the work on rename each element manually and replace with my png or gif blank or edit the course to delete one by one from the main scene maybe months of work. Now my 2 reference pictures: picture.png and picture.gif are blank with transparency and if you replace with the names of the elements we want to disapear, then disapear like magic from scene and no need to edit or rename each time to replace from file.
Thank you
0
 

Author Closing Comment

by:coerrace
ID: 35198636
Excellent work!!!
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 35201519
Great, glad that was helpful.

~bp
0
 
LVL 39

Expert Comment

by:BillDL
ID: 35202300
Thanks for the explanation :-)
0
 

Author Comment

by:coerrace
ID: 35202312
No problem. Thanks billprew your script is running so nice and we have 20% of total work finished in a couple of hours!
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 35202411
Great, always nice to hear when a little bit of scripting can save a lot of manual time, thanks for the feedback.

~bp
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

OfficeMate Freezes on login or does not load after login credentials are input.
When you try to share a printer , you may receive one of the following error messages. Error message when you use the Add Printer Wizard to share a printer: Windows could not share your printer. Operation could not be completed (Error 0x000006…
This Micro Tutorial will teach you how to the overview of Microsoft Security Essentials. This is a free anti-virus software that guards your PC against viruses, spyware, worms, and other malicious software. This will be demonstrated using Windows…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

749 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