RoboCopy 在通用文件路径中给出错误
RoboCopy giving error in universal file path
我正在进行一些 Arduino HID 研究。
我试图将我的 Leo 设置为打开 powershell 并按卷名将我的文档文件夹中的所有 .pdf 备份到闪存驱动器。
我希望它可以移植到不同的机器上。因此不能使用包含用户名的指定文件路径。
我找到的原剧本就是这个
param([parameter(mandatory=$true)]$VolumeName,
[parameter(mandatory=$true)]$SrcDir)
# find connected backup drive:
$backupDrive = $null
get-wmiobject win32_logicaldisk | % {
if ($_.VolumeName -eq $VolumeName) {
$backupDrive = $_.DeviceID
}
}
if ($backupDrive -eq $null) {
throw "$VolumeName drive not found!"
}
# mirror
$backupPath = $backupDrive + "\"
& robocopy.exe $SrcDir $backupPath /MIR /Z
我遇到的问题是,当我经过 C:\users$env:username\Documents\
的路径时
Powershell 抛出错误说明。
"ERROR 123 (0x0000007B) Accessing Source Directory C:\Users$env:USERNAME\Documents\
The filename, directory name, or volume label syntax is incorrect."
接下来我尝试删除 $srcDIR 参数并在变量中指定路径,新脚本如下所示:
param([parameter(mandatory=$true)]$VolumeName)
$backupDrive = $null
get-wmiobject win32_logicaldisk | % {
if ($_.VolumeName -eq $VolumeName) {
$backupDrive = $_.DeviceID
}
}
$backupPath = $backupDrive + "\"
$source=@($env:username + "\Documents\")
$destination=@($backupPath)
robocopy $source $destination *.pdf /mir /z
这也失败了,给了我另一个路径错误,显然 robocopy 正在输入我在这里两次看到的用户名:
-------------------------------------------------------------------------------
ROBOCOPY :: Robust File Copy for Windows
-------------------------------------------------------------------------------
Started : Tuesday, August 15, 2017 3:49:04 AM
Source : C:\Users\me\me\Documents\
Dest = F:\
Files : *.pdf
Options : /S /E /DCOPY:DA /COPY:DAT /PURGE /MIR /Z /R:1000000 /W:30
------------------------------------------------------------------------------
2017/08/15 03:49:04 ERROR 3 (0x00000003) Accessing Source Directory C:\Users\damav\damav\Documents\
The system cannot find the path specified.
所以我编辑了最后一行以包含直接路径并且没有变量。
robocopy C:\Users$env:username\Documents\ $backupDrive *.pdf /mir /z
输出给出了不同的结果,这比其他问题更让我困惑。看看:
-------------------------------------------------------------------------------
ROBOCOPY :: Robust File Copy for Windows
-------------------------------------------------------------------------------
Started : Tuesday, August 15, 2017 3:55:36 AM
Source : C:\Users\me\Documents\
Dest = F:\
Files : *.pdf
Options : /S /E /DCOPY:DA /COPY:DAT /PURGE /MIR /Z /R:1000000 /W:30
------------------------------------------------------------------------------
0 C:\Users\me\Documents\
0 C:\Users\me\Documents\My Music\
New Dir 0 C:\Users\me\Documents\My Pictures\
2017/08/15 03:55:36 ERROR 5 (0x00000005) Time-Stamping Destination Directory F:\My Pictures\
Access is denied.
Waiting 30 seconds... Retrying...
2017/08/15 03:56:07 ERROR 5 (0x00000005) Time-Stamping Destination Directory F:\My Pictures\
Access is denied.
Waiting 30 seconds...
之所以如此混乱,是因为 robocopy 试图复制的这些目录不存在于我的文档文件夹或闪存驱动器中。
C:\Users\me\Documents\
C:\Users\me\Documents\我的音乐\
C:\Users\me\Documents\我的图片\
F:\我的图片\
所以一头雾水,特地来求助高手。
我还在文件路径中尝试了 %USERNAME%
和 %USERPROFILE%
,使用我上面提到的相同脚本变体,但由于 robocopy 假设它们是实际路径名的一部分,这也不起作用。 I.E. C:users\%USERPROFILE%\Documents
总而言之,我需要能够将命名卷闪存驱动器插入我的电脑。
插入我的 Arduino,让它在 CMD、POWERSHELL 中输入命令,或在记事本中创建一个 .ps1,但我遇到的问题是在路径中未使用特定用户名时无法识别源目录路径这是不可能的,因为我需要跨机器和用户进行移植。
Issue 1:
The Problem I am having is that when I pass the path of C:\users$env:username\Documents\ Powershell throws an error stating.
只是假设,但这听起来像是关于如何传输路径的问题。看看下面的代码:
'C:\users$env:username\Documents\'
"C:\users$env:username\Documents\"
两行都在生成一个字符串,但只有第二行 "translates" 到正确的路径。 Powershell 区分普通引号 (") 和单引号 (')。有关详细信息,请参阅 About Quoting Rules。
脚本在我的测试中完全正常,使用源目录的正常引号。
Issue 2:
Next i tried removing the $srcDIR parameter and specifying the path in a variable with the new script looking like this:
That failed as well giving me the another path error apparently robocopy is imputing my username twice seen here:
您的问题是 $env:username 仅包含您的用户名,这取决于您启动脚本的位置,您的代码的行为会有所不同。从 C:\users\me 开始,它转换为 C:\users\me\me\documents,如果你在 C:\users 中,它会转换为 C:\uses\me\documents 并且它可以工作。我会使用 $env:username 而不是硬编码路径:
"$env:USERPROFILE\Documents"
Issue 3:
The reason why it is so confusing is because these directories that robocopy tried to copy do not exist within my documents folder nor flash drive. C:\Users\me\Documents\
robocopy 错误告诉您它无法在您的目标上写入,可能是 permission/file 系统(NTFS 到 FAT32?)问题。确保您可以在闪存驱动器上写入。
例如先试试不用powershell脚本直接用robocopy写就可以了
robocopy C:\users\me\Documents\ F:\ *.pdf /MIR /Z
更改短文件名的注册表项作为最佳实践分析器中建议的清理的一部分。它告诉我修改的关键是这个。
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem\ NtfsDisable8dot3NameCreation
它默认为 2,我根据 BPA 将其更改为 1。我回去把它改回 2 并重新运行 robocopy 脚本没有错误。
只是想让人们知道什么对我有用:
reg add HKLM\SYSTEM\CurrentControlSet\Control\FileSystem /v NtfsDisable8dot3NameCreation /t REG_DWORD /d 2 /f
我的 Windows 10 1909 (18363.778) 默认为 0。我刚设置了 2,现在效果很好。
我正在进行一些 Arduino HID 研究。 我试图将我的 Leo 设置为打开 powershell 并按卷名将我的文档文件夹中的所有 .pdf 备份到闪存驱动器。
我希望它可以移植到不同的机器上。因此不能使用包含用户名的指定文件路径。
我找到的原剧本就是这个
param([parameter(mandatory=$true)]$VolumeName,
[parameter(mandatory=$true)]$SrcDir)
# find connected backup drive:
$backupDrive = $null
get-wmiobject win32_logicaldisk | % {
if ($_.VolumeName -eq $VolumeName) {
$backupDrive = $_.DeviceID
}
}
if ($backupDrive -eq $null) {
throw "$VolumeName drive not found!"
}
# mirror
$backupPath = $backupDrive + "\"
& robocopy.exe $SrcDir $backupPath /MIR /Z
我遇到的问题是,当我经过 C:\users$env:username\Documents\
的路径时
Powershell 抛出错误说明。
"ERROR 123 (0x0000007B) Accessing Source Directory C:\Users$env:USERNAME\Documents\
The filename, directory name, or volume label syntax is incorrect."
接下来我尝试删除 $srcDIR 参数并在变量中指定路径,新脚本如下所示:
param([parameter(mandatory=$true)]$VolumeName)
$backupDrive = $null
get-wmiobject win32_logicaldisk | % {
if ($_.VolumeName -eq $VolumeName) {
$backupDrive = $_.DeviceID
}
}
$backupPath = $backupDrive + "\"
$source=@($env:username + "\Documents\")
$destination=@($backupPath)
robocopy $source $destination *.pdf /mir /z
这也失败了,给了我另一个路径错误,显然 robocopy 正在输入我在这里两次看到的用户名:
-------------------------------------------------------------------------------
ROBOCOPY :: Robust File Copy for Windows
-------------------------------------------------------------------------------
Started : Tuesday, August 15, 2017 3:49:04 AM
Source : C:\Users\me\me\Documents\
Dest = F:\
Files : *.pdf
Options : /S /E /DCOPY:DA /COPY:DAT /PURGE /MIR /Z /R:1000000 /W:30
------------------------------------------------------------------------------
2017/08/15 03:49:04 ERROR 3 (0x00000003) Accessing Source Directory C:\Users\damav\damav\Documents\
The system cannot find the path specified.
所以我编辑了最后一行以包含直接路径并且没有变量。
robocopy C:\Users$env:username\Documents\ $backupDrive *.pdf /mir /z
输出给出了不同的结果,这比其他问题更让我困惑。看看:
-------------------------------------------------------------------------------
ROBOCOPY :: Robust File Copy for Windows
-------------------------------------------------------------------------------
Started : Tuesday, August 15, 2017 3:55:36 AM
Source : C:\Users\me\Documents\
Dest = F:\
Files : *.pdf
Options : /S /E /DCOPY:DA /COPY:DAT /PURGE /MIR /Z /R:1000000 /W:30
------------------------------------------------------------------------------
0 C:\Users\me\Documents\
0 C:\Users\me\Documents\My Music\
New Dir 0 C:\Users\me\Documents\My Pictures\
2017/08/15 03:55:36 ERROR 5 (0x00000005) Time-Stamping Destination Directory F:\My Pictures\
Access is denied.
Waiting 30 seconds... Retrying...
2017/08/15 03:56:07 ERROR 5 (0x00000005) Time-Stamping Destination Directory F:\My Pictures\
Access is denied.
Waiting 30 seconds...
之所以如此混乱,是因为 robocopy 试图复制的这些目录不存在于我的文档文件夹或闪存驱动器中。 C:\Users\me\Documents\
C:\Users\me\Documents\我的音乐\ C:\Users\me\Documents\我的图片\ F:\我的图片\
所以一头雾水,特地来求助高手。
我还在文件路径中尝试了 %USERNAME%
和 %USERPROFILE%
,使用我上面提到的相同脚本变体,但由于 robocopy 假设它们是实际路径名的一部分,这也不起作用。 I.E. C:users\%USERPROFILE%\Documents
总而言之,我需要能够将命名卷闪存驱动器插入我的电脑。 插入我的 Arduino,让它在 CMD、POWERSHELL 中输入命令,或在记事本中创建一个 .ps1,但我遇到的问题是在路径中未使用特定用户名时无法识别源目录路径这是不可能的,因为我需要跨机器和用户进行移植。
Issue 1:
The Problem I am having is that when I pass the path of C:\users$env:username\Documents\ Powershell throws an error stating.
只是假设,但这听起来像是关于如何传输路径的问题。看看下面的代码:
'C:\users$env:username\Documents\'
"C:\users$env:username\Documents\"
两行都在生成一个字符串,但只有第二行 "translates" 到正确的路径。 Powershell 区分普通引号 (") 和单引号 (')。有关详细信息,请参阅 About Quoting Rules。
脚本在我的测试中完全正常,使用源目录的正常引号。
Issue 2:
Next i tried removing the $srcDIR parameter and specifying the path in a variable with the new script looking like this: That failed as well giving me the another path error apparently robocopy is imputing my username twice seen here:
您的问题是 $env:username 仅包含您的用户名,这取决于您启动脚本的位置,您的代码的行为会有所不同。从 C:\users\me 开始,它转换为 C:\users\me\me\documents,如果你在 C:\users 中,它会转换为 C:\uses\me\documents 并且它可以工作。我会使用 $env:username 而不是硬编码路径:
"$env:USERPROFILE\Documents"
Issue 3: The reason why it is so confusing is because these directories that robocopy tried to copy do not exist within my documents folder nor flash drive. C:\Users\me\Documents\
robocopy 错误告诉您它无法在您的目标上写入,可能是 permission/file 系统(NTFS 到 FAT32?)问题。确保您可以在闪存驱动器上写入。 例如先试试不用powershell脚本直接用robocopy写就可以了
robocopy C:\users\me\Documents\ F:\ *.pdf /MIR /Z
更改短文件名的注册表项作为最佳实践分析器中建议的清理的一部分。它告诉我修改的关键是这个。
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem\ NtfsDisable8dot3NameCreation
它默认为 2,我根据 BPA 将其更改为 1。我回去把它改回 2 并重新运行 robocopy 脚本没有错误。
只是想让人们知道什么对我有用:
reg add HKLM\SYSTEM\CurrentControlSet\Control\FileSystem /v NtfsDisable8dot3NameCreation /t REG_DWORD /d 2 /f
我的 Windows 10 1909 (18363.778) 默认为 0。我刚设置了 2,现在效果很好。