Solved

Read files and copy command prompt .bat

Posted on 2011-03-22
15
706 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 51

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 78

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
 

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 78

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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 51

Accepted Solution

by:
Bill Prew earned 500 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 38

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 51

Expert Comment

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

~bp
0
 
LVL 38

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 51

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

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

I recently purchased an HP EliteBook 2540p notebook/laptop. It has two video ports on it – VGA and DisplayPort. HP offers an optional docking station for the 2540p that also has both a VGA port and a DisplayPort. There are numerous online reports do…
New Windows 7 Installations take days for Windows-Updates to show up and install. This can easily be fixed. I have finally decided to write an article because this seems to get asked several times a day lately. This Article and the Links apply to…
This Micro Tutorial will go in depth within Systems and Security in Windows 7 and will go into detail regarding Action Center, Windows Firewall, System, etc. This will be demonstrated using Windows 7 operating system.
The viewer will learn how to successfully download and install the SARDU utility on Windows 7, without downloading adware.

706 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

17 Experts available now in Live!

Get 1:1 Help Now