My network directory file synch batch file thing

Click For Summary
SUMMARY

This discussion focuses on creating a batch file to synchronize MP3 directories between a laptop and an HTPC. The user mapped their HTPC's MP3 drive to the E drive on their laptop and sought a solution to copy only new files from their C drive without overwriting existing ones. The solution involved using a FOR statement in a batch file to check for existing files and create directories as needed. The user also mentioned alternative solutions like Subversion and Linux commands for file synchronization.

PREREQUISITES
  • Batch file programming
  • Windows command line operations
  • File system navigation and management
  • Basic understanding of directory structures
NEXT STEPS
  • Explore advanced batch file techniques for automation
  • Learn about file synchronization tools like rsync for Linux
  • Research version control systems such as Git for collaborative projects
  • Investigate using Cygwin for Unix-like command capabilities on Windows
USEFUL FOR

Individuals managing multiple devices with shared media libraries, developers looking for efficient file synchronization methods, and users interested in automating file management tasks on Windows systems.

aychamo
Messages
375
Reaction score
0
My network directory file synch batch file thing :)

Hey guys

I had been looking for a nice cheap easy to use program to sync two of my mp3 directories. This is what I did:

Here is my setup:
My HTPC .mp3 drive is mapped to my laptops' E drive.
I always download my songs onto my computer (C drive) and keep them on my C drive as I travel with my laptop.

My problem:
I maintain a \Artist\Album\Songname.mp3 directory structure on my C drive and I mimic the same directory structure on my HTPC for my music there. It was a pain if I would get a new artist or album on my C drive and then have to copy it over. Also, i couldn't find a command in windows to copy all my *new* files over but not overwrite the old ones (copying so many gigs of data over the network took a while).

My solution:
I wanted a program that would only copy files from my C drive to my HTPC's .mp3 drive (mapped to E on my computer) that did not exist on the HTPCs harddrive. I found some programs, etc, but they were all $$ or I couldn't automate it.

Turns out, it was pretty simple to accomplish. I had to reach back into the old days of batch file programming and pull out the FOR statement.

I then encountered a problem if a directory did not exist, but I solved that and had it create directories and then copy new files over. It's all in the batch file, it should be fairly readable... here it is if anyone needs it:

A quick note: To keep it clean, I have my batch file generate a 2nd batch file that it needs, and the .mp3 list.. and it cleans them up afterwards. I added the generation thing in last so that's why you see the echoing of REM'ed statements to a batchfile you'd never get to read :-)

PHP:
@echo off

echo @if "%%3"=="" goto dothedir>copyit.bat
echo @if "%%3"==".bat" goto end>>copyit.bat
echo @if "%%3"==".lst" goto end>>copyit.bat
echo @echo off>>copyit.bat
echo if not exist %%2 goto notexists>>copyit.bat

echo REM if the third parameter (extesnion) is blank, it's a dir so jump down>>copyit.bat
echo REM there, ..  if the 2nd paremeter (destination) doesn't exist, then copy it!>>copyit.bat

echo REM this is for if the destination does exist>>copyit.bat
echo goto end>>copyit.bat

echo REM if the destination doesn't exist, it comes here, copies first to 2nd>>copyit.bat
echo :notexists>>copyit.bat

echo echo copy %%1 %%2>>copyit.bat
echo copy %%1 %%2>>copyit.bat
echo goto end>>copyit.bat
     
echo REM if the passed file is a directory, come here, check it if exists>>copyit.bat
echo REM if it does exist, end, if it doesn,t jump to the part to maek it>>copyit.bat
echo :dothedir>>copyit.bat
echo @echo off>>copyit.bat
echo if not exist %%2 goto makethedir>>copyit.bat
echo goto end>>copyit.bat

echo REM this makes the directory, and let's you know>>copyit.bat
echo :makethedir>>copyit.bat
echo mkdir %%2>>copyit.bat
echo echo Created directory: %%2>>copyit.bat
echo goto end>>copyit.bat

echo :end>>copyit.bat

echo Generating dir list
dir /s /b /og *.*>mp3.lst

echo Executing FOR statement
for /F "delims=$" %%i in (mp3.lst) do @call copyit.bat "%%i" "e:\media%%~pnxi" %%~xi

echo Cleaning up ..
del copyit.bat
del mp3.lst
echo Done!
exit
 
Computer science news on Phys.org
Developpers and group projects often have the same issue. How to keep a common codebase/document base while so many different users and different computers are accessing it. Well, there's a solution! :surprise:

http://subversion.tigris.org/

It might be a little more complex then typical windows world stuff, but it gets the job done, and quite nicely for devs. It will work with text or binary files.
 
In Linux/Unix you could setup a cron job that executes this command:

cp -uRp <source directory> <destination directory>

u = only overwrite the file if the timestamp is different (This means the file has been modified)
R = copy recursively
p = preserve file attributes (like the time stamp)

You would do this on both computers at different times. If you wanted to you could setup cygwin to do this.

[edit] Subversion could work, but a hassle because you need to commit your changes.
 

Similar threads

Replies
16
Views
4K
Replies
10
Views
5K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 22 ·
Replies
22
Views
3K
  • · Replies 10 ·
Replies
10
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
Replies
81
Views
8K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
8K