Robocopy - Copying folders and files doesn't recreate directory structure - windows

Well, it kind of does but skips one level I think. So, here's folder structure I have (Windows 2008 R2 SP2 standard):
ClientA
|_ClientAfolder1
|_ClientAfolder2
ClientB
|_ClientBfolder1
|_ClientBfolder2
And so forth...
Here's the script I'm using:
for /f "delims=" %%a in ('type "folderlist.txt" ') do robocopy "%%a" "Z:\temp\test" /E /xo /fft /COPYALL /r:0 /w:0
My folderlist.txt looks like this:
f:\ClientA\ClientAfolder1
f:\ClientA\ClientAfolder2
f:\ClientB\ClientBfolder1
f:\ClientB\ClientBfolder2
And after executing the script, my folder structure becomes like this:
ClientAfolder1
ClientAfolder2
ClientBfolder1
ClientBfolder2
So, it skips creating parent folders which are ClientA and ClientB. What am I doing wrong?
Thank you for any help.

for /f "usebackq delims=" %%a in ("folderlist.txt") do (
robocopy "%%a" "Z:\temp\test%%~pnxa" /E /xo /fft /COPYALL /r:0 /w:0
)

Related

Windows Batch File, how to rename and remove part of filename

I am trying to rename a group of files in a directory that all have a part in the filename I want removed later on.
The example of names are:
123876.111thepartIwanttoremove.exe
thisisatestfile.111thepartIwanttoremove.exe
this?392!.111thepartIwanttoremove.exe
thankyouall.222thepartIwanttoremove.exe
test.222thepartIwanttoremove.exe
whatis#d354.222thepartIwanttoremove.exe
My code is:
forfiles /S /M *.111thepartIwanttoremove.exe /C "cmd /c rename #file #fname.doc"
ren ???.111thepartIwanttoremove.* ???.doc
ren ????.111thepartIwanttoremove.* ????.doc
ren ?????.111thepartIwanttoremove.* ?????.doc (and so on)
forfiles /S /M *.222thepartIwanttoremove.exe /C "cmd /c rename #file #fname.jpg"
ren ???.222thepartIwanttoremove.* ???.jpg
ren ????.222thepartIwanttoremove.* ????.jpg
ren ?????.222thepartIwanttoremove.* ?????.jpg (and so on)
So for an example I want the file:
123876.111thepartIwanttoremove.exe
To look like this:
123876.doc
what function can I use to remove the .*thepartIwanttoremove.exe part of the name afterwards without writing so many lines with "?"?
Thank you very much for your help.
Assuming all the files have three parts to the file name that are separated by periods, this style of code should work for you.
for /F "tokens=1-3 delims=." %%G in ('dir /a-d /b *.111*.exe') do rename "%%G.%%H.%%I" "%%G.doc"
for /F "tokens=1-3 delims=." %%G in ('dir /a-d /b *.222*.exe') do rename "%%G.%%H.%%I" "%%G.jpg"

BATCH - Mirrow two Folders in both Ways

I safe some Files at my Local Drive(Laptop) and also have Files saved at my Network Storage at Work.
I want that both Folders have the same Files, If I create/delete or change an File in one Folder, the other Folder should get Updated when I use the BATCH.
It should use the newest Version on an Document.
So I tried it with ROBOCOPY Folder01 Folder02 /MIR /R:3 /W.20, it worked but only in one Way, from Folder01 to Folder02, so if I created an File in Folder02 and used the Batch, the File got deleted.
Then I tried to copy both Folders into one TEMP Folder and then copy the TEMP-Files to both Folders. I used
ROBOCOPY Folder01 TEMP /XO /E /R:3 /W:20
ROBOCOPY Folder02 TEMP /XO /E /R:3 /W:20
ROBOCOPY TEMP Folder01 /MIR /R:3 /W.20
ROBOCOPY TEMP Folder02 /MIR /R:3 /W.20
this was almost perfect, always the newest Document was used and everything was there, but when I delete a File from Folder01, which still exists in Folder02, it will come back next time I use my BATCH.
Sorry for my English
Greetings, Tobias
Try this solution provided by #Sachadee with Xcopy
:://Synchro.bat
:://SachaDee 2014
#echo off&cls
:: We set Folders to synchonized
set "Folders= C:\HackooTest E:\Backup\Folder1 E:\Backup\Folder2 E:\Backup\Folder3"
for %%a in (%Folders%) do (
for %%b in (%Folders%) do (
if not "%%a"=="%%b" (
set "VAR%%a%%b=%%a %%b"
)
)
)
for /f "tokens=2,3 delims== " %%a in ('set VAR') Do (
echo xcopy "%%a" "%%b" /E /D /C /Y /I
)
pause

How to use Robocopy to copy files with TimeStamp in command line

Following is my command to copy the files in my computer and from my computer to network.
ROBOCOPY "K:\Builds" F:\Builds\ /E /COPY:DAT
ROBOCOPY "E:\" "K:\Shan Khan\" /E /COPY:DAT
How i can make timestamp in destination folder only when copying the file for example
"K:\Builds" when copied to F:\Builds\
F:\Builds\ ---> F:\Builds_26092015
"E:\" when copied to "K:\Shan Khan\Workspace"
"K:\Shan Khan\Workspace"---> "K:\Shan Khan\Workspace_26092015"
Kindly note that K drive is password protected and i manually saved the password while mapping the IP address to K drive.
I tried this lines and it works.
it created the directory in such a way
Fri 06_26_2015
for /f "tokens=1* delims=" %%a in ('date /T') do set datestr=%%a
md F:\Builds\"%date:/=_%"
ROBOCOPY "K:\Builds" "F:\Builds\%date:/=_%" /E /COPY:DAT /DCOPY:T

Copy the newest file from all the subdirectories

I am trying to get all the newest file from subdirectories into one network directory with a command without getting the subdirectory structures. They are the SQL Server log files with extension of *.trn. I have the following but it doesn't work.
Trying to get only the newest *.trn files from ...........Backup and it's subdirectories.
for /R E:\SQLSERVER\PRODINSTANCE1\Backup %%f in (*.trn) do xcopy %%f "\\198.152.71.14\NetBackups$\MSSQL\Logs" /B /O:D /d /Y
You can use the dir command with the /od switch and a for loop:
#echo off &setlocal enabledelayedexpansion
for /d /r "E:\SQLSERVER\PRODINSTANCE1\Backup" %%a in (*) do (
for /f "delims=" %%i in ('dir /b /a-d /od "%%~a"') do set "newest=%%~fi"
xcopy "!newest!" "\\198.152.71.14\NetBackups$\MSSQL\Logs" /B /O:D /d /Y
)
For more help enter help dir in the command prompt.
I'm not sure if you're using xcopy correctly. /O doesn't take arguments. It only copies ownership/ACL info. (Is that really what you want for log files?)
Since you didn't describe what "doesn't work" means, my only suggestion is to hedge against file names with spaces in them.
FOR /R "%SRC_DIR%" %%f in (*.trn) do xcopy "%%~f" "%DEST_DIR%" /B /O /D /Y
This worked for me (I tested with .pdf's).

how to traverse specified subfolders in a windows batch file?

Here is my folder hierarchy:
[Numbers]
[Numbers/12545]
[Numbers/12545/dev]
[Numbers/12545/prod]
[Numbers/32445]
[Numbers/32445/dev]
[Numbers/32445/prod]
...
[Numbers/.....]
[Numbers/...../dev]
[Numbers/...../prod]
I want to copy some text files under the only "[Numbers/...../dev]" folders. How should i do?
I tried the below code and it's not work because it coppies under the all subfolders.
for /r %NUMBER_DIRS% %%d in (.) do (
copy %PROJECT_INPUTS%\*.txt "%%d"
)
Thanks.
Try this:
for /d /r "%NUMBER_DIRS%" %%d in (*DEV) do copy "%PROJECT_INPUTS%\*.txt" "%%~d\*.txt"
#ECHO OFF
SETLOCAL
FOR /f "delims=" %%i IN (
'dir /s /b /a:d "\numbers" ^| findstr /i /e "\dev"'
) do ECHO COPY %PROJECT_INPUTS%\*.txt "%%i\"
This will report what the batch PROPOSES to do. Remove the ECHO keyword before the COPY to execute the copy.
Note : you may need to add /y to the copy options if you want to OVERWRITE an existing file in the destination directories.
I presume that you're copying FROM %PROJECT_INPUTS% TO many ...\dev directories.

Resources