I am trying to write a batch to copy all files (those in subfolders too) from directory1 to directory2 and then moving any extra files (or directories if possible) found in directory2 to directory3.
In other words, I am trying to mirror directory2 with directory1 but instead of the purge action, moving the extra files to directory3...
So far, I've been able to get those extra files in a text file... Now, I don't know how to proceed...
ROBOCOPY directory1 directory2 /E /COPYALL /R:0 /XO /X /tee /njh /njs /np /ns /nc /ndl > Extrafiles.txt
Further, the Extrafiles.txt does contain a lot of extra spaces at start of each file path that means a little string manipulation before moving the file. And which command to use to move the files by automatically creating the target directory if it does not exist?
Sample output of Extrafiles.txt
directory2\File1.txt
directory2\subdirectory2\deeper\File2.txt
Any help will be appreciated. Thank You.
Making it a little more clear:
Suppose directory1 contains:
\subdir1
file4.txt
file5.txt
\deeper
file6.txt
file7.doc
file1.txt
file2.txt
and directory2 already contains:
\subdir1
file4.txt
file007.ppt
\deeper
file6.txt
file7.doc
\deepest
file155.txt
file1.txt
Then I want these files to be copied:
directory1\file2.txt to directory2\file2.txt
directory1\subdir1\file5.txt to directory2\subdir1\file5.txt
And then these files to be moved:
directory2\subdir1\file007.ppt to directory3\subdir1\file007.ppt
directory2\subdir1\deeper\deepest\file155.txt to directory3\subdir1\deeper\deepest\file155.txt
Hope I made it clear enough :)
You can use for to iterate through the contents of the file generated by robocopy (see for /?).
Then you can use delayed expansion and variable substitution to generate the target path (see set /?), and finally call a subroutine that will do mkdir if needed followed by move (see call /?).
A draft solution would be:
#echo off
setlocal enabledelayedexpansion
set srcdir=directory2
set tgtdir=directory3
for /f "usebackq tokens=*" %%A in ("Extrafiles.txt") do (
set srcpath=%%A
set tgtpath=!srcpath:%srcdir%=%tgtdir%!
call :mkdirmove !srcpath! !tgtpath!
)
goto :EOF
:mkdirmove
if not exist %~dp2nul mkdir %~dp2
move %1 %2
goto :EOF
Related
when it comes to creating a batch-file I would sadly call myself a newbie and therefore it's kind of difficult for me to achieve what I want on my own:
so here is how my codes look:
#ECHO OFF
for /r "C:\source" %%f in (*) do copy /y "%%f" C:\ReadyToExport
del C:\ReadyToExport\*.pdf*
for /f "delims=" %%i in ('xcopy "C:\ReadyToExport" C:\import /EXCLUDE:LogFile.log /S /E /D') do (
echo %%i >> C:\LogFile.log)
PAUSE
and here is what the code exactly does
1- for /r "C:\source" %%f in (*) do copy /y "%%f" C:\ReadyToExport
this line copies all files from source to another target folder the good thing is that this command copies all the files inside other subfolders from source and paste them into the folder C:\ReadyToExport without any subfolders.
2- del C:\ReadyToExport\*.pdf*
this command filters all the .pdf files because they are unnecessary
3-
for /f "delims=" %%i in ('xcopy "C:\ReadyToExport" C:\import /EXCLUDE:LogFile.log /S /E /D') do (
echo %%i >> C:\LogFile.log)
this command basically copies all files from "C:\ReadyToExport" into C:\import and writes the name of the recorded file in LogFile.log then the copied files will be excluded when the script runs again because I don't want to copy the files again if I already copied them before
and here is what I want to achieve:
I want to copy only modified files from the folder "C:\ReadyToExport" to the target C:\import but keep in mind that
the files in the target folder "C:\import" will be deleted but since the names of the files that have already been copied are registered in LogFile.log the files will be excluded and not copied again.
in another word: files in target file do not exist anymore but their names are written in LogFile
so is there any way to copy only modified ones? even though they don't exist in the target folder anymore? but their names are in LogFile.txt? can the script somehow write the last modified date in LogFile.txt near the file name? and then compare the date from the source? so it copies only files that have been changed and ignore all files that didn't?
p.s: using Xcopy, robocopy, etc is not a problem
any answer will be appreciated.
thnx
your method does, in fact, works so thank you so much not the way the I want with exclude:logfile.log
but this definitely worked:
robocopy C:\ReadyToExport\ C:\import /M /E
since it copies only the files that hast the attribute archivable checked in the settings.
I am using the following script lines in a batch script (.bat) to copy the contents of a directory (foo) to another (bar):
move "C:\foo\*.*" "C:\bar\"
for /d %%a in ("C:\foo\*") do move "%%~fa" "C:\bar\"
The first line moves files and the second lines moves folders. However, these aren't moving the hidden directories. .git is a common example. I tried for /d %%a in ("C:\foo\.*") do move "%%~fa" "C:\bar\" with no success.
How can I move my hidden directories along with the rest of my files and directories?
EDIT: The following solution is very close to doing what is required, but fails because the "move" command can't find the hidden folder (tried the same on a .folder that wasn't hidden and it worked):
for /f "tokens=*" %%G in ('dir /b /a:hd "C:\foo\*"') do move "C:\foo\%%G" "C:\bar\"
For what you are trying to do, you can first use the attrib command before moving things to remove hidden attributes from files. You can use this to accomplish your goal:
attrib -h "C:\Program Files\Git\usr\tmp\*.*"
move "C:\Program Files\Git\usr\tmp\*.*" "C:\Program Files\Git\usr\bin\"
To do this with other things, you can do this:
attrib -h "<SourceParentFolder>\*.*"
move <source> <destination>
NOTES: You should note #Mark's comment. Using C:\file\path\folder\* is not correct. You should use C:\file\path\folder\*.*. For more information view #Mark's comment
For more information on attrib use attrib /? or check this
A file or directory name beginning with . does not mean that it is hidden.
Anyway, for/for /D iterates over non-hidden files/directories. However, dir allows to return hidden items as well when using its /A option, which can be made use of by using for /F:
rem // Change into source directory:
pushd "C:\Program Files\Git\usr\tmp" && (
rem // Iterate over all directories, even hidden and system ones:
for /F "delims= eol=|" %%I in ('dir /B /A:D-S-L ".*"') do (
rem // Actually move the directory:
move "%%I" "C:\Program Files\Git\usr\bin\"
)
rem // Return from source directory:
popd
)
After some extra research, I found that robocopy seems to be included by default in Windows 10 distributions and robocopy /MOVE allows moving all the needed files and folders in a single line, such as:
robocopy "C:\foo" "C:\bar" /E /MOVE
Additional logging options can be added to reduce the output to the command line.
I'm trying to use ROBOCOPY to move all files, folders, and subdirectories from one location to another. There are some files that SHOULD NOT be moved. I want to store a list of these files that are NOT to be moved in a text file.
What syntax would the robocopy be for
1. The robocopy command?
2. The syntax of the files for exclusion. As in does the path need to be absolute?
I have looked around but all the posts seem to tip toe around the idea
ROBOCOPY ^
"C:\SOURCE" ^
"C:\TARGET" ^
/E
ExclusionList.txt
-file1.txt
-file2.txt
If i just need /XF pointing to the exclusion file?
or does /XF point at each exlcuded file?
or do I have to do this with the :
/JOB:jobname : Take parameters from the named JOB file.
but how do I use /JOB.... would it be a text file with the extension .JOB... and if so how can I use variables from there.
Thanks,
If you don't have very many files for exclusion, then you may be able to generate the list as a variable to include in the /XF option.
#Echo Off
SetLocal EnableDelayedExpansion
Set "exList=C:\Users\The_BMan\Desktop\ExclusionList.txt"
Set "xFiles="
For /F UseBackQDelims^=^ EOL^= %%A In ("%exList%"
) Do If Not Defined xFiles (Set xFiles="%%~A") Else Set xFiles=!xFiles! "%%~A"
Echo( /XF %xFiles%
Timeout -1
Currently running a backup with batch file using robocopy and using the /log command to write the output to a text file. The log file only contains the folder path currently and would like to remove everything except the file name that was copied over.
robocopy "C:\Junk" "H:\Junk"/s /mov /log:"c:\New Junk\Junk (%date:~-10,2%-%date:~-7,2%-%date:~-2,2%).txt" /NDL /NJH /NJS /NS /NC /NP
Eventually I'll have multiple text files that will need to be searched for the path "C:\Junk" to be removed from them. I've tried Findstr, but with horrible results. Any help would be appreciated
I was able to solve the issue by using a for loop with the /r switch. Here's the simple code
#ECHO OFF CD C:\Junk FOR /R %%G in (*.txt) DO ECHO %%~nG >> "H:\New Junk\Junk (%date:~-10,2%-%date:~-7,2%-%date:~-2,2%).txt" PAUSE
And then running the robocopy command without the
I'm trying to tidy up a data folder and have written a batch file to take care of a lot of preliminary work (delete empty folders, delete junk files, etc), but I'm falling over when trying to deal with files within duplicate folders.
This is an example of the current situation:
w:\Data\Corporations\555\20130101\Concat_000001\555_20130101_data.zip
w:\Data\Corporations\555\20130101\Concat_000002\555_20130101_data.zip
w:\Data\Corporations\555\20130101\Concat_000003\555_20130101_data.zip
w:\Data\Corporations\555\20130101\Concat_000004\555_20130101_data.zip
There should only be one Concat folder per YYYYMMDD folder, and should look like this:
w:\Data\Corporations\555\20130101\Concat\555_20130101_data.zip
There are hundreds of folders in w:\Data\Corporations to be processed, so I figure I need to first of all find any folder named Concat_*, make a folder named Concat within the same parent folder, and then move any zip from Concat_ to Concat.
I have tried various combinations of FOR /D in (Concat_*) with MD and MOVE commands, but with no luck so far. I've also tried calling a subroutine from the FOR statement that would jump back a level in the tree, create a folder named Concat, go back to Concat_* and move the .zip files, but again with no luck.
Any help would be greatly appreciated.
Cheers,
Pete
try this:
for /r "w:\Data\Corporations\555" %%a in (*.zip) do for %%b in ("%%~dpa.") do md "%%~dpbConcat" 2>nul & move /y "%%~fa" "%%~dpbConcat"
The following does what you asked. If you uncomment the last line, then empty config_* folders will be removed. Non-empty config_* folders will be preserved.
#echo off
for /f "delims=" %%F in ('dir /b /ad /s concat_*') do (
if not exist "%%~dpFconcat\" mkdir "%%~dpFconcat\"
if exist "%%F\*.zip" move /y "%%F\*.zip" "%%~dpFconcat\" >nul
REM uncomment line below if you want to remove empty concat_* folders
REM dir /b "%%F"|findstr "^" >nul || rd "%%F"
)