批处理文件的MID功能

时间:2014-09-22 19:52:46

标签: windows batch-file batch-processing

我回来了批处理文件问题。我问了this关于在批处理文件中获取范围的问题,以便我们可以更轻松地完成备份。将文件复制到我们的系统后,我们解压缩文件,以便我们对客户端数据进行“运行备份”。我现在要完成的是将刚从当前目录解压缩的文件移动到存档目录。到目前为止,我有以下内容:

@echo off
FOR /F %%i IN (H:\benton_off_site_backup\zipbackups\ZipBackupsList.txt) DO (
c:\wzzip\wzunzip -o -d H:\benton_off_site_backup\zipbackups\%%i h:\benton_off_site_backup\
move H:\benton_off_site_backup\zipbackups\%%i H:\benton_off_site_backup\zipbackups\AlreadyUnzipped
)
exit

这很好,但AlreadyUnzipped文件夹包含以年份标记的子文件夹。年份文件夹包含按月标记的子文件夹。

i.e. H:\benton_off_site_backup\zipbackups\AlreadyUnzipped\2014\1.January
     H:\benton_off_site_backup\zipbackups\AlreadyUnzipped\2014\2.February
     ...

这些文件夹包含已解压缩的所有内容。我想要发生的是,一旦解压缩过程完成,我希望将文件移动到它的相应存档文件夹。一个例子是:

daily20140105.zipx

需要转到

H:\benton_off_site_backup\zipbackups\AlreadyUnzipped\2014\1.January

我在网上做了一些研究并找到了this网站,但一直无法做到。我一直在做同等的MID。如果我需要进一步解释自己,那么我很乐意这样做。提前谢谢!

更新

非常感谢@David Ruhmann的所有帮助!他让我到了这一点

setlocal EnableDelayedExpansion
set "count=0"
for %%A in (January February March April May June July August September October November December) do set /a "count+=1" & set "month[!count!]=%%A"

FOR /F %%i IN (H:\benton_off_site_backup\zipbackups\ZipBackupsList.txt) DO (
c:\wzzip\wzunzip -o -d H:\benton_off_site_backup\zipbackups\%%i h:\benton_off_site_backup\

set "filename=%%~ni"
set "mm=!filename:~-4,2!"
if "!filename:~-4,1!"=="0" set "mm=!filename:~-3,1!"
call move H:\benton_off_site_backup\zipbackups\%%~i H:\benton_off_site_backup\zipbackups\AlreadyUnzipped\!filename:~-8,4!\!mm!.%%month[!mm!]%%\%%i

当我调试它时,以下内容就会反映出来。

move H:\benton_off_site_backup\zipbackups\daily20140920.zipx
H:\benton_off_site_backup\zipbackups\AlreadyUnzipped\2014\9.September\daily20140920.zipx

路径是正确的,但它没有做任何事情!它只提取文件,然后说系统找不到路径并移动到下一个要提取的文件。 GRRRR!

FINISHED:

我非常感谢@DavidRuhmann和@Magoo。如果没有他们的帮助和敏锐的洞察力,我会坚持不停地寻找答案。我已将他们的两个输入用于我的最终节目,如下所示:

@echo off

setlocal EnableDelayedExpansion
set "count=0"
for %%A in (January February March April May June July August September October November December) do set /a "count+=1" & set "month[!count!]=%%A"

FOR /F %%i IN (H:\benton_off_site_backup\zipbackups\ZipBackupsList.txt) DO (
 c:\wzzip\wzunzip -o -d H:\benton_off_site_backup\zipbackups\%%i h:\benton_off_site_backup\

 set "filename=%%~ni"
 set "mm=!filename:~-4,2!"
 if "!filename:~-4,1!"=="0" set "mm=!filename:~-3,1!"
 CALL SET "monthdir=!filename:~-8,4!\!mm!.%%month[!mm!]%%"
 MD H:\benton_off_site_backup\zipbackups\AlreadyUnzipped\!monthdir! 2>NUL
 call move H:\benton_off_site_backup\zipbackups\%%~i H:\benton_off_site_backup\zipbackups\AlreadyUnzipped\!monthdir!\%%i

)
endlocal
exit

如果你还在读这篇文章,那就去投票给这些家伙吧。再次感谢你!

2 个答案:

答案 0 :(得分:2)

这样的事情应该按照你的要求做。

@echo off

setlocal EnableDelayedExpansion
set "count=0"
for %%A in (January February March April May June July August September October November December) do set /a "count+=1" & set "month[!count!]=%%A"

FOR /F %%i IN (H:\benton_off_site_backup\zipbackups\ZipBackupsList.txt) DO (
c:\wzzip\wzunzip -o -d H:\benton_off_site_backup\zipbackups\%%i h:\benton_off_site_backup\

set "filename=%%~ni"
set "mm=!filename:~-4,2!"
if "!filename:~-4,1!"=="0" set "mm=!filename:~-3,1!"
call move "H:\benton_off_site_backup\zipbackups\%%~i" "H:\benton_off_site_backup\zipbackups\AlreadyUnzipped\!filename:~-8,4!\!mm!.%%month[!mm!]%%\%%~i"

)
endlocal
exit

答案 1 :(得分:1)

仅解决现在遇到的错误 - 这纯粹是“空气代码”(理论和未经测试)

(还注意到for有一个未公开的括号 - 可能是一个剪切和粘贴的故障)

FOR /F %%i IN (H:\benton_off_site_backup\zipbackups\ZipBackupsList.txt) DO (
 c:\wzzip\wzunzip -o -d H:\benton_off_site_backup\zipbackups\%%i h:\benton_off_site_backup\

 set "filename=%%~ni"
 set "mm=!filename:~-4,2!"
 if "!filename:~-4,1!"=="0" set "mm=!filename:~-3,1!"
 SET "monthdir=!filename:~-8,4!\!mm!.%month[!mm!]%"
 echo(MD H:\benton_off_site_backup\zipbackups\AlreadyUnzipped\!monthdir!
 ECHO(move H:\benton_off_site_backup\zipbackups\%%~i H:\benton_off_site_backup\zipbackups\AlreadyUnzipped\!monthdir!\%%i\
)

所需的MD命令仅用于ECHO用于测试目的。 在您确认命令正确无法后,将ECHO(MD更改为MD以实际创建目录。附加2>nul以禁止错误消息(例如,当目录已存在时)

为了测试目的,所需的MOVE命令仅为ECHO在您确认命令正确后,将ECHO(MOVE更改为MOVE以实际移动文件。附加>nul以取消报告消息(例如1 file moved


进一步的想法:

当然,扩张是错误的。

 CALL SET "monthdir=!filename:~-8,4!\!mm!.%%month[!mm!]%%"

应该有效。它适合我。

同时 - 两条echo ed行:

现有目录的额外MD不会造成任何伤害。如果你愿意,可以称之为安全措施;如果你愿意,可以省略它。正如我所记录的那样,如果删除echo(并附加2>nul,则会抑制为现有目录生成的错误消息。

最后,我认为“无法找到文件”消息的原因可能是\命令中的终端move - 我的印象是目的地是目录规范,而不是文件规范,因此应省略终端\

修订批次细分:

FOR /F %%i IN (H:\benton_off_site_backup\zipbackups\ZipBackupsList.txt) DO (
 c:\wzzip\wzunzip -o -d H:\benton_off_site_backup\zipbackups\%%i h:\benton_off_site_backup\

 set "filename=%%~ni"
 set "mm=!filename:~-4,2!"
 if "!filename:~-4,1!"=="0" set "mm=!filename:~-3,1!"
 CALL SET "monthdir=!filename:~-8,4!\!mm!.%%month[!mm!]%%"
 echo(MD H:\benton_off_site_backup\zipbackups\AlreadyUnzipped\!monthdir!
 ECHO(move H:\benton_off_site_backup\zipbackups\%%~i H:\benton_off_site_backup\zipbackups\AlreadyUnzipped\!monthdir!\%%i
)