![]()
[<<]Message[>>] [<<]Author[>>] [<<]Subject[>>] [<<]Thread[>>]
Number : 9132 Date : 2004-11-04 Author : Garry Deane Subject : Re: File rename revisited Size(KB) : 3
--- In xxcopy@yahoogroups.com, "jeremyrector66" wrote: > > Thanks for your response Garry. I have seen some of your > other scripts for renaming files, but I know way too little > about the sort of regular expression that you used. I plan > on making this a learning experience. > > 1. I am actually looking to do both. All of my current backups > use this scheme. Moving forward, I can do this to them. > As time allows I can uncompress and convert my archives > as well. OK. I've worked out a batch file which converts your existing backup directories which are as listed below. You should be able to use similar xxcopy commands when you convert to the new backup scheme. > 2. Very close. My source looks like this. > > \src\041103\0900\dir1\file1.txt > \src\041103\0900\dir1\file2.txt > \src\041103\1000\dir1\file1.txt > \src\041103\1000\dir1\file2.txt > \src\041103\1000\dir1\subdir1\file1.txt > > and I want it to look like this > > \src\dir1\file1-041103-0900.txt > \src\dir1\file2-041103-0900.txt > \src\dir1\file1-041103-1000.txt > \src\dir1\file2-041103-1000.txt > \src\dir1\subdir1\file1-041103-1000.txt > > This lets me use a single directory tree instead of multiples > for dates and times. File versions sort out by date/time and > I can see at a glance how many revisions there are, and > recover the correct one without having to search through > a separate tree for each day and seperate sub-tree's for > every hour. > > My current system seemed like a good idea at the time. OK, I think that example explains it clearly. I'd probably use a batch file to construct a variable which converts say \src\041103\0900\dir1\file1.txt to \src\dir1\file1-041103-0900.txt Then I'd just use a MOVE command for each file using these variables. However this could get pretty complex depending on the actual \src\ name and handling file names and paths with spaces. Since you aren't experienced with batch, the following is a simpler approach. It mostly uses xxcopy commands and fairly standard batch code. It is quite versatile however it does require 2 lots of file copies so may not be too efficient if you have a large amount of data to deal with. Note that the following code is not as large as it looks because I've commented the steps involved (lines starting with :: are comments) so that you can follow the process and modify it to your own needs. Garry @echo off setlocal set src=d:\test\src dir :: The above is the source path which has the yymmdd subdirs :: If you want to pass this as a parameter, use: :: set src=%~1 if not exist "%src%" echo Source not found!&goto :eof :: The next is the dest path where the renamed files will go set dst=d:\test\dst dir :: The next is a temporary dir for the part renamed files set tmp=d:\test\tmp :: Clean out any files in the temporary path rd /s/q %tmp% 2>nul :: Step 1. Gather files into the temp dir with the path :: appended to the file name. Files end up with a name like :: filename.ext`yyymmmdd`hh00`origpath xxcopy "%src%\" "%tmp%\" /sr :: Step 2. Split each filename into its components of :: filename, yyymmmdd, hh00, origpath then rename them to :: filename-yyymmmdd-hh00`origpath :: See FOR /? for an explanation of the FOR command for /f "tokens=1-3,* delims=`" %%a in ('dir /b %tmp%') do ( set fname=%%a set fdate=%%b set ftime=%%c set fpath=%%d call :rename ) :: Step 3. Rebuild the files using the original path :: in the dest directory and remove after copy :: Remove the /rcy if you want to view the renamed :: intermediate files in the temp directory xxcopy "%tmp%\" "%dst%\" /srr/rcy/y/pd0/ed goto :eof :rename :: This renames the files in the temp directory using the :: extracted components of the gathered filename. :: Note the the next line is wrapped. Everything before :: the "goto :eof" should be on one line. ren "%tmp%\%fname%`%fdate%`%ftime%`%fpath%" "%fname%-%fdate%-%ftime%`%fpath%" goto :eof
This message if part of XXCOPY's message Archive. The archive contains all the messages posted at Yahoo!Groups: XXCOPY.