Solved

Find a file in a dir and if found copy it

Posted on 2014-04-16
11
266 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
Comment Utility
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
Comment Utility
sorry windows batch only at the moment
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
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
Comment Utility
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 51

Accepted Solution

by:
Bill Prew earned 500 total points
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

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

Author Comment

by:LuckyLucks
Comment Utility
Didnt worl. Pls see attached screen shot of all execution
ErrorDoc.docx
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
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
Comment Utility
The source file does have records and is in the same folder as the batch.
0
 

Author Comment

by:LuckyLucks
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

743 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

8 Experts available now in Live!

Get 1:1 Help Now