Batch Help - Merging Folders

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
lukerenfroe
Posts: 4
Joined: 02 Mar 2012 09:49

Batch Help - Merging Folders

#1 Post by lukerenfroe » 02 Mar 2012 09:58

I am attempting to clean up a multimedia folder for multiple users. Currently the folders are like this:

c:\multimedia\user1\music1
c:\multimedia\user1\music2
c:\multimedia\user1\music3

c:\multimedia\user2\music1
c:\multimedia\user2\music2
c:\multimedia\user2\music3

I have about 10,000 users. I would like to run a batch file that will clean this up. I would like to merge all music folders under each user.

I do not know how to do this, but the output should look something like this:
c:\multimedia\user1\music
c:\multimedia\user2\music

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch Help - Merging Folders

#2 Post by foxidrive » 02 Mar 2012 15:23

This is untested.

Copy \user1\music1/2/3 to c:\test\multimedia\ and run this to see if it works as you expect it to.

Any files with the same name in music1/2/3/4 etc will be overwritten.

Code: Select all

@echo off
set d=c:\test\multimedia
for /f "delims=" %%a in ('dir "%d%" /b /ad') do (
pushd "%d%\%%a"
md tempmusic
for /f "delims=" %%b in ('dir music* /b /ad') do (
move /y "%%b\*.*" tempmusic
rd "%%b"
)
ren tempmusic Music
popd
)

lukerenfroe
Posts: 4
Joined: 02 Mar 2012 09:49

Re: Batch Help - Merging Folders

#3 Post by lukerenfroe » 02 Mar 2012 15:36

But what about user2 and the other 10k? I need to keep the folder structures the same... multimedia\user1\music , multimedia\user2\music... etc... I have over 10,000 users.

Not finding much when googling. Kindof discouraged. I don't know what I need, but maybe some type of variable in the filepath that could link the users, ie:

copy "c:\multimedia\*VARIABLE*\music1" "c:\multimedia\*VARIABLE*\music"
copy "c:\multimedia\*VARIABLE*\music2" "c:\multimedia\*VARIABLE*\music"
copy "c:\multimedia\*VARIABLE*\music3" "c:\multimedia\*VARIABLE*\music"
delete "c:\multimedia\*VARIABLE*\music1"
delete "c:\multimedia\*VARIABLE*\music2"
delete "c:\multimedia\*VARIABLE*\music3"

where the *VARIABLE* is user1, user2, user3, user4, etc. The actual usernames in my folder structures are like this: {5D5D23C2-A2D8-49D4-802C-641A1592B158}. With each user having different letter and number combos.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch Help - Merging Folders

#4 Post by foxidrive » 02 Mar 2012 23:20

Yeah, it's designed to do that. You need to test it though, unless you want to backup the 10,000 users and test it on the lot. ;)

lukerenfroe
Posts: 4
Joined: 02 Mar 2012 09:49

Re: Batch Help - Merging Folders

#5 Post by lukerenfroe » 03 Mar 2012 08:54

Awesome! Tested and working. Thank you!

Follow up question:

If I wanted to rename the files in a particular folder to the date modified in this format: 2012.03.03.pdf is this possible?

Thanks again!

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch Help - Merging Folders

#6 Post by foxidrive » 03 Mar 2012 20:46

You do realise that with that name format that you can only have one file of each filetype - because they would have the same name.

You didn't give any indication of folder but this code below should work for the folder it is in.

Code: Select all

@echo off 
setlocal EnableExtensions
:: Date and time routines by Ritchie Lawrence
:: Windows NT4 and above
:begin
(set today=%date%)
set t=2&if "%date%z" LSS "A" set t=1
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('echo/^|date') do (
  for /f "tokens=%t%-4 delims=.-/ " %%d in ('date/t') do (
  set %%a=%%d&set %%b=%%e&set %%c=%%f))
(set yy=%yy%&set mm=%mm%&set dd=%dd%)
for /f "tokens=5-8 delims=:. " %%a in ('echo/^|time') do (
  set hh=%%a&set nn=%%b&set ss=%%c&set cs=%%d)
if 1%hh% LSS 20 set hh=0%hh%
(set hh=%hh%&set nn=%nn%&set ss=%ss%&set tt=%cs%)
if not "%date%"=="%today%" goto :begin

:: The following lines set and echo the timestamp variable
set timestamp=%yy%-%mm%-%dd%_%hh%.%nn%.%ss%
set timestamp=%yy%.%mm%.%dd%
echo timestamp=%timestamp%


 for /f "delims=" %%a in (
 'dir *.* /b /a-d ^|find /v "%~nx0"'
 ) do ren "%%a" "%timestamp%%%~xa"


Change the last line to this if you want this format "orginal filename_2012.03.04.ext"

) do ren "%%a" "%%~na_%timestamp%%%~xa"

lukerenfroe
Posts: 4
Joined: 02 Mar 2012 09:49

Re: Batch Help - Merging Folders

#7 Post by lukerenfroe » 04 Mar 2012 06:30

Thanks for the reply. I reviewed this and it looks like it will be putting a timestamp of today's date. I am looking to rename the files to the "Date Modified".

Thanks!

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch Help - Merging Folders

#8 Post by foxidrive » 04 Mar 2012 08:29

Oh, ok.

Try this - I had it squirreled away from a previous request.
Remove the 'echo' if it looks right, to actually enable the rename.

The order of %%c %%b %%a is probably regional and may need to be adjusted.

@echo off
setlocal
for /f "delims=" %%a in ('dir /a:-d /b') do call :ren "%%a"
pause
goto :EOF

:ren

for /f "tokens=1-5 delims=/-: " %%a in ("%~t1") do set datetime=%%c.%%b.%%a
echo ren %1 "%datetime%%~x1"


Post Reply