请原谅我对.bat文件的无知,但我并不习惯他们。 我想要做的是获取一个.bat文件来搜索特定的文件名,然后搜索该.txt文件以找到该文件的特定所有者。如果全部匹配,则将该文件移至另一个目录。
这可能,我怎么开始? 我熟悉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 :(得分:0)
我从头开始提供一个我认为符合您要求的脚本,所以这里是:
<footer>my disclamer... </footer>
有许多解释性说明。核心命令是@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
,因为这将无法识别。
请注意,如果给定的登录名包含空格,脚本将失败。