Win7 7/64位/批/初学者
总之,我试着
我查找了-循环和xcopy之类的东西,但是我对批处理脚本完全陌生,所以这就是我到目前为止得到的结果:
@echo off
::I found this to be useful for variables in for-loops
Setlocal EnableDelayedExpansion
set odrive=%odrive:~0,2%
set backupcmd=xcopy /S /C /G /D /E /I /R /Y
::this part is meant to find all available drives
for %%i in (D E F G H I J) do if exist %%i: (
::and store the drive name
set target=!target!%%i:\
echo "!target!"::now I want to find a certain folder and use its filepath
for /d /r "!target!" %%a in (*) do if /i "%%~nxa"=="foldername"
set folderpath=!folderpath!%%a ::I want to use this just like I did earlier
echo "!folderpath!" ::but it turns out to be empty
::the last step is to use the folderpath for the backupcmd
%backupcmd% "!folderpath!" "%drive%\backedUpFiles"::reset variables
set "target="
set "folderpath="
)不知道这些是否是“好的”批处理脚本,但到目前为止,我可以理解它。我希望这个错误在字里行间(字面意思)之间,也许它只是与我使用变量的方式有关。错误是关于“无效的驱动器名”。
我希望有人能帮我,因为我真的在几天后完成了搜索代码。
发布于 2015-05-03 13:35:07
试试这个:
@echo off
Setlocal EnableDelayedExpansion
set searchitem=level 3
set target=%drive%\backedUpFiles
set backupcmd=xcopy /S /C /G /D /E /I /R /Y
set backupcmd=echo
for %%X in (C D E F G H I J) do if exist %%X:\ (
if exist "%%X:\%searchitem%\" %backupcmd% "%%X:\%searchitem%\" "%target%"
for /f "delims=" %%D in ('dir /b/s/ad "%%X:\%searchitem%" 2^>nul') do %backupcmd% "%%D" "%target%"
)作为示例,我将文件夹名设置为“第3级”,以演示带有嵌入空格的文件夹名。在循环中,我们必须分别处理第一级子文件夹( dir的一个怪癖,不是列出文件夹名称,而是列出它的内容)。我发现dir比for /R循环要快得多,反正我也不能在没有错误的情况下运行它。
https://stackoverflow.com/questions/30011584
复制相似问题