how can I copy the files in path.txt to new folder with same folder structure? - batch-file

how can I copy the files in path.txt to new folder with same folder structure?
in my path.txt,there are some file paths. I want to copy these file to a folder which keeping the same folder structures. how to do the job using bat common?
includes/functions/extra_functions/functions_common.php
includes/functions/html_output.php
includes/templates/jy_default/part/list_content_products/list_content_products.php
includes/templates/jy_default/part/product_info_also_like/product_info_also_like.php
includes/templates/jy_default/part/product_info_main_image/product_info_main_image.php
subam/css/global.css
subam/includes/classes/class.jy_csv.php
subam/includes/main_page/sub_categories.php
subam/sass/bootstrap/_forms.scss

As your list does not have an absolute path you need to execute this batch file from the folder that contains your file.txt as well as the folders that are listed in file.txt
It's not tested: and your forward slashes should be backslashes but Windows will handle either.
#echo off
for /f "delims=" %%a in (file.txt) do (
xcopy "%%a" "d:\target folder\%%~pa\"
)

I see you tagged BATCH and CMD. Do you know any C#? The following .cs script should work and will give the option for a nice GUI.
In C# something like this would be very simple, you could use a while statement to loop the items in the list. While looping, you can concatenate the file/folder names to a location path of your choice and then copy the files to this location :
int counter = 0;
string line;
string concat_path = "C:\\my_new_folder\"
// Read the file
System.IO.StreamReader file = new System.IO.StreamReader("c:\\your_text_file.txt");
while((line = file.ReadLine()) != null)
{
//copy the file, to the concat location, with the other folder names in place
//notice i have added "C:\" before the line, as it appears in your text doc you dont use full file paths, make sure you add the correct locaiton so that they can be found in sub folders
System.IO.File.Copy("C:\" + line, concat_path + line, true);
counter++;
}
//close the file open
file.Close();

You dont't need to read a list from a file. You could simply try this in command prompt:
xcopy "full path to your folder" "full path destination folder" /e
Below is what you get if you type xcopy /? in command prompt:
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]
[/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 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.
The switch /Y may be preset in the COPYCMD environment variable.
This may be overridden with /-Y on the command line.

Related

How to copy files inside a folder without other folders in batch?

I am having the following folder structure:
Folder1
-Folder2
-File1
-File2
-File3
I have tried the following in batch script to copy all inside the Folder1
echo d | xcopy "Folder1\*.*" "DestinationFolder"/f /s /y /r
But I need to copy only File1, File2, File3. Need to ignore Folder2. I don't have any idea about how to achieve this. Please help me resolve this.
If Folder2 is empty, /S will do,
but since you have /s in your command, so I guess it's not empty.
So use this:
echo \Folder2\>__tmp4Exclude__
echo d | xcopy "Folder1\*.*" "DestinationFolder" /f /s /y /r /EXCLUDE:__tmp4Exclude__
del __tmp4Exclude__
__tmp4Exclude__ is a temp file created to contain the list of files to exclude while copying.
From xcopy /?:
/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.
There is no need to use xcopy to copy just files! xcpoy is usually used to copy directory trees.
So, use copy instead. You can do:
copy "Folder1\*.*" "Destination\"
Probably you may need to use /(-)Y option:
/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.
From copy /? (copy help page)

Create batch file for copy all the contained in root folder (including folder and files) to another folder

I have a batch file to test copy all the contain of the folder to another folder as below:
XCOPY /S /Y "C:\Windows\Source\1\*.*" "C:\Windows\Source\O365PP\Office\Data"
But, when i run it, It will never copy a single files from the source and the cmd screen was shown and won't disappear. Kindly advice.
Thank you
First, you'll have to tell XCOPY if the destination is a File or Directory:
C:\Scripts>xcopy /s /y c:\Scripts\Test\*.* c:\scripts\mine
Does C:\scripts\mine specify a file name or directory name on the target
(F = file, D = directory)?
In this case, adding the switch /I would help.
/I If destination does not exist and copying more than one file,
assumes that destination must be a directory.
Also, double quotes are not needed UNLESS your filenames have a space.
#franklin is correct saying the destination needs the \*.*

How to copy files from a folder tree to a single folder and only the latest files?

How to copy files from a folder tree to a single folder and only the latest files using batch commands?
These copies must happen every one hour and avoid overwrite rather than copy only latest files.
Current command:
for /R "D:\Logshipping\NDWAnalyzer\" %%f in (*.*) do copy "%%f" "D:\LogShipping1\NDWAnalyzer\"
The above command copies every time whole files in the folder rather than I need only to copy the latest - the files which are not present in the destination folder.
This is an easy task using xcopy with parameter /M which results in copying only files with archive attribute set and clearing this attribute after copying. The archive attribute is automatically set for a new or modified file.
#echo off
rem Check before copying for deletion of files in destination directory
rem which were copied already before and not updated in the meantime, i.e.
rem no archive attribute set anymore on file, but file is also not existing
rem anymore in destination directory. The archive attribute is set temporarily
rem on those files before running xcopy. This helps also in case of renaming
rem a file in one of the source directories which was copied already before
rem with previous name to destination directory as the rename operation does
rem not set archive attribute.
for /R "D:\Logshipping\NDWAnalyzer" %%F in (*) do (
if not exist "D:\LogShipping1\NDWAnalyzer\%%~nxF" %SystemRoot%\System32\attrib.exe +a "%%~F"
)
rem Copy from all directories in D:\Logshipping\NDWAnalyzer all files with
rem archive attribute set (= modified or added since last run) to directory
rem D:\LogShipping1\NDWAnalyzer with clearing the archive attribute after
rem copy and with copying also all attributes including read-only attribute.
for /R "D:\Logshipping\NDWAnalyzer" %%D in (.) do (
%SystemRoot%\System32\xcopy.exe "%%~fD\*" "D:\LogShipping1\NDWAnalyzer\" /C /H /I /K /M /Q /R /Y >nul
)
It is up to you if you want to use first + second loop or just second loop.
With removing first loop it would be possible to delete files in destination directory still existing in a source directory or rename a file in a source directory which was copied already before to destination directory without being copied once again.
For more details on the used commands open a command prompt window and run
attrib /?
for /?
if /?
xcopy /?
A help is output for each command which should be read to understand the two loops above.

Move all files except some (file pattern) from a DOS command

From a DOS command I want to move all files that do not match a file name pattern.
Something like this:
For example I want to move all files that do not start with "aaa"
for %i in (*) do if not %i == aaa* move %i .\..
XCOPY is designed to work with 'exclude' lists... See below:
dir /b /a-d "source"|findstr /b "aaa" >"%temp%\aaafiles.tmp"
xcopy "source" "destination\" /exclude:%temp%\aaafiles.tmp /y
The first line performs a DIR (directory) listing of the source folder, listing files in bare format (/b) ignoring directory names (/a-d). The output is piped into the FINDSTR command.
FINDSTR looks at each filename and compares it's beginning (/b) with the string "aaa".
If the start of a filename matches the string "aaa", the filename is redirected (written) to the file aaafiles.tmp in the users TEMP directory.
The /b is vital because you don't want to exclude files such as theaaafile.txt.
The XCOPY command copies files from the source folder to the destination folder except files that are listed in aaafiles.tmp.
Prompting to overwrite existing files (/y) is turned off.
source and destination will need to be replaced your own foldernames.
Robocopy is a possibility
robocopy source destination *.* /mov /XF aaa*.*
for options see here http://technet.microsoft.com/en-us/library/cc733145.aspx
If you don't mind fiddling with the archive bit, you can use it to selectively copy and delete files based on a file mask.
Move (copy and delete) all files except those beginning w/"aaa" from current directory to "dest". May also specify full source path.
attrib +a *.*
attrib -a aaa*.*
xcopy /a [or /m] *.* dest
del /aa /q *.*
One way you can do it is create a list of the files to move in a temporary file. Then use the file in with the for command. Generate the list using findstr.
> dir/b/a-d | findstr /v aaa* > "%temp%\#movelist"
> for /f %f in (%temp%\#movelist) do move %f ...
The first command gets a list of all files (with no directories) in the current directory and then pipes the list to findstr which excludes (/v) filenames that match the pattern and puts it in the file #movelist in the temp directory. The second command just takes those results so you may do what you will with them (move it).
There's probably a better way to do it in a single command without the temporary file, I just don't know how to write it. I'm not sure how to call the dir command from within the for command. AFAIK it only takes program files that exist, not builtin commands.
In some cases it can be made more simple. For example, I had to copy recursively a bunch of directories but excluding all images (png and bmp files), so I simply created an excludeList.txt file containing:
.png
.bmp
and run
xcopy /S /I <source> <dest> /exclude:c:\excludeList.txt
It will match any file or directory containing .png, but not necessarily ending by .png. (I did not investigate if smart use of wildcards or regular expressions are possible).
It does not handle your particular example (for which you have already a good answer) but it solved my problem, and this page is what I found when I googled in search of a solution :)
Not ideal, but moving all files to the destination and moving the files back to the source is a fast way with actual move operation (no copies). Of course this assumes there are no files in destination matching the wildcard.
move source\*.* destination\ && move destination\aaa*.* source\

how to copy a directory over another through Batch except from a specific file or file type

I need to copy a file directory for a web site from one server to another when a change have been made i have this part working fine.
But i need to copy all the file except the web.config.
Is there any way in batch to copy a whole directory except a specific file or file type?
Any help is appreciated.
Kind Regards,
Berty
xCopy might help, with the exclude option
c:\>xcopy /?
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]
[/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 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.
The switch /Y may be preset in the COPYCMD environment variable.
This may be overridden with /-Y on the command line.

Resources