Windows 7 Forums
Welcome to Windows 7 Forums. Our forum is dedicated to helping you find support and solutions for any problems regarding your Windows 7 PC be it Dell, HP, Acer, Asus or a custom build. We also provide an extensive Windows 7 tutorial section that covers a wide range of tips and tricks.


Windows 7: Advanced copying script

17 Jan 2011   #1
purpleglow

Windows 7 Enterprise x86
 
 
Advanced copying script

I've got some experience doing some basic batch scripting earlier, but it seems that this one is just too advanced for me.

I should create a script that copies one file (should be given as argument) from specific location to another and keeps the directory structure which is deeper than some specific folder of the source path. Let me explain this a bit more:

The destination folder is something like:

x:\stuff\

ex 1. If the source file is in x:\some\thing\stuff\test\file.txt, the file is copied to x:\stuff\test\file.txt

ex 2. If the source file is in x:\some\thing\stuff\test\test2\file.txt, the file is copied to x:\stuff\test\test2\file.txt

of course if those folders 'test' and 'test2' don't exit in the destination folder they are created.

how I get the path after the 'stuff' folder and put it into the destination path? for example in ex. 1, how do get the '\test\file.txt' and put it to the destination path like this: x:\stuff\test\file.txt

Thanks!


My System SpecsSystem Spec
.
17 Jan 2011   #2
Dzomlija

Windows 7 Ultimate x64
 
 

Quote   Quote: Originally Posted by purpleglow View Post
I've got some experience doing some basic batch scripting earlier, but it seems that this one is just too advanced for me.

I should create a script that copies one file (should be given as argument) from specific location to another and keeps the directory structure which is deeper than some specific folder of the source path. Let me explain this a bit more:

The destination folder is something like:

x:\stuff\

ex 1. If the source file is in x:\some\thing\stuff\test\file.txt, the file is copied to x:\stuff\test\file.txt

ex 2. If the source file is in x:\some\thing\stuff\test\test2\file.txt, the file is copied to x:\stuff\test\test2\file.txt

of course if those folders 'test' and 'test2' don't exit in the destination folder they are created.

how I get the path after the 'stuff' folder and put it into the destination path? for example in ex. 1, how do get the '\test\file.txt' and put it to the destination path like this: x:\stuff\test\file.txt

Thanks!
Have you tried using XCOPY:

Code:
 
Copies files and directory trees.
XCOPY source [destination] [/A | /M] [/D[:date]] [/P] [/S [/E]] [/V] [/W]
                           [/C] [/I] [/Q] [/F] [/L] [/G] [/H] [/R] [/T] [/U]
                           [/K] [/N] [/O] [/X] [/Y] [/-Y] [/Z] [/B]
                           [/EXCLUDE:file1[+file2][+file3]...]
  source       Specifies the file(s) to copy.
  destination  Specifies the location and/or name of new files.
  /A           Copies only files with the archive attribute set,
               doesn't change the attribute.
  /M           Copies only files with the archive attribute set,
               turns off the archive attribute.
  /D:m-d-y     Copies files changed on or after the specified date.
               If no date is given, copies only those files whose
               source time is newer than the destination time.
  /EXCLUDE:file1[+file2][+file3]...
               Specifies a list of files containing strings.  Each string
               should be in a separate line in the files.  When any of the
               strings match any part of the absolute path of the file to be
               copied, that file will be excluded from being copied.  For
               example, specifying a string like \obj\ or .obj will exclude
               all files underneath the directory obj or all files with the
               .obj extension respectively.
  /P           Prompts you before creating each destination file.
  /S           Copies directories and subdirectories except empty ones.
  /E           Copies directories and subdirectories, including empty ones.
               Same as /S /E. May be used to modify /T.
  /V           Verifies the size of each new file.
  /W           Prompts you to press a key before copying.
  /C           Continues copying even if errors occur.
  /I           If destination does not exist and copying more than one file,
               assumes that destination must be a directory.
  /Q           Does not display file names while copying.
  /F           Displays full source and destination file names while copying.
  /L           Displays files that would be copied.
  /G           Allows the copying of encrypted files to destination that does
               not support encryption.
  /H           Copies hidden and system files also.
  /R           Overwrites read-only files.
  /T           Creates directory structure, but does not copy files. Does not
               include empty directories or subdirectories. /T /E includes
               empty directories and subdirectories.
  /U           Copies only files that already exist in destination.
  /K           Copies attributes. Normal Xcopy will reset read-only attributes.
  /N           Copies using the generated short names.
  /O           Copies file ownership and ACL information.
  /X           Copies file audit settings (implies /O).
  /Y           Suppresses prompting to confirm you want to overwrite an
               existing destination file.
  /-Y          Causes prompting to confirm you want to overwrite an
               existing destination file.
  /Z           Copies networked files in restartable mode.
  /B           Copies the Symbolic Link itself versus the target of the link.
  /J           Copies using unbuffered I/O. Recommended for very large files.
The switch /Y may be preset in the COPYCMD environment variable.
This may be overridden with /-Y on the command line.
My System SpecsSystem Spec
17 Jan 2011   #3
purpleglow

Windows 7 Enterprise x86
 
 

Quote   Quote: Originally Posted by Dzomlija View Post

Have you tried using XCOPY:
Well, the basic copying isn't the problem. I'd like to know how I can get only certain part of the file's current path and put it into the destination path. Like this:

current path:

c:\files\foo\test.txt

destination path is:

x:\files\ + i have to add this part from the source path (current path) foo\test.txt, so the destination path should be x:\files\foo\

Let's explain a little bit more: what I'm trying to do is a copying script that gets filename as an argument and checks whats the path under the certain folder (in the above example this certain folder is 'files') and adds this path to destination path. My plan is to add this script to window's 'Send to:' menu.. so user just have to right click some file under the certain folder (it might be in the subfolder and that's the problem here) and 'send to: ' a script as an argument.

Hope you got my idea! Thanks!
My System SpecsSystem Spec
.

17 Jan 2011   #4
purpleglow

Windows 7 Enterprise x86
 
 

I propably forgot to mention that if the source file's folder is x:\temp\test\sub1\file.txt, in the destination the folder 'test' (under this folder, in both source and destination, the directory structure is the same) might be in a different path, e.g. z:\misc\zoo\foo\test\
My System SpecsSystem Spec
17 Jan 2011   #5
Kari

 

This command copies everything, subfolders and files, from C:\Example to D:\Test\Test2\Example, creating the necessary folders:
Code:
robocopy C:\Example\ D:\Test\Test2\Example\ /MIR
The command makes an exact mirror copy (the /MIR switch) of source folder structure to destination, i.e. file
  • C:\Example\Next_Level\And_One_More_Level\Some_File.xxx
would be copied to
  • D:\Test\Test2\Example\Next_Level\And_One_More_Level\Some_File.xxx

Kari
My System SpecsSystem Spec
17 Jan 2011   #6
purpleglow

Windows 7 Enterprise x86
 
 

Quote   Quote: Originally Posted by Kari View Post
This command copies everything, subfolders and files, from C:\Example to D:\Test\Test2\Example, creating the necessary folders:
Code:
robocopy C:\Example\ D:\Test\Test2\Example\ /MIR
The command makes an exact mirror copy (the /MIR switch) of source folder structure to destination, i.e. file
  • C:\Example\Next_Level\And_One_More_Level\Some_File.xxx
would be copied to
  • D:\Test\Test2\Example\Next_Level\And_One_More_Level\Some_File.xxx
Kari

Thanks for the reply Kari,

I can't mirror the whole main folder, because there are lots of files that are only in destination directory, but are not in the source directory. Mirroring would remove the files from the destination folder, right?

So, if I use robocopy like this:
Code:
robocopy c:\Example\Next_Level\And_One_More_Level\Some_File.xxx D:\Test\Test2\Example\ /MIR
Will there be a folder D:\Test\Test2\Example\Next_Level\And_One_More_Level\Some_File.xxx ?

Thanks!
My System SpecsSystem Spec
17 Jan 2011   #7
Kari

 

Quote   Quote: Originally Posted by purpleglow View Post
Thanks for the reply Kari,

I can't mirror the whole main folder, because there are lots of files that are only in destination directory, but are not in the source directory. Mirroring would remove the files from the destination folder, right?

So, if I use robocopy like this:
Code:
robocopy c:\Example\Next_Level\And_One_More_Level\ D:\Test\Test2\Example\Some_File.xxx /MIR
Will there be a folder D:\Test\Test2\Example\Next_Level\And_One_More_Level\Some_File.xxx ?

Thanks!
Yes, if Some_File.xxx were a folder. Both source and destination strings in robocopy can only contain folders, no files. If you need to copy single files, use xcopy instead.

If you don't want to / need to replicate the folder structure, replace the /MIR switch with /S or /E. These two switches do a copy but do not purge destination (delete files). Switch /S copies folders only if they contain files or subfolders, /E copies even the empty folders.

Kari
My System SpecsSystem Spec
17 Jan 2011   #8
purpleglow

Windows 7 Enterprise x86
 
 

Quote   Quote: Originally Posted by Kari View Post
Quote   Quote: Originally Posted by purpleglow View Post
Thanks for the reply Kari,

I can't mirror the whole main folder, because there are lots of files that are only in destination directory, but are not in the source directory. Mirroring would remove the files from the destination folder, right?

So, if I use robocopy like this:
Code:
robocopy c:\Example\Next_Level\And_One_More_Level\ D:\Test\Test2\Example\Some_File.xxx /MIR
Will there be a folder D:\Test\Test2\Example\Next_Level\And_One_More_Level\Some_File.xxx ?

Thanks!
Yes, if Some_File.xxx were a folder. Both source and destination strings in robocopy can only contain folders, no files. If you need to copy single files, use xcopy instead.

If you don't want to / need to replicate the folder structure, replace the /MIR switch with /S or /E. These two switches do a copy but do not purge destination (delete files). Switch /S copies folders only if they contain files or subfolders, /E copies even the empty folders.
If I use robocopy with /E switch, without mirroring, doesn't it also copy the files that are already copied(appear in both source & destination already)?
My System SpecsSystem Spec
17 Jan 2011   #9
purpleglow

Windows 7 Enterprise x86
 
 

Ok, I finally had a chance to do some testing with robocopy and it seems that robocopy skips the files that already exist in the destination folder. Thanks a lot for the help!
My System SpecsSystem Spec
17 Jan 2011   #10
Kari

 

With robocopy you have several methods to include and / or exclude files. For instance:
Code:
robocopy C:\Example\ D:\Test\Test2\Example\ /E /MAXAGE:2
The command copies the exact source folder structure to destination folder, including empty subfolders, excluding files that are over 2 days old.

Here's a complete list of robocopy switches: Batch file copy made easy

Kari
My System SpecsSystem Spec
Reply

 Advanced copying script




Thread Tools




Similar help and support threads
Thread Forum
Windows Script Host: Can not find script file ?
Hello & Thanks , win7 x64 Starting yesterday , I keep getting the following message: Windows Script Host Can not find script file "C:\ProgramData\{95137DD0-C591-AC56-7417-DCD4A4950F5A}\2.0.1.9\lare.txt". Any idea why and how to fix it ? Thanks/ ...Vern Tags: win7 x64, Download Games...
General Discussion
advanced bios
can someone please help me how to open hidden insyde bios advanced settings for hp 635 notebook
General Discussion
Advanced Search?
Hi! Is there any form of advanced search available for Windows 7? I'd need to specify: a) folder I search in b) part of file name c) keyword IN the file contents This was all a piece of cake in XP. How can I do the same under 7?
General Discussion


Our Sites

Site Links

About Us

Find Us

Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

Designer Media Ltd

All times are GMT -5. The time now is 18:14.
Twitter Facebook Google+ Seven Forums iOS App Seven Forums Android App