We help IT Professionals succeed at work.

Find and copy files script

iris01
iris01 asked
on
I currently have need to create a script to find all files of certain types (i.e doc, xls, pdf, etc) on a users hard drive and move them into their "my documents" folder, this is as part of an upcoming desktop image refresh.

Ideally the script will also write a line to a log file in a central location just specifying how big the folder is after the file copy.

I've been through everything I can find on the subject, but haven't been able to find a script I can make work for my purposes, note I am not a scripter, I generally just butcher other scripts to fit my needs so please bare this in mind when answering!

I believe my biggest issue is cyclic copying, in that the target folder is a child of the source folder which obviously introduces a loop (and an error when running vbscripts or batch files!)

Please note, I can't use any third party tool to perform this, as it needs to be as easy to roll out as possible.

Thanks in advance.
Comment
Watch Question

Bill PrewTest your restores, not your backups...
SILVER EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
Does this need to be a VBS script, or would a DOS BAT script be okay, it could be done fairly easily there.

~bp

Author

Commented:
Batch would be OK, I've attempted this with Xcopy however haven't managed to get the exclude switch to work at all to exclude the target folder, without this you hit the cyclic copy issue!
Bill PrewTest your restores, not your backups...
SILVER EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
Yes, seems to me I have had trouble getting the /EXCLUDE to work with XCOPY, let me poke a little, and if I can't get that to work then I'll work up an alternative.

Have you looked at ROBOCOPY at all as an alternative, it's not native on XP (and that may rule it out), but it can be downloaded and placed on the target system if that worked.

~bp
Test your restores, not your backups...
SILVER EXPERT
Expert of the Year 2019
Top Expert 2016
Commented:
Okay, it doesn't look like XCOPY with /S will allow the target folder to be a child of the source folder, which makes sense.  Even with /EXCLUDE it doesn't seem to care.

Given that, here's a somewhat brute force approach that should get the job done.  It basically scans the base folder (and all subfolders) for any files with the extensions you provide, and copies them to the destination folder.  I use a FIND /V to filter out any from that list that are in the destination folder already, since we don't want to copy those.  See if this makes sense.

@echo off
setlocal EnableDelayedExpansion

REM Define source and target folders, and list of extensions to copy
set BaseDir=C:\
set DestDir=%USERPROFILE%\My Documents\
set CopyExts=txt,doc,xls,pdf

REM Build the full path to the source files (save some typing)
set BaseList=
for %%A in (%CopyExts%) do (
  set BaseList=!BaseList! "%BaseDir%*.%%A"
)

REM Find all matching files in the source, and copy to dest
REM Remove any matching files that are already in the dest folder
for /F "tokens=*" %%A in ('dir /a-d /s /b %BaseList% ^| find /I /V "%DestDir%"') do (
  echo copy /Y "%%~A" "%DestDir%"
)

Open in new window

~bp

Author

Commented:
Thanks Bill

testing your script, it seems to be working (i.e. I can see the batch file writing copy /Y... commands for each file it finds), however no files end up in the target folder (or anywhere else I can find!)

I've decided on the robocopy method, seems to work fine with the .exe file stored in a shared folder so that meets my needs quite well.

Have 500 points for your efforts!
Bill PrewTest your restores, not your backups...
SILVER EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
Thanks.

Yes, I often set up my BAT files in a sort of "debug" mode where the actual command(s) that will be executed are just displayed initially, this gives me (and you) and chance to see what it will do before it does anything, just in case it looks bad.  Not too dangerous with a COPY, but with MOVE, RENAME, and DELETE it can be a life saver.

Anyway, you would just change this line:

  echo copy /Y "%%~A" "%DestDir%"

to

  copy /Y "%%~A" "%DestDir%"

when it looked good.

~bp

Author

Commented:
Ah of course, how did I miss that!

Explore More ContentExplore courses, solutions, and other research materials related to this topic.