简体   繁体   English

批处理文件的MID功能

[英]MID function for Batch file

I'm back with a batch file question. 我回来了一个批处理文件问题。 I asked this question about getting ranges in a batch file so we can more easily bring over backups. 我问一个关于在批处理文件中获取范围的问题,以便我们可以更轻松地完成备份。 Once the file has been copied to our system, we unzip the file so we have a "running backup" of our client's data. 将文件复制到我们的系统后,我们解压缩文件,以便我们对客户端数据进行“运行备份”。 What I'm trying to accomplish now is to move the file that was just been unzipped from its current directory to an archive directory. 我现在要完成的是将刚从当前目录解压缩的文件移动到存档目录。 So far I have the following: 到目前为止,我有以下内容:

@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

This is fine, but the AlreadyUnzipped folder contains sub-folders labeled by the year. 这很好,但AlreadyUnzipped文件夹包含由年份标记的子文件夹。 The year folders have sub-folders labeled by month. 年份文件夹包含按月标记的子文件夹。

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

These folders contain everything that has been unzipped. 这些文件夹包含已解压缩的所有内容。 What I would like to happen is once the unzipping process has been completed, I would like the file to be moved to it's corresponding archive folder. 我想要发生的是,一旦解压缩过程完成,我希望将文件移动到它的相应存档文件夹。 An example would be: 一个例子是:

daily20140105.zipx

needs to go to 需要去

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

I've done some research online and found this website, but have been unable to get anything to happen. 我在网上做了一些研究并找到了这个网站,但一直无法发生任何事情。 I've been doing the MID equivalent. 我一直在做同等的MID。 If I need to further explain myself, then I will gladly do so. 如果我需要进一步解释自己,那么我很乐意这样做。 Thanks in advance! 提前致谢!

UPDATE UPDATE

Much thanks to @David Ruhmann for all his help! 非常感谢@David Ruhmann的所有帮助! His got me to this point 他让我到了这一步

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

When I debug it, the following is echoed it out. 当我调试它时,以下内容得到了回应。

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

The paths are correct, but it doesn't do anything! 路径是正确的,但它没有做任何事情! It only extracts the file, then says the system can't find the path and moves to the next file to be extracted. 它只提取文件,然后说系统找不到路径并移动到下一个要提取的文件。 GRRRR! GRRRR!

FINISHED: 表面处理:

I'm so very grateful to @DavidRuhmann and @Magoo. 我非常感谢@DavidRuhmann和@Magoo。 Without their help and keen insight, I would be stuck spinning my wheels in vein pursuit of an answer. 如果没有他们的帮助和敏锐的洞察力,我会坚持不停地寻找答案。 I've used both of their input for my final program which is what follows: 我已将他们的两个输入用于我的最终程序,如下所示:

@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

If you're still reading this, go give these guys some love by up-voting their contributions. 如果你还在读这篇文章的话,那就去投票给这些家伙吧。 Thank you again! 再次感谢你!

Something like this should do what you requested. 这样的事情应该按照你的要求做。

@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

Solely addressing the error now encountered - and this is purely "air code" (theoretical and untested) 仅仅解决了现在遇到的错误 - 这纯粹是“空气代码”(理论和未经测试)

(noting also that the for has an unclosed parenthesis - probably a cut-and-paste glitch) (还注意到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\
)

The required MD commands are merely ECHO ed for testing purposes. 所需的MD命令只是ECHO编用于测试目的。 After you've verified that the commands are correct , change ECHO(MD to MD to actually create the directories. Append 2>nul to suppress error messages (eg. when the directory already exists) 在验证命令正确后 ,更改ECHO(MDMD以实际创建目录。附加2>nul以禁止错误消息(例如,当目录已存在时)

The required MOVE commands are merely ECHO ed for testing purposes. 所需的MOVE命令仅仅ECHO编用于测试目的。 After you've verified that the commands are correct , change ECHO(MOVE to MOVE to actually move the files. Append >nul to suppress report messages (eg. 1 file moved ) 在确认命令正确后 ,更改ECHO(MOVE to MOVE以实际移动文件。附加>nul以抑制报告消息(例如, 1 file moved


further thoughts: 进一步思考:

Certainly, the expansion is faulty. 当然,扩张是错误的。

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

should work. 应该管用。 It does for me. 它对我有用。

Meanwhile - the two echo ed lines: 同时 - 两条echo线:

The extra MD of an existing directory will do no harm. 现有目录的额外MD不会造成任何伤害。 Call it a safety measure if you like; 如果你愿意,可以称之为安全措施; omit it if you like. 如果你愿意,可以省略它。 As I documented, if you remove the echo( and append 2>nul then the error message generated for an existing directory will be suppressed. 正如我记录的那样,如果你删除了echo(并追加2>nul那么将禁止为现有目录生成的错误消息。

Finally, I believe that the cause of the 'can't find the file' message may be the terminal \\ in the move command - I was under the impression that the destination was a directory specification, not a file specification, so that terminal \\ should be omitted. 最后,我相信'无法找到文件'消息的原因可能是move命令中的终端\\ - 我的印象是目的地是目录规范,而不是文件规范,所以终端\\应该省略。

Revised batch segment: 修订后的批次细分:

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
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM