批处理脚本:从未知目录中查找和复制文件?

时间:2015-06-27 10:46:33

标签: windows batch-file copy

我需要为我的同学制作一个批处理文件(实际上,我需要制作一个程序序列,但为了简单问一下,我使用批处理文件)。

批处理作业是将计算机中的特定文件复制到批处理文件夹中。但问题是我不知道所有同学的档案路径。

以下是我需要的东西:

  1. 帮助我的同学选择他们的文件路径。 (也许只是在他们的计算机中自动查找该文件)。

  2. 复制该文件并将其粘贴到批处理文件的文件夹(包括我的其他程序)中。

  3. 在我的所有其他程序完成工作后,将该文件复制并替换为原始文件夹。

  4. 你有任何可能有用的脚本吗?

2 个答案:

答案 0 :(得分:1)

您可以从这个批处理代码开始:

@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=
::******************************************************************************

编辑:2015年6月28日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 

答案 1 :(得分:0)

dir c:\nameoffiletosearch /s /b > %temp%\results.txt将创建一个列表,其中包含驱动器c中文件的所有出现的完整路径。

你甚至可以将它包装在一个搜索多个驱动器的循环中。

然后从results.txt获取路径并将其与复制命令一起使用。