• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 550
  • Last Modified:

Need batch file to copy files dependent on the last two characters of the file name

I get a list of images from a download each day that are in one large zip file. Once extracted, they all follow this naming convention:

MLSxxxxxxA.jpg where MLS is a constant, xxxxxx is a number that can vary in digits (currently between 6 and 8 digits), and A is an alphabetic character from A to Z. These are pictures of homes for my real estate website, and the xxxxxx is the Multiple Listing Service number. The first picture for each MLS will not have the alpha character appended (it will just be MLSxxxxxx.jpg) and each interior picture from that home will be appended starting with A. Each home will always have at least the first picture, and may have anywhere from none to 26 additional photos.

What I am trying to do is break out all the photos by the last character of the MLS number (the xxxxxx part) so that I have folders labled from 0 to 9. Thus the photo MLS123456.jpg will go into the 6 folder, the photo MLS123457 and the photo MLS 123457a will go into the 7 folder, etc.

The folders already exist, and I will get a new zip file each day that I need to extract, copy (actually ftp) to the correctly numbered folder, and them delete the orginals and await the next days file.

I have tried a few attempts using for each loops, but I can't figure out how to get just the last character when it doesn't have a letter at the end that will also work when it does.

Thanks for any help you can provide!
0
WTCarr
Asked:
WTCarr
  • 6
  • 4
  • 3
  • +1
1 Solution
 
Kyle AbrahamsSenior .Net DeveloperCommented:
What's to prevent or do you care about

MLS123457
vs
MLS245637

Both end up in the 7 folder?
0
 
WTCarrAuthor Commented:
Sorry - should have been clearer.

I do want all the ones ending in 7 in that same folder. My problem is there are over 200,000 images, and the current ones are already broken up by last digit into the folders to make it easier to handle. The new ones come in in just one lump file.  What I am trying to do is each day upload the newly added pictures into the correct folder.

So at the MLS111111.jpg and MLS 222221.jpg and MLS222221A.jpg will all go in the same /1 folder along will all the other MLSxxxxx1.jpg and MLSxxxxx1A.jpg that are already there. It can overwrite an existing file if one is already there - no need to worry about that.

Thanks for the quick response.
0
 
Kyle AbrahamsSenior .Net DeveloperCommented:
can you post your batch file?  Will modify for you.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
bkhfinkCommented:
I took some code that I use to rename pictures to post to my webpage.
Some stuff you'll need to modify with the code to begin:
1.  Copy the code to a text file and save as MovePictureFiles.vbs.
2.  Change the CreateTextFile location
3.  Change the OpenTextFile location
Some stuff you'll need to do to run the script:
1.  I put instructions at the top of the code to follow.
2.  You'll need to create the list.txt file with dir /B to have the script read in all the names of the pictures.
After the script runs it will creat a move.bat file that you can double click and all the files will move.  This will work best if  you have the script in a folder just under the picture destination folders (ex. if C:\1 then put script in C:\).  Also, create the list.txt and move.bat in same folder as the script.  Modify paths as needed.
Hope this helps.
'This script moves picture files extracted from a zip file to folders
'designated by the last numeric diget in the file name
'(ex. MLS111111.jpg and MLS 222221.jpg and MLS222221A.jpg
'will all go in the same /1 folder along will all the other 
'MLSxxxxx1.jpg and MLSxxxxx1A.jpg that are already there. 
'It will overwrite an existing file if one is already there.)
 
'To set it up extract the zip file to a folder on the desktop
'Open the folder to make sure all extracted
'If list.txt exists on the desktop, make sure to either delete it or clear it out
'Open a cmd window And cd to the directory where the pictures are
'Run the following command In the open cmd window
'dir /B >> "%USERPROFILE%\Desktop\list.txt"
'Run this script from your desktop
'Run the "move.bat" file created from this script
 
Option Explicit
 
Dim objfile	'file system object
Dim readfile	'read file object
Dim sLine	         'hold line read variable
Dim sModLine	'variable for modified sLine
Dim sFolder	'last digit in file to determine folder
Dim WshShell	'windows shell script
Dim i		'counter
Dim fso	         'File System Object
Dim sw	         'Create Text File
 
'initialize i
i=1
 
'create the windows shell object
Set WshShell = WScript.CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
Set sw = fso.CreateTextFile("C:\Documents And Settings\{user}\Desktop\move.bat")', True, True)
 
'create the file system object for creating, reading, and writing file
Set objfile = CreateObject("Scripting.FileSystemObject")
 
'open the file that contains the ip addresses and/or hostnames
Set readfile = objfile.OpenTextFile("C:\Documents And Settings\{user}\Desktop\list.txt")
 
'loop thru the list.txt file until no more records
Do while readfile.AtEndofStream <> True
	
	'read each line of the readfile
	sLine = readfile.readLine
	sModLine = Left(sLine,InStr(1,sLine,".jpg", vbTextCompare)-1)
	sFolder = Right(sModLine, 1)
	If Not IsNumeric(sFolder) Then
		sModLine = Left(sModLine,InStr(1,sModLine,sFolder)-1)
		sFolder = Right(sModLine, 1)
	End If
	'sw.WriteLine(line1 & ", " & line2 & ", " & sLen2)
	sw.WriteLine("move /Y " & sLine & " " & sFolder & "\.")
		
'increment i
i=i+1	
 
Loop
 
'Cleanup
readfile.Close
 
set objfile=Nothing
set readfile=Nothing
Set sLine=Nothing
Set sModLine=Nothing
Set sLen=Nothing
Set WshShell=Nothing
Set i=Nothing

Open in new window

0
 
bkhfinkCommented:
Sorry, I found an error and accounted for a space in the file name.
'This script moves picture files extracted from a zip file to folders
'designated by the last numeric diget in the file name
'(ex. MLS111111.jpg and MLS 222221.jpg and MLS222221A.jpg
'will all go In the same /1 folder along will all the other 
'MLSxxxxx1.jpg And MLSxxxxx1A.jpg that are already there. 
'It will overwrite an existing file if one is already there.)
 
'To set it up extract the zip file to a folder on the desktop
'Open the folder to make sure all extracted
'If list.txt exists on the desktop, make sure to either delete it or clear it out
'Open a cmd window And cd to the directory where the pictures are
'Run the following command In the open cmd window
'dir /B >> "%USERPROFILE%\Desktop\list.txt"
'Run this script from your desktop
'Run the "move.bat" file created from this script
 
Option Explicit
 
Dim objfile	'file system object
Dim readfile	'read file object
Dim sLine	'hold line read variable
Dim sModLine	'variable for modified sLine
Dim sFolder	'last digit in file to determine folder
Dim WshShell	'windows shell script
Dim i		'counter
Dim fso	'File System Object
Dim sw	'Create Text File
 
'initialize i
i=1
 
'create the windows shell object
Set WshShell = WScript.CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
Set sw = fso.CreateTextFile("C:\Documents And Settings\{user}\Desktop\move.bat")', True, True)
 
'create the file system object for creating, reading, and writing file
Set objfile = CreateObject("Scripting.FileSystemObject")
 
'open the file that contains the ip addresses and/or hostnames
Set readfile = objfile.OpenTextFile("C:\Documents And Settings\{user}\Desktop\list.txt")
 
'loop thru the list.txt file until no more records
Do while readfile.AtEndofStream <> True
	
	'read each line of the readfile
	sLine = readfile.readLine
	sModLine = Left(sLine,InStr(1,sLine,".jpg", vbTextCompare)-1)
	sFolder = Right(sModLine, 1)
	If Not IsNumeric(sFolder) Then
		sModLine = Left(sModLine,InStr(1,sModLine,sFolder)-1)
		sFolder = Right(sModLine, 1)
	End If
	'sw.WriteLine(line1 & ", " & line2 & ", " & sLen2)
	sw.WriteLine("move /Y """ & sLine & """ " & sFolder & "\.")
		
'increment i
i=i+1	
 
Loop
 
'Cleanup
readfile.Close
 
set objfile=Nothing
set readfile=Nothing
Set sLine=Nothing
Set sModLine=Nothing
Set sFolder=Nothing
Set WshShell=Nothing
Set i=Nothing

Open in new window

0
 
AmazingTechCommented:
Try this.

Adjust the folders

Set MLSInFolder=C:\MLS
Set MLSOutFolder=C:\MLSWeb
SETLOCAL ENABLEDELAYEDEXPANSION
Set MLSInFolder=C:\MLS
Set MLSOutFolder=C:\MLSWeb
 
for /f %%a in ('dir /b "%MLSInFolder%\mls*.jpg"') do (
     Set MLSJPG=%%~na
     IF !MLSJPG:~-1! GTR 9 Set MLSJPG=!MLSJPG:~0,-1!
     Move /y "%MLSInFolder%\%%a" "%MLSOutFolder%\!MLSJPG:~-1!\*.*"
)

Open in new window

0
 
WTCarrAuthor Commented:
Thanks for the help, guys. I tried them both and couldn't get either one to work right away. I did make one mistake in my original post - the files are actually prefixed with UPSTATEMLS, not just MLS as I said. But I think I corrected in both scripts to account for that.

@bkhfink,
There seems to be a problem in the vbs file at line 51, where you test the file name if the last character is not numeric. Best I could tell, it works fine unless the last character occurs again in the string, which it will if its an M, an L or an S (and all the letters in UPSTATE too now) - for example if the filename is "UPSTATEMLS20091234m.jpg". It then start counting from the wrong point. At this point in the logic, the correct folder name is the second character from the right, but I couldn't figure out how to get that one. I'm not familiar with VBS, and when I commented out line 51 and change 52 to RIGHT(sModLine,2) it returned the last two. Looking up the function I didn't see a length parameter. Looks close, though.

@AmazingTech
I changed the filename on line 5 to be \upstatemls*.jpg. It looks like line 7 doesn't work correctly to replace the variable names, and line 8 returns this for each file:

Move /y "C:\IDXTEMP\UPSTATEMLS156810.jpg" "C:\MLSWeb\!MLSJPG:~-1!\*.*"
The filename, directory name, or volume label syntax is incorrect.

I do like that your solution is one step though!

Thanks so much for your help!
0
 
AmazingTechCommented:
Can you select and copy my code again?

Looks like it's missing the \ inbetween "%MLSInFolder% and %%a in the Move /y command.
0
 
AmazingTechCommented:
Oh yeah could it be the destination folders 0-9 are not there?

Try it with xcopy or we'll need to create folders that are not there for move to work.
SETLOCAL ENABLEDELAYEDEXPANSION
Set MLSInFolder=C:\MLS
Set MLSOutFolder=C:\MLSWeb
 
for /f %%a in ('dir /b "%MLSInFolder%\mls*.jpg"') do (
     Set MLSJPG=%%~na
     IF !MLSJPG:~-1! GTR 9 Set MLSJPG=!MLSJPG:~0,-1!
     If not exist "%MLSOutFolder%\!MLSJPG:~-1!" MD "%MLSOutFolder%\!MLSJPG:~-1!"
     Move /y "%MLSInFolder%\%%a" "%MLSOutFolder%\!MLSJPG:~-1!\*.*"
)

Open in new window

0
 
WTCarrAuthor Commented:
There's still something missing, AmazingTech.
I did indeed create the 0-9 subdirectories before running the batch file.

The move command is resolving to:

Move /y "C:\IDXTEMP\UPSTATEMLS20091962.jpg" "C:\MLSWeb\!MLSJPG:~-1!\*.*"
and of course it fails because there is no target directory called C:\MLSWeb\!MLSJPG:~-1!

The "from" part is correct, but the "to" part subdirectory is coming out with the actual variable name, not "2" as it should be in this case.

Thanks for helping.
Tom
0
 
AmazingTechCommented:
OK. Let's try it with XCOPY first.
SETLOCAL ENABLEDELAYEDEXPANSION
Set MLSInFolder=C:\IDXTemp
Set MLSOutFolder=C:\MLSWeb
 
for /f %%a in ('dir /b "%MLSInFolder%\*mls*.jpg"') do (
     Set MLSJPG=%%~na
     IF !MLSJPG:~-1! GTR 9 Set MLSJPG=!MLSJPG:~0,-1!
     XCOPY /y "%MLSInFolder%\%%a" "%MLSOutFolder%\!MLSJPG:~-1!\*.*"
)

Open in new window

0
 
AmazingTechCommented:
I see now. The target cannot have \*.* at the end.
SETLOCAL ENABLEDELAYEDEXPANSION
Set MLSInFolder=C:\MLS
Set MLSOutFolder=C:\MLSWeb
 
for /f %%a in ('dir /b "%MLSInFolder%\mls*.jpg"') do (
     Set MLSJPG=%%~na
     IF !MLSJPG:~-1! GTR 9 Set MLSJPG=!MLSJPG:~0,-1!
     If not exist "%MLSOutFolder%\!MLSJPG:~-1!" MD "%MLSOutFolder%\!MLSJPG:~-1!"
     Move /y "%MLSInFolder%\%%a" "%MLSOutFolder%\!MLSJPG:~-1!\"
)

Open in new window

0
 
bkhfinkCommented:
I corrected my code to account for the "UPSTATE".  I also set it up to do the directory listing, create the batch file, run the batch file then delete the directory listing and batch file.  I also have it check to make sure the 0-9 folder exists before it moves the file.
'This script moves picture files extraced from a zip file to folders
'designated by the last numeric diget in the file name
'(ex. UPSTATEMLS111111.jpg and UPSTATEMLS 222221.jpg and UPSTATEMLS222221A.jpg
'will all go In the same /1 folder along will all the other 
'MLSxxxxx1.jpg And MLSxxxxx1A.jpg that are already there. 
'It will overwrite an existing file if one is already there.)
 
'To set it up extract the zip file to the C:\IDXTEMP folder
'Run this script from C:\
'It will get the directory listing of IDXTEMP,
'create a move.bat file that will move all the photos in the
'C:\IDXTEMP folder to one of the 0-9 folders
'designated by the last numberic diget in the file name
'then cleanup by deleting the list.txt and move.bat files.
'I also put in a check to make sure the 0-9 folder exists
'before the file is moved.
 
Option Explicit
 
Dim objfile	'file system object
Dim readfile	'read file object
Dim sLine	'hold line read variable
Dim sModLine	'variable for modified sLine
Dim sFolder	'last digit in file to determine folder
Dim sChrs	'Number of characters in the String
Dim WshShell	'windows shell script
Dim fso	'File System Object
Dim sw	'Create Text File
 
'create the windows shell object
Set WshShell = WScript.CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
 
'Create Files
'Get directory listing of C:\IDXTEMP folder
WshShell.Run ("cmd /c dir /B C:\TEMP\IDXTEMP >> C:\TEMP\list.txt")
Set sw = fso.CreateTextFile("C:\TEMP\move.bat")
 
'wait 10 seconds to creat the list.txt file
WScript.sleep 10000
 
'create the file system object for creating, reading, and writing file
Set objfile = CreateObject("Scripting.FileSystemObject")
 
'open the file that contains the directory listing
Set readfile = objfile.OpenTextFile("C:\TEMP\list.txt")
 
'loop thru the list.txt file until no more records
Do while readfile.AtEndofStream <> True
	
	'read each line of the readfile to create move.bat
	sLine = readfile.readLine
	sModLine = Left(sLine,InStr(1,sLine,".jpg", vbTextCompare)-1)
	sFolder = Right(sModLine, 1)
	If Not IsNumeric(sFolder) Then
		sChrs = Len(sModLine)
		sFolder = Right(sModLine, 2)
		sFolder = Left(sFolder, 1)
	End If
	sw.WriteLine("If exist C:\TEMP\" & sFolder & "\*.* move /Y ""C:\TEMP\IDXTEMP\" & sLine & """ C:\TEMP\" & sFolder & "\.")
	sw.WriteLine("If not exist C:\TEMP\" & sFolder & "\*.* echo """ & sFolder & " folder not found"" >> C:\TEMP\errorlog.txt")
 
Loop
 
'Run batch file
WshShell.Run ("cmd /c Call C:\TEMP\move.bat")
 
'Cleanup
readfile.Close
 
WshShell.Run ("cmd /c del C:\TEMP\list.txt")
WshShell.Run ("cmd /c del C:\TEMP\move.bat")
 
set objfile=Nothing
set readfile=Nothing
Set sLine=Nothing
Set sModLine=Nothing
Set sFolder=Nothing
Set WshShell=Nothing

Open in new window

0
 
WTCarrAuthor Commented:
bkhfink - you got it exactly right!
That worked like a charm! And since it's just one script file, I can fire that off from my scheduling program with no problem.

Thanks so much for the help.

@AmazingTech -
Sorry but I could never get the copy to part of your solution to work for me. Maybe something's different on my side, but I did copy it several times and worked to make it go.

0
 
AmazingTechCommented:
Hmmm... That's unfortunate that it doesn't work.

Just for sake of my mind can you use the below code with a few jpgs in C:\IDXTemp folder. Run it and copy and paste the output here?

I put in a pause to keep it on screen if you double click the file from explorer.

Here's a sample of my output.

C:\at>mls

C:\at>SETLOCAL ENABLEDELAYEDEXPANSION

C:\at>Set MLSInFolder=C:\MLS

C:\at>Set MLSOutFolder=C:\MLSWeb

C:\at>for /F %a in ('dir /b "C:\MLS\*mls*.jpg"') do (
Set MLSJPG=%~na
 IF !MLSJPG:~-1! GTR 9 Set MLSJPG=!MLSJPG:~0,-1!
 If not exist "C:\MLSWeb\!MLSJPG:~-1!" MD "C:\MLSWeb\!MLSJPG:~-1!"
 Move /y "C:\MLS\%a" "C:\MLSWeb\!MLSJPG:~-1!\"
)

C:\at>(
Set MLSJPG=upstatemls12133211
 IF !MLSJPG:~-1! GTR 9 Set MLSJPG=!MLSJPG:~0,-1!
 If not exist "C:\MLSWeb\!MLSJPG:~-1!" MD "C:\MLSWeb\!MLSJPG:~-1!"
 Move /y "C:\MLS\upstatemls12133211.jpg" "C:\MLSWeb\!MLSJPG:~-1!\"
)
        1 file(s) moved.

C:\at>(
Set MLSJPG=upstatemls12133212
 IF !MLSJPG:~-1! GTR 9 Set MLSJPG=!MLSJPG:~0,-1!
 If not exist "C:\MLSWeb\!MLSJPG:~-1!" MD "C:\MLSWeb\!MLSJPG:~-1!"
 Move /y "C:\MLS\upstatemls12133212.jpg" "C:\MLSWeb\!MLSJPG:~-1!\"
)
        1 file(s) moved.

C:\at>(
Set MLSJPG=upstatemls1213321a
 IF !MLSJPG:~-1! GTR 9 Set MLSJPG=!MLSJPG:~0,-1!
 If not exist "C:\MLSWeb\!MLSJPG:~-1!" MD "C:\MLSWeb\!MLSJPG:~-1!"
 Move /y "C:\MLS\upstatemls1213321a.jpg" "C:\MLSWeb\!MLSJPG:~-1!\"
)
        1 file(s) moved.

C:\at>(
Set MLSJPG=upstatemls1213329
 IF !MLSJPG:~-1! GTR 9 Set MLSJPG=!MLSJPG:~0,-1!
 If not exist "C:\MLSWeb\!MLSJPG:~-1!" MD "C:\MLSWeb\!MLSJPG:~-1!"
 Move /y "C:\MLS\upstatemls1213329.jpg" "C:\MLSWeb\!MLSJPG:~-1!\"
)
        1 file(s) moved.

C:\at>(
Set MLSJPG=upstatemls1213329a
 IF !MLSJPG:~-1! GTR 9 Set MLSJPG=!MLSJPG:~0,-1!
 If not exist "C:\MLSWeb\!MLSJPG:~-1!" MD "C:\MLSWeb\!MLSJPG:~-1!"
 Move /y "C:\MLS\upstatemls1213329a.jpg" "C:\MLSWeb\!MLSJPG:~-1!\"
)
        1 file(s) moved.

C:\at>(
Set MLSJPG=upstatemls121339
 IF !MLSJPG:~-1! GTR 9 Set MLSJPG=!MLSJPG:~0,-1!
 If not exist "C:\MLSWeb\!MLSJPG:~-1!" MD "C:\MLSWeb\!MLSJPG:~-1!"
 Move /y "C:\MLS\upstatemls121339.jpg" "C:\MLSWeb\!MLSJPG:~-1!\"
)
        1 file(s) moved.
SETLOCAL ENABLEDELAYEDEXPANSION
Set MLSInFolder=C:\IDXTemp
Set MLSOutFolder=C:\MLSWeb
 
for /f %%a in ('dir /b "%MLSInFolder%\*mls*.jpg"') do (
     Set MLSJPG=%%~na
     IF !MLSJPG:~-1! GTR 9 Set MLSJPG=!MLSJPG:~0,-1!
     If not exist "%MLSOutFolder%\!MLSJPG:~-1!" MD "%MLSOutFolder%\!MLSJPG:~-1!"
     Move /y "%MLSInFolder%\%%a" "%MLSOutFolder%\!MLSJPG:~-1!\"
)
 
PAUSE

Open in new window

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now