2016-11-08 95 views
0

请原谅我对.bat文件的无知,但我不习惯它们。 我想要做的是获得一个.bat文件来搜索特定的文件名,然后执行该.txt文件的搜索以找到该文件的特定所有者。如果全部匹配,则将该文件移到另一个目录。.Bat文件找到一个特定的文件名和所有者并移动

这是可能的,我怎么开始? 我很熟悉使用JS编程,但仅此而已。 以前从未创建批处理文件,但听到他们所有的时间奇迹。

*****这是我迄今为止收集的...并且无法使其工作。您还会注意到,我不知道如何告诉它搜索该文件的特定所有者...即(Jane Doe)和(测试)的文件名,如果全部匹配,则移动到另一个目录*** **

@echo OFF 
setlocal enableextensions disabledelayedexpansion 

set "source=C:\Users\andrew.moss\Desktop\Test1" 
set "target=C:\Users\andrew.moss\Desktop\Test2" 
set "searchString=Testing" 

set "found=" 
for /f "delims=" %%a in (' 
    findstr /m /i /l /c:"%Testing%" "%C:\Users\andrew.moss\Desktop\Test1%" 2^>nul 
') do (
    if not defined found set "found=1" 
    echo move "%%a" "%C:\Users\andrew.moss\Desktop\Test2%" 
) 

if not defined found (
    echo Failure 
) 

pause 
+0

这不是教程网站,所以你应该分享你的努力。一个好的起点是['dir'](http://ss64.com/nt/dir.html)命令 - 在新的命令提示符窗口中键入'help dir'或'dir /?'并阅读帮助非常仔细... – aschipfl

+0

当我运行我迄今为止所有我得到的是'失败' – Drew

+0

你在'%%'中包含了两个文件路径,但这些文件应该被删除...要获得所有者,你可以解析通过'for/F'循环输出'dir/Q',但这可能会有点棘手......不知道是否存在依赖'wmic'的方法... – aschipfl

回答

0

我从头开始提供的脚本,我相信适合您的需求,所以这里是:

@echo off 
setlocal EnableExtensions DisableDelayedExpansion 

rem // Define constants here: 
set "_SOURCE=."  & rem // (path to directory where to search for files) 
set "_TARGET=.."  & rem // (path to directory where to move found files) 
set "_PATTERN=test.*" & rem // (file name pattern; may contain wild-cards) 
set "$RECURSIVE=#" & rem // (set to non-empty value to search sub-dir.s) 
set "$OWNER=aschipfl" & rem /* (login name of owner; may be preceded with 
         rem  domain and a backslash, like `DOMAIN\`) */ 

rem // Predefine recursive option: 
if defined $RECURSIVE set "$RECURSIVE=/S" 
rem // Split domain off of owner: 
for /F "tokens=1-2 delims=\ eol=\ " %%C in ("%$OWNER%") do (
    if not "%%D"=="" (
     set "$DOMAIN=%%C" 
     set "$OWNER=%%D" 
    ) else set "$DOMAIN=" 
) 

rem /* Loop through all matching files in the given location found by `dir`; 
rem the `/Q` option lets include domain/owner in the output; `findstr` 
rem filters out lines beginning with ` `, hence headers and footers: */ 
pushd "%_SOURCE%" || exit /B 1 
for /F "tokens=4,*" %%E in (' 
    dir /Q /N /-C %$RECURSIVE% /A:-D "%_PATTERN%"^
     ^| findstr /V /B /C:" " 
') do (
    rem // Reset flag: 
    set "FLAG=" 
    rem // Check whether certain domain has been specified: 
    if defined $DOMAIN (
     rem // Domain specified, so domain and owner must match: 
     if /I "%%E"=="%$DOMAIN%\%$OWNER%" (
      rem // Match encountered, hence set flag: 
      set "FLAG=#" 
     ) 
    ) else (
     rem // Domain not specified, so owner must match only: 
     set "OWN=%%E" 
     setlocal EnableDelayedExpansion 
     rem // Dismiss domain of currently iterated file item: 
     set "OWN=!OWN:*\=!" 
     rem // Check whether owner matches: 
     if "!OWN!"=="%$OWNER%" (
      endlocal 
      rem // Match encountered, hence set flag: 
      set "FLAG=#" 
     ) else endlocal 
    ) 
    rem // Check whether flag is set: 
    if defined FLAG (
     rem /* Flag is set, hence do something with current file item; 
     rem here, the file is moved to the given target directory; 
     rem remove the `if exist` query to force overwriting: */ 
     if not exist "%_TARGET%\%%~nxF" (
      > nul move /Y "%%~fF" "%_TARGET%\" 
     ) 
    ) 
    endlocal 
) 
popd 

endlocal 
exit /B 

有很多解释性发言。核心命令是dir /Q,它能够返回文件的所有者。请注意,它会返回它们的登录名,前面带有域名和一个反斜杠,例如DOMAIN\jdoe。因此,您需要在脚本中以这种方式指定拥有者。您可以省略域前缀,在这种情况下,该域将被忽略。但是,例如,您不能指定显示用户名称,例如Jane Doe,因为这不会被识别。

请注意,如果给定的登录名包含空格,脚本将会失败。

+0

请注意,初始[修订1](http://stackoverflow.com/revisions/40505600/1)不起作用,因为它是一个错误; [修订2](http://stackoverflow.com/revisions/40505600/2)可以工作,但不会移动任何文件,只是为了演示而回应它们; [修订3](http://stackoverflow.com/revisions/40505600/3)终于移动找到的文件...我建议关闭/删除[其他问题](http://stackoverflow.com/q/40516634 )与此脚本的初始修订... – aschipfl

+0

非常感谢您以您的方式提供帮助。我非常感谢。 我关闭了另一个问题。 我试过修订3,它现在告诉我“系统找不到指定的文件” – Drew

+0

让我们[在聊天中继续讨论](http://chat.stackoverflow.com/rooms/127738/discussion-between-drew-和aschipfl)。 – aschipfl

相关问题