|
Answer» My skill when it comes to DOS and batch files is pretty limited. I have a solution for my particular task, but I know it's fairly clunky.
Task: I have a number of files sitting in a single folder. They need to be moved to different folders on another DRIVE, depending on their filename. Specifically, the folder names are the first 8 characters of the filename up to the first '_'.
eg.
dingleberries_2_05.mpg would go into folder dinglebe
red_5_3.mpg would go into folder red
Then any files leftover (i.e. without matching folders on the other drive) just get moved into a 'leftovers' DIRECTORY where someone can deal with them. I don't want the batch file to create new directories for these files because in most cases a mismatch will mean someone has misnamed the file.
I can achieve this with a simple COPY / DELETE batch file but someone would have to manually maintain the list of valid directories, eg.:
COPY d:\nick\test01* d:\nick\test01\ DEL d:\nick\test01* /Q COPY d:\nick\test02* d:\nick\test02\ DEL d:\nick\test02* /Q COPY d:\nick\test03* d:\nick\test03\ DEL d:\nick\test03* /Q COPY d:\nick\* d:\nick\leftovers\ DEL d:\nick\* /Q
I know you can set variables and do stuff with substrings - and it looks they'd allow one to do what I'm trying to do much more easily. I've had a google but can't find anything similar enough to what I want that I can work from.
Can anyone help?move red???*.??? %systemdrive%\red move dingle???*.??? %systemdrive%\dingle move ???*.??? %systemdrive%\leftover
the above only works in order, you are looking for file red_5_1.mpg , so red???*.??? is its equivilant, as all files named red are red_6_2.mpg , red 8_1.mpg the last LINE moves the leftover files to the leftover directory.Move is easier than the copy/delete, but I'm probably not going to be responsible for maintaining this thing and was hoping for a batch file that wouldn't need to be updated as new directories are added.
I've seen a RANGE of DOS scripts using substrings and variables that look like they'd do it but go cross-eyed trying to figure out how they work.use dir command to search out the potential directorys instead
dir dingle???*. /b
for /f "tokens=1*" %a in ('dir dingle???*. /b') do move %a C:\folder\%aIf I'm reading that right, someone would still need to update the batch file each time a new directory is added?No, because the dir command checks every directory. The for command combined with dir pretty much says "For every directory, do this."OK, let me try. I love ugly batch files. No, I really hate them! I think what you want is a batch file the will live on and on. That it will create needed dirs by a sort of kind of file prefix thing. I don't know how batch files can extract stings, but I can write a simple program that would do that. Then I would pas the information onto the remainder of the batch file. Remember the the old QBAIC that came with a version of DOS or Windows way back when? It still works in XP, but can not deal with long file name. But we can work AROUND that. Part of the code would be ike this: S=INST(A$,"_") : B$=LEFT$(S-1) It would read a TXT file with the names of all the files. It would write a new TXT file with the unique prefixes. Or something like that. Then the batch file could make DIRs from the new TXT file and copy files that match a prefix.
Is this the kind of thing you want? I have used QBASIC to do string tricks for batch files.Quote from: dslgeek on September 14, 2008, 08:23:59 PM I think what you want is a batch file the will live on and on.
This is exactly what I'm after.
http://www.computing.net/answers/programming/mid-or-substr-batch-simulation/14516.html
Doesn't seem to be 'pure' DOS but would seem to work.
I've also seen some examples of substrings used as variables - which would seem capable of doing what I want. But I've no idea how to approach it.
But while I suck at DOS, I'm relatively comfortable with VB and I think I can write some simple script to rewrite the batch file every time a new folder is added. This would let me keep the batch file simple (i.e. a list of MOVE's).
I think I'll have a go at this first and see if I can pull it off.
|