2012-03-29 82 views
0
Setlocal enabledelayedexpansion 
localDirectoryPath=d:/folder/subfolder 
set /p AHFilename=<%fileName% 
set pathname=!localDirectoryPath!\!AHFilename! 
echo pathname=!pathname! 

回显时的路径名,输出为d:/folder/subfolder /fileName,即子文件夹和/之间存在空格,因此路径名的路径变得不可访问。命令提示符上显示的错误是“d:/ folder/subfolder”不存在。从路径中删除空间

请帮忙。如果所述空间被移除,则代码可以正确运行。

+1

我敢肯定,你在这行的后面加上一个空格结果:'localDirectoryPath = d:/文件夹/ subfolder',只是将其删除! – Aacini 2012-03-29 23:48:10

回答

1

你确定你没有这条线后面输入一个空格:`localDirectoryPath = d:/文件夹/子文件夹

我没有批知识的人,但没有你的代码甚至可以运行?我错过了%fileName%变量的设置,我想你在第二行之前缺少一个SET调用,你应该用\在“set pathname”行中替换\。我想......应该不是这样的:?

Setlocal enabledelayedexpansion 
SET localDirectoryPath=d:/folder/subfolder 
set fileName=d:/someFile.txt 
set /p AHFilename=<%fileName% 
set pathname=!localDirectoryPath!\!AHFilename! 
echo pathname=!pathname! 
1

对我的作品好了原样,只是一对夫妇的错别字:

  • - 您需要获得“设置”,使localdirectorypath申报工作。
  • - 您在本地目录和路径名中混合反斜线和正斜杠。

    选择一个。你使用什么文件系统?

  • - 设置AHFilename时使用重定向会给你混合的结果。

改正的代码是为我工作在Windows XP设备上:

Setlocal enabledelayedexpansion 
set localDirectoryPath=c:\folder\subfolder 
set /p AHFilename=%fileName% 
set pathname=!localDirectoryPath!\!AHFilename! 
echo pathname=!pathname! 

如果仍然心不是为你工作,你可以封装的变量和数据报价:

set "localDirectoryPath=c:\folder\subfolder" 

如果这仍然不起作用,可以在使用它的地方剥离localdirectorypath数据中的最后一个字符:

set pathname=!localDirectoryPath:~0,-1!\!AHFilename! 
echo pathname=!pathname! 
1

正如其他人指出的那样,您发布的代码无法运行。它当然不能在subfolder\之间引入空格。

但是,如果您确实有一个变量在文件夹和/或文件名的末尾有不需要的空格,则很容易摆脱它们。

Windows不允许文件或文件夹名称的最后一个字符为点或空格 - 如果尝试创建它,Windows会从名称中去除尾随的点和空格。但是像DIR这样的命令不会忽略尾随空格。

D:\>mkdir "my folder " 

D:\>dir "my folder " 
Volume in drive D has no label. 
Volume Serial Number is F8FD-5039 

Directory of D:\my folder 

File Not Found 

D:\>dir "my folder" 
Volume in drive D has no label. 
Volume Serial Number is F8FD-5039 

Directory of D:\my folder 

03/29/2012 06:00 PM <DIR>   . 
03/29/2012 06:00 PM <DIR>   .. 
       0 File(s)    0 bytes 
       2 Dir(s) 67,054,551,040 bytes free 

您可以使用参数或FOR变量修饰符修剪路径名称中错误的尾点或空格。修饰符会将名称转换为标准格式,包括从路径中的每个文件夹名称中剥离尾随点和/或空格。

@echo off 
set "myVar=my folder " 
echo this will fail because of space at end of path 
dir "%myVar%" 
echo(
echo The ~f modifier strips the trailing space 
for %%F in ("%myVar%") do dir "%%~fF" 

这里是上面的脚本

this will fail because of space at end of path 
Volume in drive D has no label. 
Volume Serial Number is F8FD-5039 

Directory of D:\my folder 

File Not Found 

The ~f modifier strips the trailing space 
Volume in drive D has no label. 
Volume Serial Number is F8FD-5039 

Directory of D:\my folder 

03/29/2012 06:00 PM <DIR>   . 
03/29/2012 06:00 PM <DIR>   .. 
       0 File(s)    0 bytes 
       2 Dir(s) 67,054,551,040 bytes free 
+1

仅供参考:与消息的核心无关 - 可以在最后创建一个带有空格的文件。大多数系统管理员需要不时地解决这个问题 - 请参阅http://serverfault.com/questions/225276/batch-file-code-to-find-and-rename-all-files-ending-in-a-space/230958#230958 – RobW 2012-04-03 18:57:17

+0

@RobW - 哇,非常有趣(和讨厌)。谢谢(你的)信息。链接中重定向路径开头的\\?\语法是什么?我不熟悉它。 – dbenham 2012-04-03 19:30:18

+0

@dbenham检查了这一点... http://docs.racket-lang.org/reference/windowspaths.html – 2012-12-27 17:59:37