Alternative to multiple robocopy commands iterating the same directory - cmd

I have a list of file extensions I need to collect all the files of from a particular directory while maintaining attributes, timestamps etc. which has resulted in my usage of Robocopy. I'm hoping someone can suggest a more efficient method than my current solution?
At present I copy these files using the following command into an "unprocessed" directory:
robocopy %Directory1% "%Directory2%\unprocessed" /Z /E /copy:dat *.pst *.ost *.doc *.docx *.pdf *.docm *.xls *.xlsx *.ppt /log+:%Directory%.txt
The individual collected files from inside the "unprocessed" directory are then sorted to individual folders named after each file extension and created at the same level as the "unprocessed" directory, again using individual Robocopy commands:
robocopy "%directory2%\unprocessed" %directory2%\pst *.pst /Z /E
...
...
robocopy "%directory2%\unprocessed" %directory2%\ppt *ppt /Z /E
As you can appreciate, this results in unnecessary iterations of the "unprocessed" directory multiple times. I'm unable to copy files straight to the final resting places due to the nature of robocopy so I'm hoping someone can suggest a more suitable solution that will still allow file attributes to remain intact.
(I apologise in advance that this could turn into a discussion as opposed to someone being directly able to answer this)

Given the % signs in your examples, it looks like you're probably running this as part of a batch file. Either way, if you have the environment variables defined, you could use the following:
Command line:
for %e in (pst ost doc docx pdf dcom xls xlsx ppt) do robocopy "%Directory1%" "%Directory2%\%e" /Z /E /copy:dat *.%e /log+:%Directory%.txt
Bat file:
for %%e in (pst ost doc docx pdf dcom xls xlsx ppt) do robocopy "%Directory1%" "%Directory2%\%%e" /Z /E /copy:dat *.%%e /log+:%Directory%.txt
One other thing to note: You'll probably end up with all docx files in both doc and docx, likewise xlsx would end up in both xls and xlsx. Windows always picks up longer extensions with 3 character extensions.

Related

XCOPY crashing when I try to copy and rename 80K files. Is there a better way?

Probably there is a better approach to fix what I am trying to do. I have a folder with pictures that I want to copy to another folder renaming them.
Basically, I have a list of all the 80K files of the file name they have, and what is the new name that they should have. There is no fixed pattern to rename the files. What I’ve been doing is using Excel to find the path and current file name and then renaming the files checking the new file name in a SQL database.
Basically, for every file I form a XCOPY line then I paste them on powershell.
echo F|xcopy /s /q /y /f "file path" "renamed file path"
My problem is that for some reason powershell stops processing the xcopy lines after approximately 4K files. I already tried running this as a BAT with these lines for each file.
xcopy /s /q /y /f "file path" "renamed file path *" with an asterisk at end to avoid the question if it’s a file or a folder. But it stops processing after 2K files.
I know that robocopy is better than xcopy but I need to rename each file, and there is not a clear pattern in the renaming, I don’t know how to do a robocopy each file and then rename each file, but that should be even worse no?
Also, the server has a good SSD and its only processing 1 xcopy line per each second, should be a lot faster. The .jpg files are small (˜200Kb).
Any ideas are appreciated.
Regards,
Tiago

Move only .xls files not .xlsx, .xlsx, .xlsb

I'm looking to move only .xls files out of a directory (including subfolders) and not .xlsx or .xlsm files, I found a command that I thought would do the job and it works fine if I specify .xlsx and will only pull through files with that extension.
But when specifying .xls it pulls through .xls, .xlsx, .xlsm and .xlsb even though I don't have a trailing wildcard.
for /r "c:\source" %x in (*.xls) do move "%x" "c:\destination"
I'm not that familiar with this so I'm probably just missing something obvious but have tried a search and cannot find anything to resolve this issue.
Any help would be appreciated. Thanks
The *.xls pattern is checked against both the long file names that are visible to the user and the short 8.3 file names that exist in the background by default and are intended for backwards compatibility. The short file names have extensions of 3 characters only, so a file with the long name longfilename.xlsx has a short file name extension .xls.
To overcome this behaviour, the easiest solution is to establish an additional filter by findstr:
for /F "delims= eol=|" %x in ('dir /S /A:-D /B "C:\source\*.xls" ^| findstr /I "\.xls$"') do move "%x" "c:\destination"

Xcopy certain file names only

I've never used the Xcopy feature before and I was wondering if it would be possible to use xcopy to copy only certain files within a directory tree.
For example, suppose I have the following documents:
\servername\generateddocuments\2014\20141231\GUID1.doc
\servername\generateddocuments\2014\20141231\GUID2.doc
\servername\generateddocuments\2015\20150101\GUID3.doc
\servername\generateddocuments\2015\20150101\GUID4.doc
Now, suppose I have a spreadsheet that tells me which .doc files I need to copy:
GUID1.doc
GUID3.doc
Is there a way to base the xcopy on the spreadsheet(or txt document) so I don't copy the files I don't need?
I don't think xcopy can read files to include from a file. But you can create a batch file that does this:
for /F "tokens=*" %%A in (documents.txt) do (
copy %%A x:\targetfolder\
)
Try typing HELP XCOPY at a command prompt and look at the /EXCLUDE parameter. The documentation isn't quite correct, you can put a list of files in a single file, one file name per line, and they will be excluded from the xcopy.

converting xcopy to robocopy

Tried to use xcopy and excluding a folder and all its subfolders.
C:\Merged\org\a>xcopy /I /E /Y C:\Merged\org\*.* C:\Merged\dest /exclude:"C:\Mer
ged\org\a\*.*"
Can't read file: "C:\Merged\org\a\*.*"
I guess I cannot exclude folder, but only file with xcopy.
So I think of moving to robocopy, but I'm not sure which flags to use.
What is the equivalent robocopy to my above xcopy ?
Well, you can. Read up on 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.
The problem is you specify the path you want to exclude to /EXCLUDE when you should specify the name of a file, which contains the "paths" to exclude (it is actually more flexible see above). The error message you get even hints at that.
Create a file, e.g. C:\ignore.txt, that contains the line "C:\Merged\orga\a\", then invoke XCOPY as follows:
xcopy.exe C:\Merged\org\*.* C:\Merged\dest /exclude:C:\ignore.txt /I /E /Y
If you want to use robocopy nevertheless, you can do so like:
robocopy.exe c:\merged\org c:\merged\dest /xd c:\merged\org\a /IS /E
I has been wrote seemless wrappers from xcopy command line to the robocopy. You can find it from here: https://github.com/andry81/contools.
The scripts:
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/xcopy_dir.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/xcopy_file.bat

Copy a file to all folders batch file?

I need copy credits.jpg from C:\Users\meotimdihia\Desktop\credits.jpg to D:\Software\destinationfolder and all subfolders
I read many and i write
/R "D:\Software\destinationfolder" %%I IN (.) DO COPY "C:\Users\meotimdihia\Desktop\credits.jpg" "%%I\credits.jpg"
then i save file saveall.bat but i run it , it dont work at all.
help me write 1 bat
Give this a try:
for /r "D:\Software\destinationfolder" %i in (.) do #copy "C:\Users\meotimdihia\Desktop\credits.jpg" "%i"
Of course, if it's to go into a batch file, double the '%'.
This was the first search I found on google for batch file copy file to all subfolders.
Here's a way with xcopy.
There's also robocopy but that would be too powerful for a simple task like this. (I've used that for entire drive backups because it can use multi-threading)
But, let us focus on xcopy.
This example is for saving to a file with the extension .bat. Just drop the additional % where there is two if running directly on the command line.
cd "D:\Software\destinationfolder"
for /r /d %%I in (*) do xcopy "C:\temp\file.ext" "%%~fsI" /H /K
cd "D:\Software\destinationfolder" change directory to the folder you want to copy the file to. Wrap this in quotes if the path has whitespaces.
the for loop - See help here. Or type for /? in a command prompt.
/r - Loop through files (recurse subfolders)
/d - Loop through several folders
%%I - %%parameter: A replaceable parameter
xcopy - Type xcopy /? in the command line for lots of help. You may need to press Enter to read the entire help on this.
C:\temp\file.ext - The file you want to copy
"%%~fsI" - Expands %%I to a full pathname with short names only
/H - Copies files with hidden and system file attributes. By default, xcopy does not copy hidden or system files
/K - Copies files and retains the read-only attribute on Destination files if present on the Source files. By default, xcopy removes the read-only attribute.
The last two parameters are just examples if you're having trouble with any read-only files and will retain the most important file properties.
Lots more xcopy parameters here
xcopy examples here
Just for completeness. This example below will copy the same file in each folder of the current directory and not any sub-folders. Just the /r option is removed for it to behave like this.
for /d %%I in (*) do xcopy "C:\temp\file.ext" "%%~fsI" /H /K
If you can use it: Here is a PowerShell solution (PowerShell is integrated in Windows 7 and available from XP and up):
$file = "C:\...\yourfile.txt"
$dir = "C:\...\YourFolder"
#Store in sub directories
dir $dir -recurse | % {copy $file -destination $_.FullName}
#Store in the directory
copy $file -destination $dir
I'm pretty sure that the last line can be integrated in dir ... but I'm not sure how (I do not use PowerShell very often).

Resources