在批处理脚本中循环重命名和移动文件

发布于 2025-01-11 10:02:53 字数 567 浏览 0 评论 0原文

因此,我希望能够将一个或多个 rar 或 zip 文件拖到批处理 .cmd 脚本中,该脚本使用 7zip 将文件提取到上一级目录,将提取的文件重命名为当前文件夹名称,然后使用rar 或 zip 档案。

到目前为止,我已经:

set work=%temp%\%random%%random%%random%%random%
mkdir "%work%" || goto :eof

for %%A in (*.zip *.rar) do (
    "%ProgramFiles%\7-Zip\7z.exe" e -o"%work%" "%%~A"
    for %%F in ("%work%\*") do move "%%~F" "%%~nA%%~xF"
)

rmdir "%work%"

将文件提取到当前目录并将其重命名为存档名称,但我想将其命名为存档所在的目录,因此 %%~nA 需要更改,但键入“for /?”似乎没有给我一个获取当前目录的选项。然后我需要将提取的文件向上移动一级并删除包含存档文件的文件夹,我只是不知道在循环中使用 move 和 rmdir 时如何引用文件。

这是我第一次尝试批处理脚本,请放轻松。

So I want to be able to drag one or more rar or zip files to a batch .cmd script that extracts the file with 7zip to a directory one level up, renames the extracted file to the current folder name and then deletes the folder with the rar or zip archives.

So far I have:

set work=%temp%\%random%%random%%random%%random%
mkdir "%work%" || goto :eof

for %%A in (*.zip *.rar) do (
    "%ProgramFiles%\7-Zip\7z.exe" e -o"%work%" "%%~A"
    for %%F in ("%work%\*") do move "%%~F" "%%~nA%%~xF"
)

rmdir "%work%"

And that extracts the file to the current directory and renames it to the archive name but I want to name it to the directory the archive is in so %%~nA needs to be changed but typing "for /?" doesn't seem to give me an option to get the current directory. Then I need to move the extracted file one level up and delete the folder with the archive files, I just don't know how to reference the files when using move and rmdir when in a loop.

This is my first attempt at batch scripting please go easy on me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

水溶 2025-01-18 10:02:53

这将更多地是关于如何在“咳嗽”和“咳嗽”糟糕的批处理编程语言中进行相当好的编程的一课。

第一阶段是从外向内或自上而下工作。构建这个框架只花了几分钟。子目标/步骤是:

  1. 简单地循环所有参数,看看我们是否可以将它们保存到变量中,然后将它们回显。
  2. 弄清楚如何提取 SubFolderName(这将是文件名),并在每个循环中回显它。
  3. 构建 Extract 文件夹并根据每个循环进行 echo。
  4. 意识到您忘记了需要调用批处理文件的文件夹路径,将其添加为第一个参数,并在进入 :ltpLoop 之前提取它。

%1 获取第一个参数,而 SHIFT 将移动所有参数,以便 %1=%2、%2=%3、%3=%4、%4=%5 等...这有效地删除了第一个参数并将参数总数减少 1。当 SHIFT 删除所有参数并且 %1 不再具有值时,IF 语句退出脚本。

另外,很多人会在 ECHO 命令后加上句点(ECHO.Text to echo),但我遇到了一个不好的情况,即 period 打印了一个空行而不是文本,而是分号;很好 - 所以我没有这样做,期间!

@ECHO OFF
    GOTO :Start

:LoopThroughParameters
    SET CmdPath=%~1
    SHIFT
    ECHO;[[[%CmdPath%]]]
:ltpLoop
    IF [%1] EQU [] GOTO :EOF
    SET Folder=%~1
    SET SubFolderName=%~nx1
    SET ExtractFolder=%~1\Extract

    ECHO;
    ECHO;[%Folder%]
    ECHO;[%SubFolderName%]
    ECHO;[%ExtractFolder%]
    SHIFT
    GOTO :ltpLoop

:Start
    CALL :LoopThroughParameters "%~dp0" %*
PAUSE

在第二阶段,我们首先通过删除不需要的回显来清理代码。

:LoopThroughParameters
    SET CmdPath=%~1
    SHIFT

:ltpLoop
    IF [%1] EQU [] GOTO :EOF
    SET Folder=%~1
    SET SubFolderName=%~nx1
    SET ExtractFolder=%~1\Extract
    <<New Code Will Go Here>>
    SHIFT
    GOTO :ltpLoop

开始在 <> 中进行实验区域。

    ECHO;FOR %%C IN ("%Folder%\*.zip" "%Folder%\*.rar") DO (

通过创建 5 个文件夹(每个文件夹中包含一个 zip 文件)来测试代码,然后将文件夹拖到批处理文件上,看看我们是否喜欢它构建的命令。

FOR %C IN ("D:\Temp\StackOverflow\71345433\volume5\*.zip" "D:\Temp\StackOverflow\71345433\volume5\*.rar") DO (
FOR %C IN ("D:\Temp\StackOverflow\71345433\volume1\*.zip" "D:\Temp\StackOverflow\71345433\volume1\*.rar") DO (
FOR %C IN ("D:\Temp\StackOverflow\71345433\volume2\*.zip" "D:\Temp\StackOverflow\71345433\volume2\*.rar") DO (
FOR %C IN ("D:\Temp\StackOverflow\71345433\volume3\*.zip" "D:\Temp\StackOverflow\71345433\volume3\*.rar") DO (
FOR %C IN ("D:\Temp\StackOverflow\71345433\volume4\*.zip" "D:\Temp\StackOverflow\71345433\volume4\*.rar") DO (

然后我们尝试构建提取命令:

    FOR %%C IN ("%Folder%\*.zip" "%Folder%\*.rar") DO (
        ECHO;"%ProgramFiles%\7-Zip\7z.exe" e -o"%ExtractFolder%" "%%~C"
    )

检查结果是否正确:

"C:\Program Files\7-Zip\7z.exe" e -o"D:\Temp\StackOverflow\71345433\volume5\Extract" "D:\Temp\StackOverflow\71345433\volume5\Last Zip file.zip"
"C:\Program Files\7-Zip\7z.exe" e -o"D:\Temp\StackOverflow\71345433\volume1\Extract" "D:\Temp\StackOverflow\71345433\volume1\SomeZipFile.zip"
"C:\Program Files\7-Zip\7z.exe" e -o"D:\Temp\StackOverflow\71345433\volume2\Extract" "D:\Temp\StackOverflow\71345433\volume2\JustAnotherZip.zip"
"C:\Program Files\7-Zip\7z.exe" e -o"D:\Temp\StackOverflow\71345433\volume3\Extract" "D:\Temp\StackOverflow\71345433\volume3\YetAnotherZip.zip"
"C:\Program Files\7-Zip\7z.exe" e -o"D:\Temp\StackOverflow\71345433\volume4\Extract" "D:\Temp\StackOverflow\71345433\volume4\How Many Zips Was This.zip"

在第 3 阶段,我们进行实际尝试 - 删除回声:

"%ProgramFiles%\7-Zip\7z.exe" e -o"%ExtractFolder%" "%%~C"

检查我们是否喜欢结果(您可以看到我正在使用一些旧的 latitude-e5440 PDF)在某处被遗忘的文件夹中找到):

D:\Temp\StackOverflow\71345433>dir /s /b
D:\Temp\StackOverflow\71345433\DragDropAndExtract.CMD
D:\Temp\StackOverflow\71345433\volume1
D:\Temp\StackOverflow\71345433\volume2
D:\Temp\StackOverflow\71345433\volume3
D:\Temp\StackOverflow\71345433\volume4
D:\Temp\StackOverflow\71345433\volume5
D:\Temp\StackOverflow\71345433\volume1\Extract
D:\Temp\StackOverflow\71345433\volume1\SomeZipFile.zip
D:\Temp\StackOverflow\71345433\volume1\Extract\lat_e_reimage_guide_en-us.pdf
D:\Temp\StackOverflow\71345433\volume2\Extract
D:\Temp\StackOverflow\71345433\volume2\JustAnotherZip.zip
D:\Temp\StackOverflow\71345433\volume2\Extract\latitude-e5440-laptop_owners-manual_en-us.pdf
D:\Temp\StackOverflow\71345433\volume3\Extract
D:\Temp\StackOverflow\71345433\volume3\YetAnotherZip.zip
D:\Temp\StackOverflow\71345433\volume3\Extract\latitude-e5440-laptop_user's guide_en-us.pdf
D:\Temp\StackOverflow\71345433\volume4\Extract
D:\Temp\StackOverflow\71345433\volume4\How Many Zips Was This.zip
D:\Temp\StackOverflow\71345433\volume4\Extract\latitude-e5440-laptop_users-guide_en-us.pdf
D:\Temp\StackOverflow\71345433\volume5\Extract
D:\Temp\StackOverflow\71345433\volume5\Last Zip file.zip
D:\Temp\StackOverflow\71345433\volume5\Extract\latitude-e5440-laptop_white papers_en-us.pdf

对于第 4 阶段,我们回显 extract 命令和 xcopy 命令以查看它们的样子:

        ECHO;"%ProgramFiles%\7-Zip\7z.exe" e -o"%ExtractFolder%" "%%~C"
        ECHO; XCOPY /V /C /Y "%ExtractFolder%\*.*" "%CmdPath%\%SubFolderName%.*"

只是提醒一下,每个测试都是通过将 5 个文件夹拖到批处理文件中来完成的:

"C:\Program Files\7-Zip\7z.exe" e -o"D:\Temp\StackOverflow\71345433\volume5\Extract" "D:\Temp\StackOverflow\71345433\volume5\Last Zip file.zip"
XCOPY /V /C /Y "D:\Temp\StackOverflow\71345433\volume5\Extract\*.*" "D:\Temp\StackOverflow\71345433\\volume5.*"
"C:\Program Files\7-Zip\7z.exe" e -o"D:\Temp\StackOverflow\71345433\volume1\Extract" "D:\Temp\StackOverflow\71345433\volume1\SomeZipFile.zip"
XCOPY /V /C /Y "D:\Temp\StackOverflow\71345433\volume1\Extract\*.*" "D:\Temp\StackOverflow\71345433\\volume1.*"
"C:\Program Files\7-Zip\7z.exe" e -o"D:\Temp\StackOverflow\71345433\volume2\Extract" "D:\Temp\StackOverflow\71345433\volume2\JustAnotherZip.zip"
XCOPY /V /C /Y "D:\Temp\StackOverflow\71345433\volume2\Extract\*.*" "D:\Temp\StackOverflow\71345433\\volume2.*"
"C:\Program Files\7-Zip\7z.exe" e -o"D:\Temp\StackOverflow\71345433\volume3\Extract" "D:\Temp\StackOverflow\71345433\volume3\YetAnotherZip.zip"
XCOPY /V /C /Y "D:\Temp\StackOverflow\71345433\volume3\Extract\*.*" "D:\Temp\StackOverflow\71345433\\volume3.*"
"C:\Program Files\7-Zip\7z.exe" e -o"D:\Temp\StackOverflow\71345433\volume4\Extract" "D:\Temp\StackOverflow\71345433\volume4\How Many Zips Was This.zip"
XCOPY /V /C /Y "D:\Temp\StackOverflow\71345433\volume4\Extract\*.*" "D:\Temp\StackOverflow\71345433\\volume4.*"

第 5 阶段,尝试它活着:

        "%ProgramFiles%\7-Zip\7z.exe" e -o"%ExtractFolder%" "%%~C"
        XCOPY /V /C /Y "%ExtractFolder%\*.*" "%CmdPath%\%SubFolderName%.*"

什么我们得到了吗?:

D:\Temp\StackOverflow\71345433>dir /s/b
D:\Temp\StackOverflow\71345433\volume1
D:\Temp\StackOverflow\71345433\volume1.pdf
D:\Temp\StackOverflow\71345433\volume2
D:\Temp\StackOverflow\71345433\volume2.pdf
D:\Temp\StackOverflow\71345433\volume3
D:\Temp\StackOverflow\71345433\volume3.pdf
D:\Temp\StackOverflow\71345433\volume4
D:\Temp\StackOverflow\71345433\volume4.pdf
D:\Temp\StackOverflow\71345433\volume5
D:\Temp\StackOverflow\71345433\volume5.pdf

第 6 阶段,删除目录会是什么样子? (决定用 REM 隐藏其他命令)

        REM "%ProgramFiles%\7-Zip\7z.exe" e -o"%ExtractFolder%" "%%~C"
        REM XCOPY /V /C /Y "%ExtractFolder%\*.*" "%CmdPath%\%SubFolderName%.*"
        ECHO;RD /Q /S "%Folder%"

我们有:

RD /Q /S "D:\Temp\StackOverflow\71345433\volume5"
RD /Q /S "D:\Temp\StackOverflow\71345433\volume1"
RD /Q /S "D:\Temp\StackOverflow\71345433\volume2"
RD /Q /S "D:\Temp\StackOverflow\71345433\volume3"
RD /Q /S "D:\Temp\StackOverflow\71345433\volume4"

第 7 阶段是最终代码和测试:

@ECHO OFF
    GOTO :Start

:LoopThroughParameters
    SET CmdPath=%~1
    SHIFT

:ltpLoop
    IF [%1] EQU [] GOTO :EOF
    SET Folder=%~1
    SET SubFolderName=%~nx1
    SET ExtractFolder=%~1\Extract
    
    FOR %%C IN ("%Folder%\*.zip" "%Folder%\*.rar") DO (
        "%ProgramFiles%\7-Zip\7z.exe" e -o"%ExtractFolder%" "%%~C"
        XCOPY /V /C /Y "%ExtractFolder%\*.*" "%CmdPath%\%SubFolderName%.*"
        RD /Q /S "%Folder%"
    )

    SHIFT
    GOTO :ltpLoop

:Start
    CALL :LoopThroughParameters "%~dp0" %*
PAUSE

完成后所有文件夹都被删除。

现在,让我们做一些实验。我从来没有见过这个失败,但我也没有经常使用它。让我们尝试一下:

@ECHO OFF
    GOTO :Start

:LoopThroughParameters
    SET CmdPath=%~1
    SHIFT

:ltpLoop
    IF [%1] EQU [] GOTO :EOF
    SET Folder=%~1
    SET SubFolderName=%~nx1
    SET ExtractFolder=%~1\Extract
    
    FOR %%C IN ("%Folder%\*.zip" "%Folder%\*.rar") DO (
        ( 
            "%ProgramFiles%\7-Zip\7z.exe" e -o"%ExtractFolder%" "%%~C" 
        ) && (
            XCOPY /V /C /Y "%ExtractFolder%\*.*" "%CmdPath%\%SubFolderName%.*"
        ) && (
            RD /Q /S "%Folder%"
        ) || (
            PowerShell Write-Host -ForegroundColor red "Error:"
            PAUSE
        )
    )

    SHIFT
    GOTO :ltpLoop

:Start
    CALL :LoopThroughParameters "%~dp0" %*

如果有任何用 && 分隔的命令;失败,则执行跳转到以 || 为前缀的命令。但是如果所有命令都用 && 分隔成功且不抛出错误代码,则命令以 || 为前缀被跳过。理论上,这将防止文件夹被删除,并在出现问题时暂停代码。

因此,我们重新创建了文件夹,然后使用HxD修复了volume4文件夹中的损坏zip文件。
HxD a PDF Bad

效果非常好!文件夹volume4未被删除。再次尝试测试,这次损坏了文件夹volume2中的PDF,完美!

正如您所知,我可以在 Batch 中完成许多在 PowerShell 中无法完成的事情,是的@Olaf,这不一定是值得自豪的事情! ;)

This is going to be more of a lesson on how to do reasonably good programing in *cough* a bad *cough* the batch programming language.

The first stage is to work from the outside inwards, or top down. It only took a few minutes to build this framework. The sub goals/steps are:

  1. Simply loop through all of the parameters and see if we can save them to a variable and then echo them out.
  2. Figure out how to extract the SubFolderName (This will later be the file name), and echo it per each loop.
  3. Build the Extract folder and echo per each loop.
  4. Realize you forgot you were going to need the folder path of the calling batch file, add it as first parameter, and extract it before entering the :ltpLoop.

The %1 gets the first parameter, while SHIFT will shift all parameters so that %1=%2, %2=%3, %3=%4, %4=%5, etc... This effectively removes the first parameter and reduces the total number of parameters by 1. The IF statement exits the script when SHIFT has removed all parameters and %1 no longer has a value.

Also, a lot of people will follow the ECHO command with a period (ECHO.Text to echo), but I had one bad case where period printed a blank line instead of the text, but semicolon ; was fine - so I no do period, period!

@ECHO OFF
    GOTO :Start

:LoopThroughParameters
    SET CmdPath=%~1
    SHIFT
    ECHO;[[[%CmdPath%]]]
:ltpLoop
    IF [%1] EQU [] GOTO :EOF
    SET Folder=%~1
    SET SubFolderName=%~nx1
    SET ExtractFolder=%~1\Extract

    ECHO;
    ECHO;[%Folder%]
    ECHO;[%SubFolderName%]
    ECHO;[%ExtractFolder%]
    SHIFT
    GOTO :ltpLoop

:Start
    CALL :LoopThroughParameters "%~dp0" %*
PAUSE

In stage 2 we start by cleaning up the code by removing the unneeded echos.

:LoopThroughParameters
    SET CmdPath=%~1
    SHIFT

:ltpLoop
    IF [%1] EQU [] GOTO :EOF
    SET Folder=%~1
    SET SubFolderName=%~nx1
    SET ExtractFolder=%~1\Extract
    <<New Code Will Go Here>>
    SHIFT
    GOTO :ltpLoop

Start experimenting in the <<New Code Will Go Here>> area.

    ECHO;FOR %%C IN ("%Folder%\*.zip" "%Folder%\*.rar") DO (

The code is tested by creating 5 folders, with one zip file in each folder, and dragging the folders on to the batch file and seeing if we like the commands it built.

FOR %C IN ("D:\Temp\StackOverflow\71345433\volume5\*.zip" "D:\Temp\StackOverflow\71345433\volume5\*.rar") DO (
FOR %C IN ("D:\Temp\StackOverflow\71345433\volume1\*.zip" "D:\Temp\StackOverflow\71345433\volume1\*.rar") DO (
FOR %C IN ("D:\Temp\StackOverflow\71345433\volume2\*.zip" "D:\Temp\StackOverflow\71345433\volume2\*.rar") DO (
FOR %C IN ("D:\Temp\StackOverflow\71345433\volume3\*.zip" "D:\Temp\StackOverflow\71345433\volume3\*.rar") DO (
FOR %C IN ("D:\Temp\StackOverflow\71345433\volume4\*.zip" "D:\Temp\StackOverflow\71345433\volume4\*.rar") DO (

Then we try to build the extraction commands:

    FOR %%C IN ("%Folder%\*.zip" "%Folder%\*.rar") DO (
        ECHO;"%ProgramFiles%\7-Zip\7z.exe" e -o"%ExtractFolder%" "%%~C"
    )

Check if results look correct:

"C:\Program Files\7-Zip\7z.exe" e -o"D:\Temp\StackOverflow\71345433\volume5\Extract" "D:\Temp\StackOverflow\71345433\volume5\Last Zip file.zip"
"C:\Program Files\7-Zip\7z.exe" e -o"D:\Temp\StackOverflow\71345433\volume1\Extract" "D:\Temp\StackOverflow\71345433\volume1\SomeZipFile.zip"
"C:\Program Files\7-Zip\7z.exe" e -o"D:\Temp\StackOverflow\71345433\volume2\Extract" "D:\Temp\StackOverflow\71345433\volume2\JustAnotherZip.zip"
"C:\Program Files\7-Zip\7z.exe" e -o"D:\Temp\StackOverflow\71345433\volume3\Extract" "D:\Temp\StackOverflow\71345433\volume3\YetAnotherZip.zip"
"C:\Program Files\7-Zip\7z.exe" e -o"D:\Temp\StackOverflow\71345433\volume4\Extract" "D:\Temp\StackOverflow\71345433\volume4\How Many Zips Was This.zip"

In stage 3 we try it for real - remove the echo:

"%ProgramFiles%\7-Zip\7z.exe" e -o"%ExtractFolder%" "%%~C"

Check if we like the results (You can see I'm using some old latitude-e5440 PDFs I happen to find in a forgotten folder someplace):

D:\Temp\StackOverflow\71345433>dir /s /b
D:\Temp\StackOverflow\71345433\DragDropAndExtract.CMD
D:\Temp\StackOverflow\71345433\volume1
D:\Temp\StackOverflow\71345433\volume2
D:\Temp\StackOverflow\71345433\volume3
D:\Temp\StackOverflow\71345433\volume4
D:\Temp\StackOverflow\71345433\volume5
D:\Temp\StackOverflow\71345433\volume1\Extract
D:\Temp\StackOverflow\71345433\volume1\SomeZipFile.zip
D:\Temp\StackOverflow\71345433\volume1\Extract\lat_e_reimage_guide_en-us.pdf
D:\Temp\StackOverflow\71345433\volume2\Extract
D:\Temp\StackOverflow\71345433\volume2\JustAnotherZip.zip
D:\Temp\StackOverflow\71345433\volume2\Extract\latitude-e5440-laptop_owners-manual_en-us.pdf
D:\Temp\StackOverflow\71345433\volume3\Extract
D:\Temp\StackOverflow\71345433\volume3\YetAnotherZip.zip
D:\Temp\StackOverflow\71345433\volume3\Extract\latitude-e5440-laptop_user's guide_en-us.pdf
D:\Temp\StackOverflow\71345433\volume4\Extract
D:\Temp\StackOverflow\71345433\volume4\How Many Zips Was This.zip
D:\Temp\StackOverflow\71345433\volume4\Extract\latitude-e5440-laptop_users-guide_en-us.pdf
D:\Temp\StackOverflow\71345433\volume5\Extract
D:\Temp\StackOverflow\71345433\volume5\Last Zip file.zip
D:\Temp\StackOverflow\71345433\volume5\Extract\latitude-e5440-laptop_white papers_en-us.pdf

For stage 4 we echo both the extract command and xcopy command to see what they look like:

        ECHO;"%ProgramFiles%\7-Zip\7z.exe" e -o"%ExtractFolder%" "%%~C"
        ECHO; XCOPY /V /C /Y "%ExtractFolder%\*.*" "%CmdPath%\%SubFolderName%.*"

Just a reminder, every test is done by dragging the 5 folders onto the batch file:

"C:\Program Files\7-Zip\7z.exe" e -o"D:\Temp\StackOverflow\71345433\volume5\Extract" "D:\Temp\StackOverflow\71345433\volume5\Last Zip file.zip"
XCOPY /V /C /Y "D:\Temp\StackOverflow\71345433\volume5\Extract\*.*" "D:\Temp\StackOverflow\71345433\\volume5.*"
"C:\Program Files\7-Zip\7z.exe" e -o"D:\Temp\StackOverflow\71345433\volume1\Extract" "D:\Temp\StackOverflow\71345433\volume1\SomeZipFile.zip"
XCOPY /V /C /Y "D:\Temp\StackOverflow\71345433\volume1\Extract\*.*" "D:\Temp\StackOverflow\71345433\\volume1.*"
"C:\Program Files\7-Zip\7z.exe" e -o"D:\Temp\StackOverflow\71345433\volume2\Extract" "D:\Temp\StackOverflow\71345433\volume2\JustAnotherZip.zip"
XCOPY /V /C /Y "D:\Temp\StackOverflow\71345433\volume2\Extract\*.*" "D:\Temp\StackOverflow\71345433\\volume2.*"
"C:\Program Files\7-Zip\7z.exe" e -o"D:\Temp\StackOverflow\71345433\volume3\Extract" "D:\Temp\StackOverflow\71345433\volume3\YetAnotherZip.zip"
XCOPY /V /C /Y "D:\Temp\StackOverflow\71345433\volume3\Extract\*.*" "D:\Temp\StackOverflow\71345433\\volume3.*"
"C:\Program Files\7-Zip\7z.exe" e -o"D:\Temp\StackOverflow\71345433\volume4\Extract" "D:\Temp\StackOverflow\71345433\volume4\How Many Zips Was This.zip"
XCOPY /V /C /Y "D:\Temp\StackOverflow\71345433\volume4\Extract\*.*" "D:\Temp\StackOverflow\71345433\\volume4.*"

Stage 5, try it live:

        "%ProgramFiles%\7-Zip\7z.exe" e -o"%ExtractFolder%" "%%~C"
        XCOPY /V /C /Y "%ExtractFolder%\*.*" "%CmdPath%\%SubFolderName%.*"

What did we get?:

D:\Temp\StackOverflow\71345433>dir /s/b
D:\Temp\StackOverflow\71345433\volume1
D:\Temp\StackOverflow\71345433\volume1.pdf
D:\Temp\StackOverflow\71345433\volume2
D:\Temp\StackOverflow\71345433\volume2.pdf
D:\Temp\StackOverflow\71345433\volume3
D:\Temp\StackOverflow\71345433\volume3.pdf
D:\Temp\StackOverflow\71345433\volume4
D:\Temp\StackOverflow\71345433\volume4.pdf
D:\Temp\StackOverflow\71345433\volume5
D:\Temp\StackOverflow\71345433\volume5.pdf

Stage 6, what will remove directory look like? (Decided to hide the other commands with REM)

        REM "%ProgramFiles%\7-Zip\7z.exe" e -o"%ExtractFolder%" "%%~C"
        REM XCOPY /V /C /Y "%ExtractFolder%\*.*" "%CmdPath%\%SubFolderName%.*"
        ECHO;RD /Q /S "%Folder%"

And we have:

RD /Q /S "D:\Temp\StackOverflow\71345433\volume5"
RD /Q /S "D:\Temp\StackOverflow\71345433\volume1"
RD /Q /S "D:\Temp\StackOverflow\71345433\volume2"
RD /Q /S "D:\Temp\StackOverflow\71345433\volume3"
RD /Q /S "D:\Temp\StackOverflow\71345433\volume4"

Stage 7 is the final code and tests:

@ECHO OFF
    GOTO :Start

:LoopThroughParameters
    SET CmdPath=%~1
    SHIFT

:ltpLoop
    IF [%1] EQU [] GOTO :EOF
    SET Folder=%~1
    SET SubFolderName=%~nx1
    SET ExtractFolder=%~1\Extract
    
    FOR %%C IN ("%Folder%\*.zip" "%Folder%\*.rar") DO (
        "%ProgramFiles%\7-Zip\7z.exe" e -o"%ExtractFolder%" "%%~C"
        XCOPY /V /C /Y "%ExtractFolder%\*.*" "%CmdPath%\%SubFolderName%.*"
        RD /Q /S "%Folder%"
    )

    SHIFT
    GOTO :ltpLoop

:Start
    CALL :LoopThroughParameters "%~dp0" %*
PAUSE

All folders were deleted when done.

Now, let's do something experimental. I've never seen this fail, but I haven't used it that often. Let's give it a try:

@ECHO OFF
    GOTO :Start

:LoopThroughParameters
    SET CmdPath=%~1
    SHIFT

:ltpLoop
    IF [%1] EQU [] GOTO :EOF
    SET Folder=%~1
    SET SubFolderName=%~nx1
    SET ExtractFolder=%~1\Extract
    
    FOR %%C IN ("%Folder%\*.zip" "%Folder%\*.rar") DO (
        ( 
            "%ProgramFiles%\7-Zip\7z.exe" e -o"%ExtractFolder%" "%%~C" 
        ) && (
            XCOPY /V /C /Y "%ExtractFolder%\*.*" "%CmdPath%\%SubFolderName%.*"
        ) && (
            RD /Q /S "%Folder%"
        ) || (
            PowerShell Write-Host -ForegroundColor red "Error:"
            PAUSE
        )
    )

    SHIFT
    GOTO :ltpLoop

:Start
    CALL :LoopThroughParameters "%~dp0" %*

If any of the commands separated by && fail, then execution jumps to the command prefixed by ||. But if all of the commands separated by && succeed without throwing an error code, then the command prefixed by || is skipped. In theory, this will prevent the folder from being deleted, and pause the code, if there is problem.

So, we re-created the folders, and then used HxD to the damage zip file in volume4 folder.
HxD making a PDF Bad

It worked perfectly! Folder volume4 was not deleted. Tried the test again, this time damaging the PDF in folder volume2, worked perfectly!

As you can tell, I can do things in Batch that many can't do in PowerShell, and yes @Olaf, that's not necessarily something to be proud of! ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文