WatchDirectory Forum
http://www.watchdirectory.net/cgi-bin/yabb25/YaBB.pl
watchDirectory forums >> How to... >> Move files based on partial file/ folder name
http://www.watchdirectory.net/cgi-bin/yabb25/YaBB.pl?num=1298919253

Message started by Asanam on Feb 28th, 2011 at 7:54pm

Title: Move files based on partial file/ folder name
Post by Asanam on Feb 28th, 2011 at 7:54pm
Hi,

I have a file(s) named 'A123 <anything>' and wish to automatically move it to a folder "\\Docs\A123 FOLDER for archiving" based on the match A123. Can anyone please comment if this would be possible in watchDirectory.

A123 could be any 4 letter code and the destination folder could be nested in "\\Docs\...".

Many thanks.

Asanam

Title: Re: Move files based on partial file/ folder name
Post by Gert on Mar 1st, 2011 at 7:13am
Hello Asanam,

This should be easy with any task that supports Dynamic Naming ( http://www.watchdirectory.net/wdhelp/plugins/dynamicname_functions.html ), such as the Copy Files task.
In your example, you would enter the Target Directory with something like
[code]\\Docs\{left{4,{filename]}\[/code]

This will take the first 4 characters from the detected file. Shouldnt there be a servername before "Docs"? Something like \\Servername\docs?

Gert

Title: Re: Move files based on partial file/ folder name
Post by Asanam on Mar 1st, 2011 at 10:49am
Hi Gert,

Thanks for the prompt response. Yes, you're right about \\Servername\.... but let's just assume that's Docs:-)

I want file <110301 B012 2 be filed> to end up in a folder <B>, subfolder <B012 This is the B Folder>, that is to ignore This is the B Folder. In other words, the match should be on the four letter code B012 only.

The code I'm testing with is D:\WD\{substring{7,1,{filename]}\{substring{7,4,{filename]}.

\\Docs\{left{4,{filename]}\  would simply create a new folder, same as I code above-wouldn't it?

Regards

Asanam

Title: Re: Move files based on partial file/ folder name
Post by Gert on Mar 2nd, 2011 at 6:31am
Correct, that will result in D:\WD\B\B012

Please note that some of the tasks require a full path (including filename) others do not require this and allow you to enter just the directory-name.

If the task you use requires a full path, simply use
D:\WD\{substring{7,1,{filename]}\{substring{7,4,{filename]}\{filename}

Title: Re: Move files based on partial file/ folder name
Post by Asanam on Mar 2nd, 2011 at 10:53am
Thanks that's near enough but I don't think I explained what I am trying to achieve..

Files

110302 A001 A scrap of paper
110301 A002 Some scanning
110228 A002 Email to Company
110227 A001 Bills for this year


should be filed into folders

A001 XYZ things
A002 JFK pieces

These folders always exist beforehand. Its a matter of getting the files into these folders using watchDirectory.

A
|__________A001 XYZ things
|                     |_______________110302 A001 A scrap of paper
|                     |_______________110227 A001 Bills for this year
|__________A002 JFK pieces
|                     |_______________110301 A002 Some scanning
|                     |_______________110228 A002 Email to Company

In otherwords, ignore XYZ things in A001 XYZ things


I do hope this is possible.

Regards

Asanam

Title: Re: Move files based on partial file/ folder name
Post by Gert on Mar 2nd, 2011 at 2:53pm
I think I know a solution, but I need to discuss it with a few people before I implement it for a beta release of WatchDirectory. I hope we will have something to test for you early next week.

Title: Re: Move files based on partial file/ folder name
Post by Asanam on Mar 7th, 2011 at 3:52pm
Gert, that's very good of you. Sorry that I wasn't able to explain the problem straight away.

Looking forward to a solution.

Regards

Asanam

Title: Re: Move files based on partial file/ folder name
Post by Gert on Mar 9th, 2011 at 8:54am
Hello Asanam,

Sorry for the late reply, I was experimenting with this.

If you use the "Run a batch file" task ( http://www.watchdirectory.net/wdhelp/plugins/wdopAutoRunBat.html ), you should be able to use the following script to do what you want.

On the ~3rd line, change the main directory where you want files moved to (do not include the A001 part).
At the bottom, you see "blocks" like

Code (]IF /i "%P811%" EQU "A001" (
  SET RESULT=A001 XYZ things
  GOTO :EOF
)
[/code):

You can copy them to add other rules to determine the exact sub-directory where files should be moved.

Hope this helps,
Gert

[code]rem see http://www.watchdirectory.net/wdhelp/plugins/wdopAutoRunBatEnv.html for
rem %WD_xxxx% variables

SET TARGET=\\Server\Share\MainDir

rem get pos 8 of the filename
SET P8=%WD_FILE_N:~7,1%

rem get pos 8-11 of the filename
SET P811=%WD_FILE_N:~7,4%

Call :GetDir

rem make sure the directory exists
MD "%TARGET%\%RESULT%"

rem now move the detected file
MOVE "%WD_FILE%" "%TARGET%\%RESULT%"

GOTO :EOF


rem this subroutine fills the var RESULT
rem with the proper subdirectory
:GetDir
SET RESULT=

IF /i "%P811%" EQU "A001" (
  SET RESULT=A001 XYZ things
  GOTO :EOF
)

IF /i "%P811%" EQU "A002" (
  SET RESULT=A002 JFK pieces
  GOTO :EOF
)




rem all other files - we don't know the destination
rem dump them in the directory "unknown"
SET RESULT=UNKNOWN

GOTO :EOF

Title: Re: Move files based on partial file/ folder name
Post by Asanam on Mar 9th, 2011 at 11:54am
Thanks for that Gert. Perhaps if I elaborated:

There are 26 folders A...Z; within A there may be 999 folders A001..A999 and each of these folders have a description ie "A001 This is the first folder", "A002 This is the second folder" and so on. Likewise with B.. B001...B999.. "B001 This is the first folder", "B002 This is the second folder" and so on; continuing with C etc through to Z.

All in all there are currently 2000 (variable) or so folders, some added or deleted as the days go by.

The script, a I understand it, requires that these folder name are 'hard coded'. The ideal solution would be an instruction to ignore everything after the 5th character and onwards in the folder name.

Would that be possible?

Regards

Asanam


Title: Re: Move files based on partial file/ folder name
Post by Gert on Mar 9th, 2011 at 5:05pm
I will try to come up with something for you so the batch script will "find" the folder on its own.

Title: Re: Move files based on partial file/ folder name
Post by Gert on Mar 10th, 2011 at 8:22am
Please give this script a try. It tries to find the proper sub-directory on its own (uses subdir UNKNOWN if not found).

[code]rem see http://www.watchdirectory.net/wdhelp/plugins/wdopAutoRunBatEnv.html for
rem %WD_xxxx% variables

SET TARGET=\\Server\Share\MainDir

rem get pos 8 of the filename
SET P8=%WD_FILE_N:~7,1%

rem get pos 8-11 of the filename
SET P811=%WD_FILE_N:~7,4%

Call :GetDir

rem make sure the directory exists
MD "%TARGET%\%RESULT%"

rem now move the detected file
MOVE "%WD_FILE%" "%TARGET%\%RESULT%"

GOTO :EOF


rem this subroutine fills the var RESULT
rem with the proper subdirectory
:GetDir

SET RESULT=UNKNOWN
SET DIR=%TARGET%\%P8%
SET SUB=%P811%
FOR /D %%V IN ("%DIR%\%SUB%*") DO SET RESULT=%P8%\%%~nxV


GOTO :EOF
[/code]


Title: Re: Move files based on partial file/ folder name
Post by Asanam on Mar 24th, 2011 at 1:12pm
Gert,

Thank you very much. This script works exactly as required. One very happy customer :D ;D

Regards,

Asanam

Title: Re: Move files based on partial file/ folder name
Post by WesleyOlins on Aug 2nd, 2012 at 1:00am

Gert wrote on Mar 1st, 2011 at 7:13am:
Hello Asanam,

This should be easy with any task that supports Dynamic Naming ( http://www.watchdirectory.net/wdhelp/plugins/dynamicname_functions.html ), such as the Copy Files task.
In your example, you would enter the Target Directory with something like
[code]\\Docs\{left{4,{filename]}\[/code]

This will take the first 4 characters from the detected file. Shouldnt there be a servername before "Docs"? Something like \\Servername\docs?

Gert


Seems that you know a lot about this Gert and I was hoping to get some assistance if you wouldn't mind.
I am trying to setup WatchDirectory to do the following:
Monitor the D:\Pictures directory
Move picture files (all of them are .jpg) from D:\Pictures (I want watch directory to monitor the first three characters of the filename)
Into D:\Pictures\1stCharacter\2ndCharacter\3rdCharacter

Is there a dynamic naming convention I could use to accomplish this?
I have been searching for quite a while and I am unable to find anything that can help.

Title: Re: Move files based on partial file/ folder name
Post by Gert on Aug 2nd, 2012 at 7:22am
Hello Wesley,

If you have the option "dynamic name is a directory" selected you can use this

Code (]D:\Pictures\{substring{0,1,{filetitle]}\{substring{1,1,{filetitle]}\{substring{2,1,{filetitle]}[/code):
If you do not have that option selected, use this instead
[code]D:\Pictures\{substring{0,1,{filetitle]}\{substring{1,1,{filetitle]}\{substring{2,1,{filetitle]}\{filename}


See http://www.watchdirectory.net/wdhelp/plugins/dynamicname_functions.html for the list of {tags} and {functions}.

Gert

Title: Re: Move files based on partial file/ folder name
Post by WesleyOlins on Aug 3rd, 2012 at 1:45am

Gert wrote on Aug 2nd, 2012 at 7:22am:
Hello Wesley,

If you have the option "dynamic name is a directory" selected you can use this

Code (]D:\Pictures\{substring{0,1,{filetitle]}\{substring{1,1,{filetitle]}\{substring{2,1,{filetitle]}[/code):
If you do not have that option selected, use this instead
[code]D:\Pictures\{substring{0,1,{filetitle]}\{substring{1,1,{filetitle]}\{substring{2,1,{filetitle]}\{filename}


See http://www.watchdirectory.net/wdhelp/plugins/dynamicname_functions.html for the list of {tags} and {functions}.

Gert


Gert.  You are amazing!

One day I'm sure I will come to you for contracting!

Thank you again for all your help!

Title: Re: Move files based on partial file/ folder name
Post by billleong on Aug 22nd, 2012 at 7:08pm
I have similar request but I wanted it to move the folder & contents. The identifier is "-FIN" attached to the name of the folder. I want WD to locate it, move the folder & content to the Archive Directory which has a mirrored structure. Example:

Server1/Folder1/plus content
Server1/Folder2-FIN/plus content
Server1/Folder3/plus content

Find folders with -FIN and move to server 2

Server2/Folder1-FIN/plus content
Server2/Folder2-FIN/plus content
Server2/Folder3-FIN/plus content

The test show that it sees the UNC paths but the script does not find the folders or move them.

Bill

WatchDirectory Forum » Powered by YaBB 2.5.2!
YaBB Forum Software © 2000-2017. All Rights Reserved.