【发布时间】:2018-01-23 03:40:28
【问题描述】:
我正在进行一些 Arduino HID 研究。 我试图将我的 Leo 设置为打开 powershell 并按卷名将我的 Documents 文件夹中的所有 .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:\用户\我\文档\我的图片\ F:\我的图片\
所以我完全被难住了,来到这里向专业人士寻求帮助。
我还使用我上面提到的相同脚本变体在文件路径中尝试了%USERNAME% 和%USERPROFILE%,但这也不起作用,因为 robocopy 假设它们是实际路径名的一部分。 I.E. C:users\%USERPROFILE%\Documents
因此,总而言之,我需要能够将命名卷闪存驱动器插入我的电脑。 插入我的 Arduino,让它在 CMD、POWERSHELL 中输入命令,或在记事本中创建一个 .ps1,但我遇到的问题是在路径中不使用特定用户名时无法识别源目录路径,这是不可能的由于我需要在机器和用户之间进行移植。
【问题讨论】:
标签: windows powershell cmd scripting robocopy