I am trying to use a batch file to automatically copy my Desktop to the cloud each afternoon. I can do this with other drives, but not my desktop

4

This command works properly to create a backup of my F drive to my OneDrive, naming the folder with the current date:

xcopy "F:\" "C:\Users\myUserName\OneDrive for Business\F Backup %date:~-4,4%%date:~-10,2%%date:~-7,2%" /e /i /h /k /q /s /v /y /z

However, when I try something similar for my Desktop, it does not work:

xcopy "C:\Users\myUserName\Desktop\" "C:\Users\myUserName\OneDrive for Business\Desktop Backup-%date:~-4,4%%date:~-10,2%%date:~-7,2%" /e /i /h /k /q /s /v /y /z

It's driving me crazy. I have both in a batch file that runs at the same time each day. The desktop line runs first and seems to work (but nothing has been copied when I check) and then the F drive command runs fine.

Any suggestions would be most appreciated. I'm guessing it's one the extra parameters not playing nice with the Desktop? As is probably obvious, my batch file knowledge is pretty nonexistent (I'm quite impressed that I got the F drive piece working...), so please forgive my ignorance. Thank you.

Elise

Posted 2015-10-30T18:07:12.860

Reputation: 41

if you run the problematic command manually in a command-prompt, does it report any errors or anything? – Ƭᴇcʜιᴇ007 – 2015-10-30T18:17:19.490

Try what Techie007 said, but i would also make sure it doesn't need admin permissions. Run CMD prompt as admin wouldn't hurt to check, but most likely it is needing some kind of permission to access the desktop and is failing. If you could tell us how you have the scheduler setup, such as what program you use, what options are set, etc. – dakre18 – 2015-10-30T18:19:41.640

Just a question... Would setting your desktop mapped network drive and coping the network drive work? – RookieTEC9 – 2015-10-30T18:49:53.410

@Ƭᴇcʜιᴇ007 - I don't seem to get any errors. It's very quick - the command prompt opens and closes quickly. – Elise – 2015-10-30T19:31:42.087

@dakre18 - That may be it. I do not have administrative rights on my work computer. I have set it to run through the Windows task scheduler. – Elise – 2015-10-30T19:32:26.103

@RookieTEC9 - That's a bit over my head, to be honest. – Elise – 2015-10-30T19:33:01.850

Open a command prompt (cmd) and then manually paste/type in the command and hit enter, that way the command prompt window won't close when it's done and you can see the results. :) – Ƭᴇcʜιᴇ007 – 2015-10-30T19:43:20.530

OK, now I really am going crazy. I tried this just in the command to originally get it working a week or so ago and I remember it telling me it had copied x number of files. I had trouble getting it to name the destination folder what I wanted (it seemed to just revert back to "Desktop" with no date). Now it's just telling me "Invalid path. 0 File(s) copied." – Elise – 2015-10-30T20:04:02.790

Just curious, but i just double checked the xcopy switches you are using (the / letters). /s and /e are roughly the same, the only difference is /s does not copy empty folders, while /e copies empty folders. You only have to use 1 of those, but that should not cause any issues. I would suggest trying it with only /s or /e and see if it gives you any errors. Let us know if there are any errors, because that may help figure out why. – dakre18 – 2015-10-30T20:47:25.703

@dakre18 - No change. Same result with both or just /s or just /e. – Elise – 2015-10-30T21:19:30.143

@Elise I would suggest testing it a bit. For example, add a blank text document to the desktop, run it and see if it copies the file. Use /s or /e with /u to see if it will copy only files that exist already. Also try adding /-y which will prompt you to overwrite a file, if it's being ignored/suppressed. Those tests should help figure out what's happening, but lets hope it isn't because you don't have admin rights. I would even suggest trying something different like a folder in your documents instead. – dakre18 – 2015-10-30T21:25:51.327

Elise - Any update on this? See Accepting an Answer to ensure you understand how that's supposed to work.

– Pimp Juice IT – 2017-04-05T05:07:31.690

Answers

1

batch file to automatically copy my Desktop to the cloud each afternoon

However, when I try something similar for my Desktop, it does not work:

xcopy "C:\Users\myUserName\Desktop\" "C:\Users\myUserName\OneDrive for Business\Desktop Backup-%date:~-4,4%%date:~-10,2%%date:~-7,2%" /e /i /h /k /q /s /v /y /z

Now it's just telling me "Invalid path. 0 File(s) copied.

POTENTIAL ISSUES AND MORE

  1. XCOPY is a deprecated command intended to be replaced by Robocopy specifically per Microsoft as of Windows Vista (and newer OSes).

    • I'm going to post an example Robocopy command batch solution below since it is a Windows native solution but NOT deprecated just like XCOPY
  2. Some of the XCOPY command switches seem to conflict in your example when used together:

    • /S Copies directories and subdirectories except empty ones.
    • /E Copies directories and subdirectories, including empty ones.
  3. Using the /I switch and not making the source ("C:\Users\myUserName\Desktop\") end in \*.* may be an issue here with your other switches since you're telling it to assume it's a directory in the destination if it doesn't exist before the copy occurs.

    • /I If destination does not exist and copying more than one . file, assumes that destination must be a directory.
  4. You're not checking whether or not the new and explicit ~\Desktop Backup YYYYMMDD folder exists first and if not then create it.

    • IF NOT EXIST "~\Desktop Backup YYYYMMDD" MD "~\Desktop Backup YYYYMMDD"
  5. You're not logging the output of these commands to a log file to see details when you're not running manually from the command line otherwise.

    • ~ /s /v /y /z>>C:\Path\Logfile.txt

BETTER MODERN WINDOWS SOLUTION

ROBOCOPY BATCH SCRIPT EXAMPLE

(See SCRIPT NOTE below for options I used in this example and also be sure to change the sourcedir and targetdir variable paths where you need those set. Be sure to test from a test location as well just to be thorough and to confirm yourself before using for production purposes)

@ECHO ON
SETLOCAL
SET SourceDir=C:\Users\myUserName\Desktop
SET TargetDir=C:\Users\myUserName\OneDrive for Business\F Backup %date:~-4,4%%date:~-10,2%%date:~-7,2%
SET LogFile=C:\LogPath\Logfile.txt
IF NOT EXIST "%TargetDir%" MD "%TargetDir%"
ROBOCOPY "%SourcePath%" "%TargetDir%" *.* /PURGE /S /NP /ZB /R:5 /LOG+:%Log% /TS /FP
GOTO EOF

SCRIPT NOTE

I used these options in my example but see below how to see all options to further suit your particular needs. Just not that some of the Robocopy options are default to do what some of the XCOPY switches were doing and needed for so you may not need to include every switch you think you'd need to otherwise.

An example would be by default it copying to destination with the (default is /COPY:DAT) and (copyflags : D=Data, A=Attributes, T=Timestamps). So you don't need to specify /COPY:DAT.

/S         :: copy Subdirectories, but not empty ones.
/PURGE     :: delete dest files/dirs that no longer exist in source.
/NP        :: No Progress - don't display % copied.
/ZB        :: use restartable mode; if access denied use Backup mode.
/R:n       :: number of Retries on failed copies: default 1 million.
/LOG+:file :: output status to LOG file (append to existing log).
/TS        :: include source file Time Stamps in the output.
/FP        :: include Full Pathname of files in the output.

FURTHER RESEARCH AND DETAIL

From command line in Windows, type in Robocopy /? and then press Enter

C:\Users\PJ>robocopy /?

-------------------------------------------------------------------------------
   ROBOCOPY     ::     Robust File Copy for Windows

-------------------------------------------------------------------------------

  Started : Fri Dec 18 02:29:48 2015

              Usage :: ROBOCOPY source destination [file [file]...] [options]

             source :: Source Directory (drive:\path or \\server\share\path).
        destination :: Destination Dir  (drive:\path or \\server\share\path).
               file :: File(s) to copy  (names/wildcards: default is "*.*").

::
:: Copy options :
::
                 /S :: copy Subdirectories, but not empty ones.
                 /E :: copy subdirectories, including Empty ones.
             /LEV:n :: only copy the top n LEVels of the source directory tree.

                 /Z :: copy files in restartable mode.
                 /B :: copy files in Backup mode.
                /ZB :: use restartable mode; if access denied use Backup mode.
            /EFSRAW :: copy all encrypted files in EFS RAW mode.

  /COPY:copyflag[s] :: what to COPY for files (default is /COPY:DAT).
                       (copyflags : D=Data, A=Attributes, T=Timestamps).
                       (S=Security=NTFS ACLs, O=Owner info, U=aUditing info).

           /DCOPY:T :: COPY Directory Timestamps.

               /SEC :: copy files with SECurity (equivalent to /COPY:DATS).
           /COPYALL :: COPY ALL file info (equivalent to /COPY:DATSOU).
            /NOCOPY :: COPY NO file info (useful with /PURGE).

            /SECFIX :: FIX file SECurity on all files, even skipped files.
            /TIMFIX :: FIX file TIMes on all files, even skipped files.

             /PURGE :: delete dest files/dirs that no longer exist in source.
               /MIR :: MIRror a directory tree (equivalent to /E plus /PURGE).

               /MOV :: MOVe files (delete from source after copying).
              /MOVE :: MOVE files AND dirs (delete from source after copying).

     /A+:[RASHCNET] :: add the given Attributes to copied files.
     /A-:[RASHCNET] :: remove the given Attributes from copied files.

            /CREATE :: CREATE directory tree and zero-length files only.
               /FAT :: create destination files using 8.3 FAT file names only.
               /256 :: turn off very long path (> 256 characters) support.

             /MON:n :: MONitor source; run again when more than n changes seen.
             /MOT:m :: MOnitor source; run again in m minutes Time, if changed.

      /RH:hhmm-hhmm :: Run Hours - times when new copies may be started.
                /PF :: check run hours on a Per File (not per pass) basis.

             /IPG:n :: Inter-Packet Gap (ms), to free bandwidth on slow lines.

                 /SL:: copy symbolic links versus the target.
::
:: File Selection Options :
::
                 /A :: copy only files with the Archive attribute set.
                 /M :: copy only files with the Archive attribute and reset it.
    /IA:[RASHCNETO] :: Include only files with any of the given Attributes set.
    /XA:[RASHCNETO] :: eXclude files with any of the given Attributes set.

 /XF file [file]... :: eXclude Files matching given names/paths/wildcards.
 /XD dirs [dirs]... :: eXclude Directories matching given names/paths.

                /XC :: eXclude Changed files.
                /XN :: eXclude Newer files.
                /XO :: eXclude Older files.
                /XX :: eXclude eXtra files and directories.
                /XL :: eXclude Lonely files and directories.
                /IS :: Include Same files.
                /IT :: Include Tweaked files.

             /MAX:n :: MAXimum file size - exclude files bigger than n bytes.
             /MIN:n :: MINimum file size - exclude files smaller than n bytes.

          /MAXAGE:n :: MAXimum file AGE - exclude files older than n days/date.
          /MINAGE:n :: MINimum file AGE - exclude files newer than n days/date.
          /MAXLAD:n :: MAXimum Last Access Date - exclude files unused since n.
          /MINLAD:n :: MINimum Last Access Date - exclude files used since n.
                       (If n < 1900 then n = n days, else n = YYYYMMDD date).

                /XJ :: eXclude Junction points. (normally included by default).

               /FFT :: assume FAT File Times (2-second granularity).
               /DST :: compensate for one-hour DST time differences.

               /XJD :: eXclude Junction points for Directories.
               /XJF :: eXclude Junction points for Files.

::
:: Retry Options :
::
               /R:n :: number of Retries on failed copies: default 1 million.
               /W:n :: Wait time between retries: default is 30 seconds.

               /REG :: Save /R:n and /W:n in the Registry as default settings.

               /TBD :: wait for sharenames To Be Defined (retry error 67).

::
:: Logging Options :
::
                 /L :: List only - don't copy, timestamp or delete any files.
                 /X :: report all eXtra files, not just those selected.
                 /V :: produce Verbose output, showing skipped files.
                /TS :: include source file Time Stamps in the output.
                /FP :: include Full Pathname of files in the output.
             /BYTES :: Print sizes as bytes.

                /NS :: No Size - don't log file sizes.
                /NC :: No Class - don't log file classes.
               /NFL :: No File List - don't log file names.
               /NDL :: No Directory List - don't log directory names.

                /NP :: No Progress - don't display % copied.
               /ETA :: show Estimated Time of Arrival of copied files.

          /LOG:file :: output status to LOG file (overwrite existing log).
         /LOG+:file :: output status to LOG file (append to existing log).

       /UNILOG:file :: output status to LOG file as UNICODE (overwrite existing
log).
      /UNILOG+:file :: output status to LOG file as UNICODE (append to existing
log).

               /TEE :: output to console window, as well as the log file.

               /NJH :: No Job Header.
               /NJS :: No Job Summary.

           /UNICODE :: output status as UNICODE.

::
:: Job Options :
::
       /JOB:jobname :: take parameters from the named JOB file.
      /SAVE:jobname :: SAVE parameters to the named job file
              /QUIT :: QUIT after processing command line (to view parameters).

              /NOSD :: NO Source Directory is specified.
              /NODD :: NO Destination Directory is specified.
                /IF :: Include the following Files.

Pimp Juice IT

Posted 2015-10-30T18:07:12.860

Reputation: 29 425