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
Find and copy file
Moderator: DosItHelp
Re: Find and copy file
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.
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
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.
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
https://www.dostips.com/DosCommandIndex.php#ROBOCOPY
Steffen
Did you try to exclude the path? As you see, according to the manpage it should be supported./XD dirs [dirs]... :: eXclude Directories matching given names/paths.
Steffen
Re: Find and copy file
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
OK, so I know that I have notepad.exe in C:\Windows, C:\Windows\System32, C:\Windows\SysWOW64, and a couple more subdirectories.
This code doesn't display the notepad.exe in C:\Windows\System32 which means the exclusion works.
Steffen
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
Steffen
Re: Find and copy file
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..
Now the hard part... teach people to start the file..