用winrar用批处理脚本 [英] Using WINRAR with batch script

查看:150
本文介绍了用winrar用批处理脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的脚本,可以请一些帮助我,

I am new to scripting, can some please assist me,

我有批处理文件,


  1. 在文件名前8个字符看起来,创建并
    移动这些文件到新的文件夹与前8个字符作为文件夹
    名称。

  2. 然后看着在步骤1中创建明年四大系列文件夹
    字符(9,10,11,12),并创建和移动到另一个子文件夹
    旁边4个字符的文件夹名称。

  3. 然后看着在步骤2中创建的文件夹,每个文件的扩展名,创建和移动
    与扩展名文件夹名称的新文件夹。

例如,我有一个看起来像这样的文件

For example, I have files that look like this

ABCEFGHI0703xyz.pdf
STUVWXYZ0805xyz.pptx

移至文件夹

ABCEFGHI\0703\PDF
STUVWXYZ\0805\PPTX

在牢记前8个字符是随机的,接下来的4个字符的年份和月份,9个类型的扩展。

Keeping in mind first 8 characters are random, next 4 character are year and month, and 9 types of extensions.

我使用这个批处理脚本来创建这些文件夹: -

I am using this batch script to create these folders:-

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=C:\sourcedir"
SET "destdir=C:\sourcedir"
FOR /f "delims=" %%a IN (
  'dir /b /a-d "%sourcedir%\*" '
 ) DO (
 SET name=%%~na
 SET ext=%%~xa
 SET name=!name:~0,8!\!name:~8,4!\!ext:~1!
 MD "!name!" 2>nul
 MOVE "%sourcedir%\%%a" "!name!\" >nul
)
GOTO :EOF

现在我想一个WINRAR命令添加到压缩文件只是在步骤3中创建的文件夹的扩展,我使用这个命令来创建档案。

Now I would like to add a WINRAR command to archive just the extension folders created in step 3, I am using this command to create the archives.

C:\ ABCEFGHI\0703\PDF>WINRAR A PDF C:\ ABCEFGHI\0703\PDF

是否有可能这个命令添加到脚本?

Is it possible to add this command to the script?

推荐答案

好吧首先你需要有一个RAR.EXE文件夹中的%PATH%,

Ok first you need to have rar.exe in a folder in %PATH%,

我建议你把一个链接在您的Windows \\ System32文件夹中,像这样:

i'd suggest you put a link in your Windows\System32 folder like so:

mklink C:\Windows\System32\rar.exe "C:\Program Files\WinRAR\rar.exe"

然后就可以开始工作。

then you can get to work.

当你已经建议,首先创建所需的目录树,然后只将所需的文件添加到您的存档,像这样:

As you already suggested, first create the desired directory tree and then just add the required files to your archive like so:

rar.exe a %ARCHIVE_NAME% MainFolder\*.pdf
rar.exe a %ARCHIVE_NAME% MainFolder\FolderA\*
rar.exe a %ARCHIVE_NAME% MainFolder\FolderB\*

而ARCHIVE_NAME%%的的文件名,你的新的的目标存档(如foo.rar)

Whereas %ARCHIVE_NAME% is the file name of your new target archive (such as foo.rar)

这会在每次*在MainFolder,一切都在FolderA'和'FolderB中.pdf文件。目录树的将是preserved

This will every *.pdf file in 'MainFolder' and everything in 'FolderA' and 'FolderB'. The directory tree will be preserved.

此外,您可能要检查是否ARCHIVE_NAME%%的已存在后,因为RAR只会指定的文件添加到现有存档文件(可能会覆盖他们)

Also, you may want to check whether %ARCHIVE_NAME% already exists, since rar will just add the specified files to an existing archive (possibly overriding them)

希望这澄清了一些事情给你。

Hope this clarifies some things for you.

编辑:这样做recursivly未知根目录

set ARCHIVE_NAME=%CD%\pdf_archive.rar
for /r %CD% %%d in ('PDF') do (
    if exist "%%d" (
        echo Archiving files in: %%d
        rar a "%ARCHIVE_NAME% "%%d"\*
    )
)

现在这个将进入每一个子目录recursivly(从当前目录开始)

Now this will go into every subdirectory recursivly (starting from your current directory)

然后IW将寻找所谓的PDF文件夹以及如果存在的话,将存档的每个文件夹中到%ARCHIVE_NAME%

Then iw will look for folders called 'PDF' and if they exist it will archive every file in that folder to %ARCHIVE_NAME%

这篇关于用winrar用批处理脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆