Page 1 of 1
Find and copy file
Posted: 09 Mar 2021 07:34
by karlsan
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..
Best regards
Karl
Re: Find and copy file
Posted: 09 Mar 2021 12:28
by aGerman
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
Re: Find and copy file
Posted: 10 Mar 2021 03:10
by karlsan
Oh, thank you. That worked fine with a copy command. But... there is always a but.
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.
Re: Find and copy file
Posted: 10 Mar 2021 04:54
by aGerman
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
Re: Find and copy file
Posted: 10 Mar 2021 05:01
by karlsan
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.
Re: Find and copy file
Posted: 10 Mar 2021 07:00
by aGerman
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
Re: Find and copy file
Posted: 12 Mar 2021 04:51
by karlsan
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..