Find and copy file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
karlsan
Posts: 4
Joined: 09 Mar 2021 07:08

Find and copy file

#1 Post by karlsan » 09 Mar 2021 07:34

Hi!
I am writing a script to zip logfiles and then attach the file to an e-mail for the user to send. The problem is that sometimes one program has variable places to put the log files. My thought was to use a searchfunction in batch to find the file on C:\ and then copy it to a second folder zip it and add it to a e-mail. I don't know if it is possible. I also need to exclude one folder in the search.

So does anyone have a clue if it is possible to search for a file on C:\ and exclude one directory or file and then copy it to another folder?
I have googled, and not found what I am looking for. Maybe just bad at google.. :P

Best regards
Karl

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Find and copy file

#2 Post by aGerman » 09 Mar 2021 12:28

Starting a file search in the root of a drive is usually a bad idea. To answer your question - There is no way that I know of to exclude a directory from searching. You might be able to exclude it from the search results. However that's not the same and will likely be still terribly slow.

Steffen

EDIT: After thinking about it, ROBOCOPY might be able to locate the file. The following line searches for "my.txt" in C: and excludes the Windows folder.

Code: Select all

@echo off &setlocal
for /f "tokens=*" %%i in ('robocopy "C:\." " nul" "my.txt" /xd "windows" /l /s /xx /r:1 /w:1 /ns /nc /ndl /np /njh /njs') do echo "%%i"
pause

karlsan
Posts: 4
Joined: 09 Mar 2021 07:08

Re: Find and copy file

#3 Post by karlsan » 10 Mar 2021 03:10

Oh, thank you. That worked fine with a copy command. But... there is always a but. :P
Do you know if I can in anyway use full paths to exclude? I want to exclude "%programdata%\telephone communications server\diag". Reason is that it creates the telephone and diag folder in different places. So because of that I can't exclude just the foldername... I have used full paths with robocopy before... but maybe I am doing something wrong in this instant.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Find and copy file

#4 Post by aGerman » 10 Mar 2021 04:54

https://www.dostips.com/DosCommandIndex.php#ROBOCOPY
/XD dirs [dirs]... :: eXclude Directories matching given names/paths.
Did you try to exclude the path? As you see, according to the manpage it should be supported.

Steffen

karlsan
Posts: 4
Joined: 09 Mar 2021 07:08

Re: Find and copy file

#5 Post by karlsan » 10 Mar 2021 05:01

I didn't get that to work, XD worked on foldernames for me but not paths. But I found a workaround. Before your command I coded so it echoes paths into a txt, then after the search I use xcopy with the txt as an exclude. So it don't make a copy of the file from the paths in the txt, if there is any. So right now it seems to work as intended. Maybe a little rough, but as long as it does what is supposed to. Shall test it on a few other PCs first. It will save me a lot of driving around to just get a file. :)

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Find and copy file

#6 Post by aGerman » 10 Mar 2021 07:00

OK, so I know that I have notepad.exe in C:\Windows, C:\Windows\System32, C:\Windows\SysWOW64, and a couple more subdirectories.

Code: Select all

@echo off &setlocal
for /f "tokens=*" %%i in ('robocopy "C:\." " nul" "notepad.exe" /xd "C:\windows\system32" /l /s /xx /r:1 /w:1 /ns /nc /ndl /np /njh /njs') do echo "%%i"
pause
This code doesn't display the notepad.exe in C:\Windows\System32 which means the exclusion works.

Steffen

karlsan
Posts: 4
Joined: 09 Mar 2021 07:08

Re: Find and copy file

#7 Post by karlsan » 12 Mar 2021 04:51

I must have broken the code in someway before. Now it works. Thanks for all the help. :)
Now the hard part... teach people to start the file.. :P

Post Reply