在批处理脚本中循环重命名和移动文件
因此,我希望能够将一个或多个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这将更多地是关于如何在“咳嗽”和“咳嗽”糟糕的批处理编程语言中进行相当好的编程的一课。
第一阶段是从外向内或自上而下工作。构建这个框架只花了几分钟。子目标/步骤是:
%1 获取第一个参数,而 SHIFT 将移动所有参数,以便 %1=%2、%2=%3、%3=%4、%4=%5 等...这有效地删除了第一个参数并将参数总数减少 1。当 SHIFT 删除所有参数并且 %1 不再具有值时,IF 语句退出脚本。
另外,很多人会在 ECHO 命令后加上句点(ECHO.Text to echo),但我遇到了一个不好的情况,即 period 打印了一个空行而不是文本,而是分号;很好 - 所以我没有这样做,期间!
在第二阶段,我们首先通过删除不需要的回显来清理代码。
开始在 <> 中进行实验区域。
通过创建 5 个文件夹(每个文件夹中包含一个 zip 文件)来测试代码,然后将文件夹拖到批处理文件上,看看我们是否喜欢它构建的命令。
然后我们尝试构建提取命令:
检查结果是否正确:
在第 3 阶段,我们进行实际尝试 - 删除回声:
检查我们是否喜欢结果(您可以看到我正在使用一些旧的 latitude-e5440 PDF)在某处被遗忘的文件夹中找到):
对于第 4 阶段,我们回显 extract 命令和 xcopy 命令以查看它们的样子:
只是提醒一下,每个测试都是通过将 5 个文件夹拖到批处理文件中来完成的:
第 5 阶段,尝试它活着:
什么我们得到了吗?:
第 6 阶段,删除目录会是什么样子? (决定用 REM 隐藏其他命令)
我们有:
第 7 阶段是最终代码和测试:
完成后所有文件夹都被删除。
现在,让我们做一些实验。我从来没有见过这个失败,但我也没有经常使用它。让我们尝试一下:
如果有任何用 && 分隔的命令;失败,则执行跳转到以 || 为前缀的命令。但是如果所有命令都用 && 分隔成功且不抛出错误代码,则命令以 || 为前缀被跳过。理论上,这将防止文件夹被删除,并在出现问题时暂停代码。
因此,我们重新创建了文件夹,然后使用HxD修复了volume4文件夹中的损坏zip文件。
效果非常好!文件夹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:
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!
In stage 2 we start by cleaning up the code by removing the unneeded echos.
Start experimenting in the <<New Code Will Go Here>> area.
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.
Then we try to build the extraction commands:
Check if results look correct:
In stage 3 we try it for real - remove the echo:
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):
For stage 4 we echo both the extract command and xcopy command to see what they look like:
Just a reminder, every test is done by dragging the 5 folders onto the batch file:
Stage 5, try it live:
What did we get?:
Stage 6, what will remove directory look like? (Decided to hide the other commands with REM)
And we have:
Stage 7 is the final code and tests:
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:
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.
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! ;)