RoboCopy giving error in universal file path

1

I am conducting some Arduino HID research. I was trying to set my Leo to open powershell and make a back up of all the .pdfs in my Documents folders to a flash drive by volume name.

I want this to be portable to different machines. So a specified file path that includes a username can not be used.

The original script I found is this one.

    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

The Problem I am having is that when I pass the path of C:\users\$env:username\Documents\ Powershell throws an error stating.

"ERROR 123 (0x0000007B) Accessing Source Directory C:\Users\$env:USERNAME\Documents\
The filename, directory name, or volume label syntax is incorrect."

Next i tried removing the $srcDIR parameter and specifying the path in a variable with the new script looking like this:

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

That failed as well giving me the another path error apparently robocopy is imputing my username twice seen here:

    -------------------------------------------------------------------------------
   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.

So I edited the last line to include a direct path and no varible.

robocopy C:\Users\$env:username\Documents\  $backupDrive *.pdf /mir /z 

The output gave a different results that confuses me more than the rest of the issues. Have a look:

-------------------------------------------------------------------------------
   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...

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\

C:\Users\me\Documents\My Music\ C:\Users\me\Documents\My Pictures\ F:\My Pictures\

So i am completely stumped and came here to ask the pros for some assistance. I have also tried %USERNAME% and %USERPROFILE% in the file path using the same script variations i stated above but that didn't work either due to robocopy assuming they was part of the actual path name. I.E. C:users\%USERPROFILE%\Documents

So to conclude I need to be able to plug a named volume flash drive into my pc. Insert my Arduino have it type out a command in CMD, POWERSHELL, or create a .ps1 in notepad but the issue i am having is with the source directory path not being recognized when not using a specific user name in the path which is not possible due to my need to have this portable across machines and users.

windows
powershell
cmd
scripting
robocopy
asked on Stack Overflow Aug 15, 2017 by Josh Pardue

2 Answers

1

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.

Just assuming, but this sounds like an issue on how you transmit your path. Take look at following code:

'C:\users\$env:username\Documents\'
"C:\users\$env:username\Documents\"

Both lines are generating a string, but only the second one "translates" to the correct path. Powershell differentiates between normal quotes (") and single quotes ('). See About Quoting Rules for more information.

The script works completely fine on my test using the normal quotes for the source directory.

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:

Your issue here is $env:username only contains your username, depending on where you start your script your code would behave differently. Starting from C:\users\me it translates to C:\users\me\me\documents, would you be in C:\users it would have translated to C:\uses\me\documents and it could have worked. Instead of a hard coded path with the $env:username I would use:

"$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\

The robocopy error tells you that it can't write on your target, could be a permission/file system (NTFS to FAT32?) issue. Make sure that you can write on your flash drive. e.g. try first to write without a powershell script just use robocopy directly

robocopy C:\users\me\Documents\ F:\ *.pdf /MIR /Z
answered on Stack Overflow Aug 15, 2017 by Olaf Reitz
0

Changing the registry key for short files names as part of the cleanup suggested in the Best Practice Analyzer. The key it told me to modify was this one.

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem\  NtfsDisable8dot3NameCreation

It was defaulted to 2 and I had changed it to 1 as per the BPA. I went back and changed it back to 2 and reran the robocopy script without errors.

Just wanted to let people know what worked for me:

reg add HKLM\SYSTEM\CurrentControlSet\Control\FileSystem  /v NtfsDisable8dot3NameCreation /t REG_DWORD /d 2 /f

My Windows 10 1909 (18363.778) was defaulted 0. I just set 2 and now it works like a charm.

answered on Stack Overflow Apr 5, 2021 by HARiOMSAi RAMKRiSHNA • edited Apr 5, 2021 by David Buck

User contributions licensed under CC BY-SA 3.0