Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Read files and copy command prompt .bat

Posted on 2011-03-22
15
Medium Priority
?
716 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 2
  • +1
15 Comments
 
LVL 58

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 83

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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

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 83

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 58

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 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 58

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 58

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

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

I'm a big fan of Windows' offline folder caching and have used it on my laptops for over a decade.  One thing I don't like about it, however, is how difficult Microsoft has made it for the cache to be moved out of the Windows folder.  Here's how to …
When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup" or a blinking cursor with black screen. A loop for Auto repair will start but fix nothing.  You will be panic as there are no back…
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…
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

636 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