Move files to multiple directories - batch-file

I have the below cmd line which moves the dll files from one folder to another.
for /r "myoldfolder" %x in (*.dll) do copy "%x" mynewfolder /y
How can I make it look to a destination of not just mynewfolder but all the directories listed by the "where python" command?

Related

Coping files from multiple directories into one folder

I have many pictures in multiple directories and want to copy them to one folder, but my script is interrupted when a file with the same name already exists in the destination folder. I tried
for /R d:\dups %f in (*.jpg) do copy "%f" d:\pictures\.
What can i add to the code to append the name of the newer file before it's copied to the the destination folder? My source directory is "d:\dups" and my destination folder is "d:\pictures". thanks!
example:
#echo off
for /R "d:\dups" %%f in (*.jpg) do copy "%%f" "d:\pictures\New-%%f"

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

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.

copy all files recursively into a single folder (without recreating folders)

With a batch (.bat), I want to copy all mp3 files that are in 1 subdirectory of D:\TEMP
D:\TEMP\\(anyfolder)\\(anyfile.mp3)
to
E:\MYFOLDER\
I tried with xcopy but
I don't know how to tell "just recurse subfolders of D:\TEMP and not subsubfolders, subsubsubfolders, etc."
When using xcopy, folders are created in the destination (in order to replicate source's folder tree), I don't want this : files should be copied in just 1 single folder.
for command is your friend. Read help for and then try this in the command prompt
for /d %a in (*) do #echo %a
as you see, it follows all subfolders in the current directory.
thus,
for /d %a in (*) do #copy %a\*.mp3 e:\myfolder
will copy all your mp3 to the destination folder.

Batch file loop to grab all folders in a directory except for a certain folder

Just wondering the best way to do this in a single batch file. I have a folder called C:\Program\Foo and I want to grab all the folders except for the testing folder inside of foo, and I want to xcopy into D:\ so in D:\ foo will be there but no test folder.
Is there a way I can loop through each folder and check for a certain name not to include?
using /Exclude would mean I would need an extra text file with "Testing" in it
I don't see why you could not create a temporary exclusion file (using a temporary folder, that is):
#ECHO OFF
FOR %%F IN ("%TEMP%\exclude.txt") DO SET tmpf=%%~sF
ECHO Testing>%tmpf%\exclude.txt
XCOPY source destination /EXCLUDE:%tmpf%\exclude.txt other options
Note: XCOPY does not recognise double quotes as path delimiters in the /EXCLUDE option and offers no alternative for specifying paths with spaces, which can be a problem on Windows XP systems. This limitation can be worked around by replacing the original path with its counterpart consisting only of short names. That is what the FOR loop in the above script does for the %TEMP% folder.
Can you use ROBOCOPY?
ROBOCOPY C:\Program\Foo D:\ * /E /XD Test
/E copies subfolders and files
/XD excludes directories
Use the EXCLUDE option and put your exclusions in that file. That will let you exclude entire directories.
http://www.pcreview.co.uk/forums/using-xcopy-backup-can-exclude-some-directories-t489674.html
xcopy "c:\document and settings" "i:\documents and settings\" /s /d /EXCLUDE:c:\a.txt
a.txt contains
\temp\
\temporary internet files\
You may need to use shorter DOS file names.

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\

Resources