Solved

Read files and copy command prompt .bat

Posted on 2011-03-22
15
707 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 52

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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 52

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 52

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 52

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…

932 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

10 Experts available now in Live!

Get 1:1 Help Now