1.

Solve : batch for copy and rename?

Answer»

Ok one mroe last thing I need is to find all LogFile.txt from all subfolders and copy it in one folder and add a folder name to file name.
For example.
I have folders

d:\backup\log\Mike\act\LogFile.txt
d:\backup\log\George\act\LogFile.txt
d:\backup\log\John\act\LogFile.txt
d:\backup\log\Suzy\act\LogFile.txt


And now batch will copy all those renamed LogFile.txt to
d:\backup\AllAct\MikeLogFile.txt
d:\backup\AllAct\GeorgeLogFile.txt
d:\backup\AllAct\JohnLogFile.txt
d:\backup\AllAct\SuzyLogFile.txtTest this:

Code: [Select]echo off
md "d:\backup\AllAct\" 2>nul
for /f "delims=" %%a in ('dir /a-d /b /s "d:\backup\log\*.txt" ') do (
for /f "delims=" %%b in ("%%~dpa\.") do copy "%%a" "d:\backup\AllAct\%%~nxb%%~xa"
)
pauseI tried this but it makes one file and fills up that file with all log files.
I have also log files in

d:\backup\log\Mike\LogFile.txt
d:\backup\log\George\act\LogFile.txt
d:\backup\log\John\LogFile.txt
d:\backup\log\Suzy\LogFile.txt

whic are the same name so it must be only LogFile.txt from this folder, it is subfolder (act) of Users Folder
d:\backup\log\Mike\act\LogFile.txt
d:\backup\log\George\act\LogFile.txt
d:\backup\log\John\act\LogFile.txt
d:\backup\log\Suzy\act\LogFile.txt

I can make that file won't be the same to rename it when it is copied to act subfolder by adding some extra charters if that makes easier?I see why my suggestion doesn't work - I missed seeing the \act\ part.

Test this: it expects a single log file in each \act\ folder under each user folder.

Code: [Select]echo off
cd /d "d:\backup\log"
for /d  %%a in (*) do for %%b in ("%%a\act\*.log") do copy "%%b" "d:\backup\AllAct\%%a.log"

nothing happends
Change .log in TWO places to .txt

foxydrive gave you a complete solution. Save this code as a batch file, and run it in the root directory of drive D:\ (assuming that the "backup" directory is there).

I just added "LogFile" to the code as you WANTED it to look like.

Code: [Select]echo off
cd /d "d:\backup\log"
for /d  %%a in (*) do for %%b in ("%%a\act\*.txt") do copy "%%b" "d:\backup\AllAct\%%aLogFile.txt"yes, thanks, I didn't see that log and txt. I changed that too, but still nowthing happendsbump
this unfortunally not working Code: [Select]echo off
for %%a in (
d:\backup\log\Mike\act\LogFile.txt
d:\backup\log\George\act\LogFile.txt
d:\backup\log\John\act\LogFile.txt
d:\backup\log\Suzy\act\LogFile.txt
) do md "%%~dpa" 2>nul &break>"%%a"
md "d:\backup\AllAct\" 2>nul

echo off
cd /d "d:\backup\log"
for /d %%a in (*) do for %%b in ("%%a\act\*.txt") do copy "%%b" "d:\backup\AllAct\%%a%%~nb.txt"
pause
Sorry this won't work, because I have added users and will add some more users.
With fixed folders or paths I can do with robocopy QUOTE from: Blisk on September 02, 2014, 05:10:33 AM

Sorry this won't work, because I have added users and will add some more users.
With fixed folders or paths I can do with robocopy

It solves the question as you asked it, with the details you provided.
The code above the second echo off simply CREATES some files where you said they were so that it can show you that the code works.

More users isn't a problem, and it doesn't use fixed folders in the way you obviously think it does.

So I don't need to change batch file everytime I add a user folder?


Discussion

No Comment Found