7z“不正确的命令行"当我在与 C 不同的驱动器中运行脚本时: [英] 7z "Incorrect command line" when I run the script in a different drive than C:

查看:13
本文介绍了7z“不正确的命令行"当我在与 C 不同的驱动器中运行脚本时:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在执行批处理脚本,以便可以在特定文件夹中压缩多个目录.

问题是,如果文件夹位于 C:something,它可以完美运行,但如果我尝试对 E:something 中的文件夹执行相同操作,则会出现错误.

脚本是这样的:

@ECHO 关闭如果 %1.==.(SET "rootpath=%cd%") 别的 (设置根路径=%~1")FOR/D %%D IN ("%rootpath%*") 做 (7za a -t7z %%D.7z %%D* -mx9)

正常工作方式示例:

C:UsersMeDesktopExampleFolder>脚本7-Zip (A) 9.20 版权所有 (c) 1999-2010 Igor Pavlov 2010-11-18扫描创建存档 C:UsersMeDesktopExampleFolderD1.7z一切都好7-Zip (A) 9.20 版权所有 (c) 1999-2010 Igor Pavlov 2010-11-18扫描创建存档 C:UsersMeDesktopExampleFolderD2.7z一切都好

失败的例子:

C:UsersMeExampleFolder> 脚本E:DocumentsExampleFolder"错误:不正确的命令行错误:不正确的命令行

当然,我也尝试在同一个文件夹中执行脚本,并且当我将位置作为参数传递时,我检查了它是否有效.谢谢.

解决方案

批号有2处错误.

第一个是不确保分配给 rootpath 的路径不以反斜杠结尾,导致 %%D 包含带有两个连续反斜杠的路径.>

由于这种编码遗漏错误导致路径中的两个连续反斜杠取决于调用批处理时的参数字符串或从驱动器根目录执行批处理文件时,因为在这种情况下%cd%扩展为由驱动器号、冒号和反斜杠组成的路径.%cd% 如果当前目录不是驱动器的根目录,则扩展为末尾没有反斜杠的路径.然而,这个错误并不严重.

第二个是使用包含关键字符(如空格)或以下字符之一的目录路径的真正问题:&()[]{}^=;!'+,`~

%%D.7z%%D* 没有用双引号括起来,使得 7za.exe 的参数列表无效尤其是路径中有 1 个或多个空格.

我是怎么发现的?

我在 7za 的左侧插入了命令 echo 以查看将在循环中执行的内容.我没有安装 7-Zip,因此需要 echo 来测试批处理文件.

解决办法:

@echo off如果 "%~1" == "" (设置RootPath=%CD%") 别的 (设置RootPath=%~1")如果 "%RootPath:~-1%" == "" 设置 "RootPath=%RootPath:~0,-1%"对于/D %%D in ("%RootPath%*") 做 (7za.exe a -t7z "%%D.7z" "%%D*" -mx9)

如果该批处理文件总是在知道 7za.exe 路径的同一台计算机上执行,最好指定 7za.exe 并包含完整路径批处理文件中的双引号.

要了解使用的命令及其工作原理,请打开命令提示符窗口,在那里执行以下命令,并仔细阅读为每个命令显示的所有帮助页面.

  • cmd/? ... 至少阅读最后一个输出帮助页面的最后一段.
  • echo/?
  • for/?
  • if/?
  • set/?

I'm doing a batch script so I can compress multiple directories inside a specific folder.

The thing is it works perfectly if the folder is at C:something, but if I try to do the same to a folder in i.e E:something, I get an error.

The script is this:

@ECHO OFF
if %1.==. (
    SET "rootpath=%cd%"
) else (
    SET "rootpath=%~1"
)
FOR /D %%D IN ("%rootpath%*") DO (
  7za a -t7z %%D.7z %%D* -mx9
)

Example of how it works normally:

C:UsersMeDesktopExampleFolder>script
7-Zip (A) 9.20  Copyright (c) 1999-2010 Igor Pavlov  2010-11-18
Scanning
Creating archive C:UsersMeDesktopExampleFolderD1.7z
Everything is Ok
7-Zip (A) 9.20  Copyright (c) 1999-2010 Igor Pavlov  2010-11-18
Scanning
Creating archive C:UsersMeDesktopExampleFolderD2.7z
Everything is Ok

Example of how it fails:

C:UsersMeExampleFolder>script "E:DocumentsExampleFolder"
Error:
Incorrect command line
Error:
Incorrect command line

Of course, I also tried to execute the script in the same folder and I checked that it works when I pass the location as an argument. Thanks.

解决方案

There are 2 mistakes in the batch code.

The first one is not making sure that path assigned to rootpath does not end with a backslash resulting in %%D containing a path with two successive backslashes.

Two successive backslashes in path caused by this coding omission mistake depends on parameter string on calling the batch or when the batch file is executed from root of a drive because in this case %cd% expands to a path consisting of drive letter, a colon and a backslash. %cd% expands to a path without a backslash at end if current directory is not root directory of a drive. However, this mistake is not critical.

The second one is the real problem on using a directory path containing a critical character like a space or one of these characters: &()[]{}^=;!'+,`~

%%D.7z and %%D* are not enclosed in double quotes making the parameters list for 7za.exe invalid especially with 1 or more spaces in path.

How I found that out?

I inserted command echo left of 7za to see what would be executed in the loop. I don't have 7-Zip installed and therefore needed the echo to test the batch file.

The solution:

@echo off
if "%~1" == "" (
    set "RootPath=%CD%"
) else (
    set "RootPath=%~1"
)
if "%RootPath:~-1%" == "" set "RootPath=%RootPath:~0,-1%"

for /D %%D in ("%RootPath%*") do (
    7za.exe a -t7z "%%D.7z" "%%D*" -mx9
)

If this batch file is executed on always the same computer where path to 7za.exe is known, it would be good to specify 7za.exe with full path enclosed in double quotes in the batch file.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • cmd /? ... read at least the last paragraph on last output help page.
  • echo /?
  • for /?
  • if /?
  • set /?

这篇关于7z“不正确的命令行"当我在与 C 不同的驱动器中运行脚本时:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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