1.

Solve : Create folder from partial filename and move files into that folder?

Answer»

Try this batch...

CODE: [Select]@echo off

REM Optional section start -------------------------------
REM Create Test files

echo hello > "New York A08Z06 Blue Train Horse JEEP Boat.DOC"
echo hello > "New York A08Z06 Blue Train Horse Jeep Boat.xls"
echo hello > "New York A08Z07 Blue Train Horse Jeep Boat.doc"
echo hello > "New York A08Z07 Blue Train Horse Jeep Boat.xls"
echo hello > "New York A08Z08 Blue Train Horse Jeep Boat.doc"
echo hello > "New York A08Z08 Blue Train Horse Jeep Boat.xls"
echo hello > "New York A08Z09 Blue Train Horse Jeep Boat.doc"
echo hello > "New York A08Z09 Blue Train Horse Jeep Boat.xls"
echo hello > "New York A08Z06 Blue Horse Jeep Boat.doc"
echo hello > "New York A08Z06 Blue Horse Jeep Boat.xls"
echo hello > "New York A08Z07 Blue Horse Jeep Boat.doc"
echo hello > "New York A08Z08 Blue Horse Jeep Boat.xls"
echo hello > "New York A08Z09 Blue Horse Jeep Boat.doc"
echo hello > "New York A08Z09 Blue Horse Jeep Boat.xls"
echo hello > "Chicago A01Z01 Green Car Dog Train.doc"
echo hello > "Chicago A01Z01 Green Car Dog Train.xls"
echo hello > "Chicago A01Z02 Green Car Dog Train.xls"
echo hello > "Chicago A01Z02 Green Car Dog Train.doc"
echo hello > "Chicago A01Z03 Green Car Dog Train.xls"
echo hello > "Chicago A01Z03 Green Car Dog Train.doc"
echo hello > "Chicago A01Z01 Green Water Dog Train.doc"
echo hello > "Chicago A01Z02 Green Water Dog Train.doc"
echo hello > "Chicago A01Z03 Green Water Dog Train.doc"
echo hello > "Chicago A01Z04 Green Water Dog Train.doc"
echo hello > "Chicago A01Z05 Green Water Dog Train.doc"

REM These are bad file names
echo hello > "FailAlphaNumeric AB1Z03 Green Car Dog Train.xls"
echo hello > "FailAlphaNumeric AB1Z03 Green Car Dog Train.doc"
echo hello > "FailKeyWord A01Z03 Green Car Dog Bus.xls"
echo hello > "FailKeyWord A01Z03 Green Car Dog Bus.doc"
echo hello > "FailBoth AB1Z03 Green Car Dog Bus.xls"
echo hello > "FailBoth AB1Z03 Green Car Dog Bus.doc"
REM Optional section end -------------------------------

setlocal enabledelayedexpansion
for /f "delims=" %%A in ( 'dir /b *.doc; *.xls' ) do (
for /f "delims=" %%B in ('cscript //nologo Script.vbs "%%~nA"') do Set "Result=%%B"
if "!Result!"=="Bad Filename" (
Echo Not moving file %%A
) else (
If not exist "!Result!" md "!Result!"
Echo Moving file %%A to folder !Result!
Move "%%A" "!Result!" > nul
)
)
echo Complete
echo.
Pause
...which calls this VBscript... (Referred to as Script.vbs in the batch file above)

Note: In the keyword list, put longer phrases before shorter phrases using the same words or the words used singly THUS:

"Large Red Book", "Red Book", "Book", "Red", "Blue", "Water"

Code: [Select]InputString = wscript.arguments(0)
TokensArray = split (InputString , " ")
KeyWordList = Array ("Large Red Book", "Blue Train", "Train", "Blue", "Water")
AlphaBlockFound = False
KeyWordFound = False
OutPutString="Bad Filename"
Wscript.echo "Filename " & InputString
For j = lbound(TokensArray) to Ubound(TokensArray)
MyString=TokensArray(j)
If Len(MyString) = 6 Then
FlagString=""
For k = 1 To 6
MyChar = Mid(MyString,k,1)
If IsNumeric(MyChar) = False Then
If InStr ("ABCDEFGHIJKLMNOPQRSTUVWXYZ", MyChar) > 0 Then
FlagString = FlagString & "A"
End If
Else
FlagString = FlagString & "N"
End If
Next
If FlagString = "ANNANN" Then
AlphaBlockFound = True
AlphaBlock = TokensArray(j)
BlockNum=j
End If
End If
Next
If AlphaBlockFound = True Then
PreambleString = ""
For L = 0 To (BLocknum-1)
PreambleString = PreambleString & " " & TokensArray (L)
Next
PostambleString = ""
For l = (BlockNum+1) To UBound(TokensArray)
PostambleString = PostambleString & " " & TokensArray (L)
Next
PostambleString = trim (PostambleString)
For q = LBound(KeyWordList) To UBound(KeyWordList)
Sstring=Trim(KeyWordList(q))
If instr (UCASE(PostAmbleString), ucase(Sstring)) Then
OutPutString = PreambleString & " " & Mid(TokensArray(blocknum), 1,3) & " " & Sstring
wscript.echo trim(OutPutString)
wscript.quit
End If
Next
End If
wscript.echo OutPutString
You've done it again. It works perfectly. Thanks for taking the time to help me out.



Discussion

No Comment Found