Batch scripts: find and copy file from unknown directory?

房东的猫 提交于 2019-12-11 11:36:01

问题


I need to make a batch file for my groupmates (actually, I need to make a sequence of the program, but for simple to ask, I use batch file).

The batch's job is copy a specific file in their computer to the batch's folder. But the problem is I don't know the path to that file of all my groupmates.

Here are the things I need:

  1. Help my groupmates choose their path to that file. (Maybe just auto-find that file in their computers).

  2. Copy that file and paste it into the batch file's folder (which include my other programs).

  3. After all my other programs finished their job, copy and replace that file to its original folder.

Do you have any script that might help?


回答1:


You can start with this batch code :

@echo off
Title Search for a file by name (Wildcard accepted) by Hackoo 2014
mode con cols=90 lines=5 & color 9B
echo(
Set /p "FileName=Please Enter the name of the file to find (Wildcard accepted) : "
echo(
Set Tmp=Tmp.txt
Set SearchResult=SearchResult.txt
Call :BrowseFolder "Select the Source folder" "C:\Program"
Set LocationFolder=%MyFolder%
echo You chose to looking into "%LocationFolder%" for this file "%FileName%"
echo( & cls & Color 0A
echo(  & echo  Please Wait for moment .... Searching for "%FileName%" on "%LocationFolder%"
Where /r "%LocationFolder%" "%FileName%" > %Tmp%
Cmd /U /C Type %Tmp% > %SearchResult%
Del %Tmp%
Start %SearchResult%
::******************************************************************************
:BrowseFolder
    set MyFolder=
    set vbs="%temp%\_.vbs"
    set cmd="%temp%\_.cmd"
    for %%f in (%vbs% %cmd%) do if exist %%f del %%f
    for %%g in ("vbs cmd") do if defined %%g set %%g=
    >%vbs% echo set WshShell=WScript.CreateObject("WScript.Shell") 
    >>%vbs% echo set shell=WScript.CreateObject("Shell.Application") 
    >>%vbs% echo set f=shell.BrowseForFolder(0,%1,0,%2) 
    >>%vbs% echo if typename(f)="Nothing" Then  
    >>%vbs% echo wscript.echo "set MyFolder=Dialog Cancelled" 
    >>%vbs% echo WScript.Quit(1)
    >>%vbs% echo end if 
    >>%vbs% echo set fs=f.Items():set fi=fs.Item() 
    >>%vbs% echo p=fi.Path:wscript.echo "set MyFolder=" ^& p
    cscript //nologo %vbs% > %cmd%
    for /f "delims=" %%a in (%cmd%) do %%a
    for %%f in (%vbs% %cmd%) do if exist %%f del %%f
    for %%g in ("vbs cmd") do if defined %%g set %%g=
::******************************************************************************

EDIT : 28/06/2015 at 07:00

@echo off
Title Search for a file by name and copy it (Wildcard accepted) by Hackoo 2015
mode con cols=90 lines=5 & color 9B
Set /p "FileName=Please Enter the name of the file to find (Wildcard accepted) : "
Set SearchResult=SearchResult.txt
Call :BrowseFolder "Select the Source folder" "C:\Program"
Set LocationFolder=%MyFolder%
echo You chose to looking into "%LocationFolder%" for this file "%FileName%"
echo( & cls & Color 0A
echo(  & echo  Please Wait for moment .... Searching for "%FileName%" on "%LocationFolder%"
where /r "%LocationFolder%" "%FileName%" > %SearchResult%
Goto:CopyMyFile
::******************************************************************************
:BrowseFolder
    set MyFolder=
    set vbs="%temp%\_.vbs"
    set cmd="%temp%\_.cmd"
    for %%f in (%vbs% %cmd%) do if exist %%f del %%f
    for %%g in ("vbs cmd") do if defined %%g set %%g=
    >%vbs% echo set WshShell=WScript.CreateObject("WScript.Shell") 
    >>%vbs% echo set shell=WScript.CreateObject("Shell.Application") 
    >>%vbs% echo set f=shell.BrowseForFolder(0,%1,0,%2) 
    >>%vbs% echo if typename(f)="Nothing" Then  
    >>%vbs% echo wscript.echo "set MyFolder=Dialog Cancelled" 
    >>%vbs% echo WScript.Quit(1)
    >>%vbs% echo end if 
    >>%vbs% echo set fs=f.Items():set fi=fs.Item() 
    >>%vbs% echo p=fi.Path:wscript.echo "set MyFolder=" ^& p
    cscript //nologo %vbs% > %cmd%
    for /f "delims=" %%a in (%cmd%) do %%a
    for %%f in (%vbs% %cmd%) do if exist %%f del %%f
    for %%g in ("vbs cmd") do if defined %%g set %%g=
::******************************************************************************
:CopyMyFile
Cls
for /f "delims=*" %%a in (%SearchResult%) do (echo Copy "%%a" "%~dp0" & Copy "%%a" "%~dp0")
Pause 



回答2:


dir c:\nameoffiletosearch /s /b > %temp%\results.txt will create a list with full path of all occurences of your file in drive c.

You could even wrap this in a loop that searches multiple drives.

Then get the path from the results.txt and use it with your copy command.



来源:https://stackoverflow.com/questions/31087866/batch-scripts-find-and-copy-file-from-unknown-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!