Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Create folder from partial filename and move files into that folder  (Read 14367 times)

0 Members and 1 Guest are viewing this topic.

Salmon Trout

  • Guest
Re: Create folder from partial filename and move files into that folder
« Reply #15 on: February 10, 2012, 01:50:53 PM »
Oh, well, if it's an employer's machine and the IT people have disabled scripting, that kills my idea. Sorry. (Home Premium? At work?)



devo

    Topic Starter


    Rookie

    • Experience: Beginner
    • OS: Unknown
    Re: Create folder from partial filename and move files into that folder
    « Reply #16 on: February 10, 2012, 02:09:40 PM »
    It's my machine.  I've enabled Windows Script Host now.
    A square box popped up:

    Today is 10/02/2012

    This is a Visual Basic Script

    Click OK to quit. 

    There's a close button and an OK button.

    Salmon Trout

    • Guest
    Re: Create folder from partial filename and move files into that folder
    « Reply #17 on: February 10, 2012, 02:37:47 PM »
    OK we're good to go... it's evening here and I am going to watch a film... and then bed... expect a script in around 12 hours...

    A question: in the 6 character block, are the letters always upper case?

    devo

      Topic Starter


      Rookie

      • Experience: Beginner
      • OS: Unknown
      Re: Create folder from partial filename and move files into that folder
      « Reply #18 on: February 10, 2012, 02:48:48 PM »
      Quote
      A question: in the 6 character block, are the letters always upper case?

      No, sometimes upper sometimes lower. 

      Thanks

      patio

      • Moderator


      • Genius
      • Maud' Dib
      • Thanked: 1769
        • Yes
      • Experience: Beginner
      • OS: Windows 7
      Re: Create folder from partial filename and move files into that folder
      « Reply #19 on: February 10, 2012, 04:08:06 PM »
      What Film ? ?
      " Anyone who goes to a psychiatrist should have his head examined. "

      Salmon Trout

      • Guest
      Re: Create folder from partial filename and move files into that folder
      « Reply #20 on: February 10, 2012, 04:46:44 PM »
      What Film ? ?

      Actually, a TV series episode: Series 1 Episode 3 of the Danish "Borgen"... (Danish TV series are a big hit here right now)

      http://en.wikipedia.org/wiki/Borgen_%28Danish_TV_series%29

      ... and then after that, "101" - a documentary film about the 1989 Depeche Mode concert in Pasadena

      http://www.imdb.com/title/tt0094590/

      You know, I had forgotten how good they were! But the hairstyles (of everybody) were so funny!



      Salmon Trout

      • Guest
      Re: Create folder from partial filename and move files into that folder
      « Reply #21 on: February 11, 2012, 06:57:13 AM »
      Batch file that calls vbs file

      Code: [Select]
      @echo off

      REM Create Test files
      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"

      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"


      setlocal enabledelayedexpansion
      for /f "delims=" %%A in ( 'dir /b *.doc; *.xls' ) do (
      for /f "delims=" %%B in ('cscript //nologo ParseName.vbs "%%~nA"') do Set Result=%%B
      if "!Result!"=="Bad Filename" (
      echo File %%A Error:  !Result!
      ) else (
      If not exist "!Result!" md "!Result!"
      Move "%%A" "!Result!"
      )
      )


      ParseName.vbs

      Code: [Select]
      InputString = wscript.arguments(0)
      TokensArray = split (InputString , " ")
      KeyWordList = Array ("Train","Blue","Sky","Cheese","Ratio","Sanitary","Rosebush")
      AlphaBlockFound = False
      KeyWordFound = False
      OutPutString="Bad Filename"
      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
      'wscript.echo "Token (" & j & ") = " & TokensArray(j) & " " & Len(TokensArray(j)) & " " & FlagString
      BlockNum=j
      PreambleString = ""
      For L = 0 To (BLocknum-1)
      PreambleString = PreambleString & " " & TokensArray (L)
      Next
      For l = (BlockNum+1) To UBound(TokensArray)
      'wscript.echo TokensArray(l)
      For q = LBound(KeyWordList) To UBound(KeyWordList)
      If Trim(TokensArray(l)) = Trim(KeyWordList(q)) Then
      OutPutString = PreambleString & " " & Mid(TokensArray(j), 1,3) & " " & KeyWordList(q)
      'wscript.echo Trim(OutPutString)
      End If
      Next
      Next
      End If
      End If
      Next
      wscript.echo Trim(OutPutString)

      devo

        Topic Starter


        Rookie

        • Experience: Beginner
        • OS: Unknown
        Re: Create folder from partial filename and move files into that folder
        « Reply #22 on: February 11, 2012, 08:23:37 AM »
        Thank you for this.  I saved the first piece of code as Batch.vbs and the second as ParseName.vbs.  When I double click on Batch.vbs I get the following error:

        Line: 1
        Char: 1
        Error: Invalid character
        Code: 800A0408
        Source: Microsoft VBScript compilation error


        Any ideas what I did wrong?


        Salmon Trout

        • Guest
        Re: Create folder from partial filename and move files into that folder
        « Reply #23 on: February 11, 2012, 08:28:34 AM »
        The first file is a batch, not a vbs, and therefore should be saved with the extension .bat

        Since you are going to run the batch by double clicking, a good idea is to prevent the comand window from closing as soon as the batch is finished. Add these 2 final lines to the batch, as follows

        Code: [Select]
        Echo Processing completed
        Pause
        « Last Edit: February 11, 2012, 08:45:50 AM by Salmon Trout »

        devo

          Topic Starter


          Rookie

          • Experience: Beginner
          • OS: Unknown
          Re: Create folder from partial filename and move files into that folder
          « Reply #24 on: February 11, 2012, 09:44:11 AM »
          Thank you so much.  It works perfectly.

          Salmon Trout

          • Guest
          Re: Create folder from partial filename and move files into that folder
          « Reply #25 on: February 11, 2012, 09:46:30 AM »
          You are very welcome. Thanks for the response.


          devo

            Topic Starter


            Rookie

            • Experience: Beginner
            • OS: Unknown
            Re: Create folder from partial filename and move files into that folder
            « Reply #26 on: September 14, 2013, 12:41:02 PM »
            Hi,

            Just an update to this.  Is it possible to have multiple keywords/phrase between the "" in the keywordlist?

            e.g. in the ParseName.vbs the keyword list is currently

            Code: [Select]
            KeyWordList = Array ("Train","Blue","Sky","Cheese","Ratio","Sanitary","Rosebush")
            Is it possible to change it to the following

            Code: [Select]
            KeyWordList = Array ("Train","Blue","Sky","Cheese","Ratio","Sanitary","Rosebush",      "Train Blue","Train Blue Sky","Cheese Green Ten Hotel")

            If a file contained "Train" it would move to the Train folder, as it currently does
            If a file contained "Train Blue" it would move to the Train Blue folder (not the Train or the Blue folder)
            If a file contained "Train Blue Sky" it would move to the Train Blue Sky folder (not the Train/Blue/Train Blue folder)

            I can get it working if I put a - between the keywords e.g. Train-Blue.  But I have to rename the file to include the hyphen and rename it afterwards.  The folder name also has the hyphen which I don't want, I'd like a space between each word.

            Salmon Trout, is this possible.


            Thanks

            Salmon Trout

            • Guest
            Re: Create folder from partial filename and move files into that folder
            « Reply #27 on: September 14, 2013, 02:10:38 PM »
            Well, my first thought, on looking at my script from February 2012 is "Did I write that?" My second thought is that I am flying to Spain tomorrow morning, so although I will give it some thought, and I am sure that what you want is possible, don't expect anything for about 1 week.

            devo

              Topic Starter


              Rookie

              • Experience: Beginner
              • OS: Unknown
              Re: Create folder from partial filename and move files into that folder
              « Reply #28 on: September 14, 2013, 02:29:06 PM »
              That's great.  Whenever you can, there's no hurry.

              Salmon Trout

              • Guest
              Re: Create folder from partial filename and move files into that folder
              « Reply #29 on: October 02, 2013, 01:59:45 PM »
              .
              « Last Edit: October 02, 2013, 02:12:49 PM by Salmon Trout »