• Command (Batch) File Help!

    Home » Forums » AskWoody support » Windows » Windows – other » Command (Batch) File Help!

    • This topic has 19 replies, 7 voices, and was last updated 15 years ago.
    Author
    Topic
    #468225

    I have a command file that I use to copy files from the original location, to a backup location. I just noticed, that the “Files” are copying OK, but the “Folders” contained in the source folder, are NOT being copied.

    Can anybody help me edit this file to also copy the folders?

    Xcopy D:”My Documents”*.* D:QBackup”My Documents”*.* /y

    Side Note: A BIG hug to Fred Langa, for opening the door to command files for backup purposes, many years ago 🙂

    Viewing 14 reply threads
    Author
    Replies
    • #1218855

      You need to add the /s or /e switch to your command, i.e.

      Xcopy D:”My Documents”*.* D:QBackup”My Documents”*.* /s /y or
      Xcopy D:”My Documents”*.* D:QBackup”My Documents”*.* /e /y

      You can also use

      Xcopy D:”My Documents” D:QBackup”My Documents” /s /y or
      Xcopy D:”My Documents” D:QBackup”My Documents” /e /y

      See xcopy /?

      Ed

    • #1218938

      Side Note: A BIG hug to Fred Langa, for opening the door to command files for backup purposes, many years ago 🙂

      If I recommend RoboCopy instead of the rather dated Xcopy, do I get a hug too?!

      BATcher

      Plethora means a lot to me.

      • #1219002

        If I recommend RoboCopy instead of the rather dated Xcopy, do I get a hug too?!

        I would second this suggestion it is a much better solution for a point to point backup. Much more extensive than the old faithful Xcopy

    • #1219061

      No argument from me.

      Just responding to his original question, but should’ve mentioned it.

      Ed

    • #1219064

      Thanks Ed…I got the command file to do what I want now!

      I looked at robocopy GUI, & did some testing. It is extremely fast, even though my command file is plenty fast enough for me. I ran robocopy to replicate the same line as my command file, but I have the same (original) problem with it NOT copying folders. I’m sure there is a command that I could use to include folders, but I don’t know what it is.

      I may move on to using robocopy when I have some time to put it ALL together.

      I should say that the complete command file I use, does not just have the line I posted, but it is a single line of a multi-line command. All total I have 9 different backup task included in the command, with each one running one after another. I then have it run using “Schedule Task” at night.

      Can I setup a robocopy command to run multiple tasks, all in the same command (i.e 9 different backup tasks)?

    • #1219069

      Like almost all the backup programs I know of, each execution with back up a set of folders using specified criteria. So nine sets of folders means nine backups.

      If I understand you correctly, you are saying that only the files in the ‘root’ are being backed up, and not subfolders or the files in them? Then for RoboCopy specify /s to do subdirectories, also.

      Here’s an example use of RoboCopy (somewhat simplified!) – look up the options in the documentation:

      Code:
      :: set up the source and target paths
      set source=C:
      set target=E:RoboCopy of C driveMonday
      set log=E:RoboLogsRoboCopy_Monday.log
      :: build up the options/parameters
      set opts=/mir /np /r:0 /w:0 /ndl
      :: excludes
      set opts=%opts% /xd Recycler "System Volume Information" cache bases
      set opts=%opts% /xf hiberfil.sys pagefile.sys
      :: perform the backup
      robocopy "%source%" "%target%" %opts% > %log%

      BATcher

      Plethora means a lot to me.

    • #1219150

      That worked Batcher.

      So i am taking the plunge into robocopy to build my 9 backup script, by creating each one at a time…then when successful I C & P the working individual script to, lets say my “Master Script”.

      Here’s where I am stuck…several of my backup jobs are for individual files (not folders). Again, I am using the Robocopy GUI to make the scripts. When I browse to the source folder, I am not able to browse to the individual file I want, so after I choose the folder, I manually type in the file. The file I type in is highlighted in red.

      robocopy “H:Program FilesKeePass Password Safe 2Keypass.kdbx

      “D:QBackupKeePass” /S /V /NP /R:10 /W:30
      pause

      When I run the script, I get this error: ” The file name, directory name, or volume syntax is incorrect”. Can anyone direct me as how to get this to work?

    • #1219171

      Larry, you have forgotten the syntax of the RoboCopy command and have reverted to Xcopy syntax!
      !

      Code:
                    Usage :: ROBOCOPY source destination [file [file]...] [options]
      
                   source :: Source Directory (drive:path or \serversharepath).
              destination :: Destination Dir  (drive:path or \serversharepath).
                     file :: File(s) to copy  (names/wildcards: default is "*.*").

      If you are copying all files then the default is *.* and so this parameter usually gets left out…
      I leave the correction of your command as an exercise for you…

      BATcher

      Plethora means a lot to me.

      • #1219247

        Larry, you have forgotten the syntax of the RoboCopy command and have reverted to Xcopy syntax!
        !

        Code:
                      Usage :: ROBOCOPY source destination [file [file]...] [options]
        
                     source :: Source Directory (drive:path or \serversharepath).
                destination :: Destination Dir  (drive:path or \serversharepath).
                       file :: File(s) to copy  (names/wildcards: default is "*.*").

        If you are copying all files then the default is *.* and so this parameter usually gets left out…
        I leave the correction of your command as an exercise for you…

        Let me clarify where I’m at…I have solved my first problem of being able to copy “sub-folders”, by adding the /s switch to the script…SOLVED

        BUT, my 2nd problem is when trying to copy (backup) an individual file.

        Again, I am using robocopy GUI to create the script…it will not “browse” to an individual “file”, but it will browse to the folder (which I select)…I then, manually type in the “file” I want to backup by adding “keypas.kdbx” to the end of the script> save & run. This is where I get the error as shown in the screen shot…the problem resides in the “source”, which I can’t figure out (& believe me I have spent 4 hours trying different switches)

        DocWho, My original problem with my xcopy command was answered with success above, but a couple of expert replies recommended that I use robocopy instead of xcopy…so they planted a mountain for me to climb, & now I have invested a lot of time trying to figure out robocopy…not sure if this journey is worth taking, as I have used xcopy for years myself with GREAT success…the quest for knowledge is painful at times LOL!

        • #1219275

          Let me clarify where I’m at…I have solved my first problem of being able to copy “sub-folders”, by adding the /s switch to the script…SOLVED

          BUT, my 2nd problem is when trying to copy (backup) an individual file.

          Again, I am using robocopy GUI to create the script…it will not “browse” to an individual “file”, but it will browse to the folder (which I select)…I then, manually type in the “file” I want to backup by adding “keypas.kdbx” to the end of the script> save & run. This is where I get the error as shown in the screen shot…the problem resides in the “source”, which I can’t figure out (& believe me I have spent 4 hours trying different switches)

          DocWho, My original problem with my xcopy command was answered with success above, but a couple of expert replies recommended that I use robocopy instead of xcopy…so they planted a mountain for me to climb, & now I have invested a lot of time trying to figure out robocopy…not sure if this journey is worth taking, as I have used xcopy for years myself with GREAT success…the quest for knowledge is painful at times LOL!

          I don’t get it. It looks like BATcher gave you the answer in his last post. Did you try the syntax he showed you?

          robocopy “H:Program FilesKeePass Password Safe 2” “D:QBackupKeePass” “Keypass.kdbx” [–other options go here–]

    • #1219202

      I’ve been using XCOPY almost as long as it has existed in MS-DOS.

      It helps if you just go to a command prompt and type:

      XCOPY /? (press Enter)

      That will give you a complete list of all the switches that can be used with XCOPY.

      The switch you needed in the above problem was the /s switch, to copy sub-directories.

      I have used xcopy to back up whole drives before.

      One typical line I use to backup the entire “My Documents” folder to my storage drive is:

      Rem Backup My Documents and all sub-folders/files.
      xcopy “C:Documents and SettingsAlexMy Documents*.*” “D:My Documents” /s /y /H /R /D

      Xcopy works great when properly applied.
      I like using what MS gave me instead of downloading more programs to add to the HD bloat.
      I’ve been doing it for thirty years, so I probably won’t change now.
      My first HD was only 20 megs in capacity, so I learned real quick to save HD space wherever possible.

      Cheers mates!
      the Doctor

    • #1219276

      I’m afraid I can’t help you with the RoboCopy GUI – I’ve never bothered with it as unnecessary to the CLI enthusiast!

      Setting your task up manually, you want something like

      Code:
      robocopy "H:Program FilesKeePass Password Safe 2"  "D:QBackupKeePass" Keypass.kdbx /S /V /NP /R:0 /W:0 
               Source Directory                             Destination         File         Parameters

      I have never seen any point in setting the /R and /W parameters to anything other than zero, because I’ve never found a condition which a small amount of more time would correct!

      BATcher

      Plethora means a lot to me.

    • #1219317

      I got it now!

      I seemed to have been blinded as to where to put the “source individual” file I wanted to backup…with xcopy, the file has to be behind the source path in the command, which I was trying to do the same with robocopy…I finally realized that the individual file has to be at the end of the script with a space in between, & also learned that the quotes have to include the drive…whereas with xcopy the quotes are behind the drive. It’s hard to break OLD habits!

      The one job I had trouble with is my address book. I used this script that would not work

      robocopy “C:Documents and Settingslarry bogieApplication DataMicrosoftAddress Book” “D:QBackupAddress Book” larry bogie.wab /S /V /NP /R:0 /W:0

      Yet this script did work

      robocopy “C:Documents and Settingslarry bogieApplication DataMicrosoftAddress Book” “D:QBackupAddress Book” /S /V /NP /R:0 /W:0

      There are 2 files: larry bogie.wab, & larry bogie.wab~. The only thing I can think of, is that is both files needed to be copied together…I duno!

      I did most of the scripts manually (without using robocopy GUI) to learn the inter workings.

      I now have all 9 of my backup jobs in 1 master script…tried & tested the script, and everything is working as I want.

      Have a look-see at my New Creation…I am VERY proud of it!

      robocopy “D:Bogie Documents” “D:QBackupBogie Documents” /S /V /NP /R:0 /W:0

      robocopy “D:Bids” “D:QBackupBids” /V /NP /R:0 /W:0

      robocopy “D:Contracts” “D:QBackupContracts” /V /NP /R:0 /W:0

      robocopy “C:Documents and Settingslarry bogieApplication DataOperaOpera” “D:QBackupOpera” /S /V /NP /R:0 /W:0 this script includes 3 backup files, that I used to do in 3 separate commands

      robocopy “H:Program FilesKeePass Password Safe 2” “D:QBackupKeePass” Keypass.kdbx /S /V /NP /R:0 /W:0

      robocopy “H:Program FilesIntuitQuickbooks 2008” “D:QBackupQuick BooksQBW” BLACKHAW.QBW /S /V /NP /R:0 /W:0

      robocopy “C:Documents and Settingslarry bogieApplication DataMicrosoftAddress Book” “D:QBackupAddress Book” /S /V /NP /R:0 /W:0
      pause

      Big Hugs to all who helped me accomplish this task 🙂

    • #1219334

      WTG, Larry.

      One other switch you might want to have a look at is /LOG:file (or /LOG+:file).

      Using this will let you verify that everything worked OK, or, if not, what the problem was.

      e.g. robocopy “D:Bogie Documents” “D:QBackupBogie Documents” /S /V /NP /R:0 /W:0 /LOG:”D:QBackup.log”

      or, if you want to keep the logs for, say, a week or so, then delete it and start a new one

      robocopy “D:Bogie Documents” “D:QBackupBogie Documents” /S /V /NP /R:0 /W:0 /LOG+:”D:QBackup.log”

      Using the same file name in all of your backups will create just the one log, or, if you prefer, you can log them to separate files.

      Very useful tool, is RoboCopy…

      Ed

      P.S. You really don’t need the quotes in D:QBackup.log but when I tried to post /LOG+:”D:QBackup.log” without them it showed as /LOG+QBackup.log

      Always remember that computers are strange beasts…

    • #1219838

      I know, I’m just an old ‘Stick in the mud’, because I don’t like changing something that works.

      But if I downloaded another program to do every little chore that I might want to do, my 200 gig
      hard drive wouldn’t be large enough to hold them all. So I try to use what’s already in Windows
      every chance I get.

      Another DOS program that I use extensively is the “Deltree.exe” command that showed up way
      back in DOS. It was present in all windows versions up to XP, where it was omitted. If reloaded
      into Windows, it still runs and works like a champ. It can delete a single file, a type of file, a folder,
      a complete folder tree or even an entire hard drive, depending on the syntax used. It has only one
      switch, the ” /y ” switch, to say yes to the old “Are you sure?” question.
      Be careful with this command though….. deltree /y C: will wipe the C drive clean.

      I wrote one batch file, that incorporates the Deltree command, that runs from my startup folder.
      It deletes the contents of all the temp file folders, temporary internet files, recent files, prefetch files and some other assorted junk, every time I reboot my PC. I call it my FREE Maid Service and I share it with my customers to help keep their PC’s clean.

      I’m glad that DOS batch files still work with Windows. It makes house keeping a lot easier.

      Cheers Mates!
      The Doctor

    • #1219842

      The functionality of DELTREE has been subsumed in XP and above into the somewhat improved RD, slightly supplemented by DEL. Have a look at their /? help information!

      Running old DOS commands in newer operating systems from which they have been removed runs the potential risk of them not coping with new situations.

      BATcher

      Plethora means a lot to me.

    • #1220136

      @ Dr.Who:
      I can imagine that you do not want to add other programs to your system than those provided, but Robocopy has actual big advantages over xcopy, especially if you want to synchronize two file system structures. The exclude by attribute option is really handy in combination with backup programs which clear the archive bit of files copied to the backup – you can copy files that are newer than your latest backup.
      Also, by default Robocopy copies only the files that have changed in your source in comparison with the destination, thus limiting the time it takes to create your copy.

      Robocopy is by default included in Windows Vista and 7 (in C:WindowsSystem32), which I found a huge surprise and relief, but also makes it ‘part’ of the OS.


      @BATcher
      :
      There are good reasons to set the default /R and /W settings to something larger than 0, if you copy over a network. Robocopy fully supports URIs, so you can use something like

      Code:
      robocopy "c:data" "\myserverBackupsc_data" *.* /xo

      If the network destination is temporarily unavailable, robocopy can perform X retries with a wait time, so you can survive network drops.
      Ok, most users will not have this problem at home, but in the corporate environment I work in we extensively use this particular option (especially when copying files from NL to far off places like India), and it will save you time if an overnight copy needs to be finished, even if the network connection is not too reliable. The /Z option (restartable mode) will also be of assistance in these situations.

      I myself make extensively use of the option to use subroutines in Windows command files. This allows you to re-use code that for instance does error checking, or abort the process in case of errors:

      Here’s a rewrite of the original script to show what I mean:

      Code:
      @echo off
      set OPTIONS=/V /NP /R:5 /W:30
      set RESULT=0
      
      call :CopyFiles "D:Bogie Documents" "D:QBackupBogie Documents" *.* /S
      call :CopyFiles "D:Bids" "D:QBackupBids" *.*
      call :CopyFiles "D:Contracts" "D:QBackupContracts" *.*
      call :CopyFiles "C:Documents and Settingslarry bogieApplication DataOperaOpera" "D:QBackupOpera" *.* /S
      
      rem I don't think this folder has any subfolders, but doesn't harm to have the /S specified
      call :CopyFiles "C:Documents and Settingslarry bogieApplication DataMicrosoftAddress Book" "D:QBackupAddress Book" *.* /S
      
      call :CopyFiles "H:Program FilesKeePass Password Safe 2" "D:QBackupKeePass" "Keypass.kdbx"
      call :CopyFiles "H:Program FilesIntuitQuickbooks 2008" "D:QBackupQuick BooksQBW" "BLACKHAW.QBW"
      
      
      rem you can put a pause here if you always want to see the output from Robocopy in the command window
      rem pause
      exit /b %RESULT%
      
      :CopyFiles
      if %RESULT% NEQ 0 exit /b %RESULT%
      robocopy "%~1" "%~2" "%~3" %~4 %OPTIONS%
      set RESULT=%ERRORLEVEL%
      if %RESULT% EQU 1 SET RESULT=0
      if %RESULT% EQU 0 exit /b %RESULT%
      
      echo There was an error when copying "%~1%~3" to "%~2"
      pause
      exit /b %RESULT%
      
      

      This script now also returns the error code back to the calling program, so you could use it in an automated environment (as a scheduled job) and for instance email a user if the script fails.

      I hope that this might be of any use to anybody.

      Kind regards,

      Eelco Ligtvoet.

      • #1220153

        If you have a network where RoboCopy needs to do retries to work, separately from Windows itself, then I would suggest you got someone to look at it!

        BATcher

        Plethora means a lot to me.

    • #1220156

      Boy, this thread has come a LOOOOOOOOOONG way from where it started LOL!

      Ed: I like your suggestion to include a “Log” , and have included it to my script 🙂

      DrWho, Just to elaborate what Eelco said, robocopy itself is a command script that is part of Windows (not a separate program to DL)…the program I DL’d is “robocopy GUI”, which is a tiny utility to make scripts ( it helped me learn how robocopy works), but robocopy is similar to xcopy, in that, it is an enhanced (Robust) script. What I really like about robocopy, is that it does not copy “unchange” files, making my script run in SUPER fast…1 second, when there are no changed files. 99% of my files are not changed daily, so with xcopy, there is a lot of copying that is not necessary.

      As far as the /r & /w retries, In my backup job, I don’t see any need, since I am merely copying drive to drive locally. I could see if you were backing up to India as Eslco was saying.

      Great help from everybody…thanks!

    Viewing 14 reply threads
    Reply To: Command (Batch) File Help!

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: