Solved

Find a file in a dir and if found copy it

Posted on 2014-04-16
11
270 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
[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
  • 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 54

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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 

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 54

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
 

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 54

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
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 …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

730 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