Find a file in a dir and if found copy it

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"
LuckyLucksAsked:
Who is Participating?
 
Bill PrewConnect With a Mentor Commented:
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
 
Pasha KravtsovSupport EngineerCommented:
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
 
LuckyLucksAuthor Commented:
sorry windows batch only at the moment
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
Bill PrewCommented:
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
 
LuckyLucksAuthor Commented:
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
 
LuckyLucksAuthor Commented:
Is there a way to err log the above code into a separate log file?
0
 
LuckyLucksAuthor Commented:
Didnt worl. Pls see attached screen shot of all execution
ErrorDoc.docx
0
 
Bill PrewCommented:
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
 
LuckyLucksAuthor Commented:
The source file does have records and is in the same folder as the batch.
0
 
LuckyLucksAuthor Commented:
Also a sample in the src file is like this:
421ff817e-fdde-471a-b5d1-127213d51.pdf
24a58ce1-499f-4b36-8225-868247ffb.pdf
0
 
LuckyLucksAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.