我正在進行一些Arduino HID研究。 我試圖設置我的Leo打開PowerShell,並通過卷名將我的Documents文件夾中的所有.pdf備份到閃存驅動器。RoboCopy在通用文件路徑中給出錯誤
我希望這可以移植到不同的機器上。因此,不能使用包含用戶名的指定文件路徑。
我發現的原始腳本就是這個。
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 + "\"
[email protected]($env:username + "\Documents\")
[email protected]($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...
之所以說它是如此混亂是因爲我的文檔文件夾中的NOR閃存驅動器中不存在這些目錄,ROBOCOPY試圖複製。 C:\用戶\我\文檔\
C:\用戶\我\ Documents \我的音樂\ C:\用戶\我\ Documents \我的圖片\ F:\我的圖片\
所以我完全陷入困境,來到這裏向專業人士尋求幫助。 我也試過%USERNAME%
和%USERPROFILE%
在文件路徑中使用我上面說過的相同的腳本變體,但是由於robocopy假設它們是實際路徑名稱的一部分,所以不起作用。 I.E. C:users\%USERPROFILE%\Documents
所以得出結論我需要能夠插入一個命名卷閃存驅動器到我的電腦。 插入我的Arduino讓它在CMD,POWERSHELL中輸入一個命令,或者在記事本中創建一個.ps1,但是我遇到的問題是在未使用特定用戶名的路徑中不能識別源目錄路徑可能是因爲我需要在機器和用戶之間使用這種便攜式設備。
關於報價。在我的acutal腳本中沒有引號被附加到文件路徑。報價僅用於該帖子。我應該更清楚。當你說腳本在你身上運行的很好。你是否給了$ SrcDir和確切的文件路徑,即C:users/yourusername /或者你是否傳遞了$ SrcDir參數$ env:username或%USERPROFILE% –
你用第二個問題/語句回答了它。現在你已經指出了這一點。 –