Solved

Find a file in a dir and if found copy it

Posted on 2014-04-16
11
267 Views
Last Modified: 2014-04-18
Hi I want to find a file given its name and if founf want to copy it to another folder , how do I?


find "myfile.pdf"
0
Comment
Question by:LuckyLucks
  • 7
  • 3
11 Comments
 
LVL 5

Expert Comment

by:Pasha Kravtsov
ID: 40005477
Is it necessary to use windows batch?

python is much easier :)
import subprocess

ls_output = subprocess.Popen(["ls"], stdout=subprocess.PIPE, shell=True)
(out, err) = ls_output.communicate()

for x in out.split("\n"):
  if x == "myfile.pdf":
    subprocess.call(["copy %s" % x, "C:\Desktop\folder"])

Open in new window

0
 

Author Comment

by:LuckyLucks
ID: 40005504
sorry windows batch only at the moment
0
 
LVL 52

Expert Comment

by:Bill Prew
ID: 40005615
Where do you want to "look" for the file?

If it was just in a particular folder, then you can change to the folder and copy it, like this.  But I suspect the file is in a less obvious place?
cd c:\somedir
copy myfile.pdf d:\destdir

Open in new window

But if it could be anywhere under a base folder, then you could do something like this, which searches for the file in all subfolders and copys it when found:

for /f "tokens=*" %%A in ('dir /b /s /a-d "c:\somedir\myfile.pdf") do copy "%%~A" "d:\destdir"

Open in new window

~bp
0
 

Author Comment

by:LuckyLucks
ID: 40005652
ok maybe I am not clear......


I have a file called src_file which has lines of filename, like:

myfile1.pdf
myfile2.pdf


I need to iterate thru this list of files and on two specific folders on the server, want to check if they exist there. If they do, I want to copy them to say C:/Found (also on server).

The two folders to search are \\2454-prod\folder1 and \\2454-prod\folder1\subfolder
0
 
LVL 52

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 40005668
Okay, that helps.  Give this a try, seems to do what you described.

@echo off
setlocal EnableDelayedExpansion

set ListFile=src_file.txt
set SearchFolders="\\2454-prod\folder1","\\2454-prod\folder1\subfolder"
set DestDir=C:\Found

for /f "usebackq tokens=*" %%F in ("%ListFile%") do (
  for %%D in (%SearchFolders%) do (
    if exist "%%~D\%%~F" copy "%%~D\%%~F" "%DestDir%"
  )
)

Open in new window

~bp
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:LuckyLucks
ID: 40005774
Is there a way to err log the above code into a separate log file?
0
 

Author Comment

by:LuckyLucks
ID: 40005829
Didnt worl. Pls see attached screen shot of all execution
ErrorDoc.docx
0
 
LVL 52

Expert Comment

by:Bill Prew
ID: 40006482
I didn't see any errors in that execution screen capture, it just appears that the source file had no records in it.

~bp
0
 

Author Comment

by:LuckyLucks
ID: 40006821
The source file does have records and is in the same folder as the batch.
0
 

Author Comment

by:LuckyLucks
ID: 40006922
Also a sample in the src file is like this:
421ff817e-fdde-471a-b5d1-127213d51.pdf
24a58ce1-499f-4b36-8225-868247ffb.pdf
0
 

Author Closing Comment

by:LuckyLucks
ID: 40008960
Worked with a few tweaks....the for loop for file was OK. For some reason the dir loop want working....maybe its env related, so I just ran the script twice , once for each of the two dirs.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Windows Rename FolderNames and SubfolderNames 9 54
Batch Script for opening the DOS prompt and run a command 9 105
Issue with cURL on Windows 7 82
how to run a batch process 16 44
I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

920 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

14 Experts available now in Live!

Get 1:1 Help Now