1

Coding is not my speciality but I have come across a problem and google search has guided me to using batch file process in solving it. Essentially I have a couple of thousands of files that need to be moved into folders and they have a very simple file structure, listed below:

UK--London--Filename.pdf
UK--London--Filename2.pdf
UK--Manchester--Filename3.pdf
UK--Liverpool--Filename4.pdf
UK--Chester--Filename5.pdf 

I would like the script to:

1. Pick up the first "--" check if the folder exists, if not create it
2. Pick up the second "--" check if the folder exists, if not create it
3. As there might be more than two "--", ignore the rest
4. Move file into the subfolder

To that end, the output should be some like (note "FILETEST" is the folder I am using to test the script):

C:\FILETEST\UK\London\UK--London--Filename.pdf
C:\FILETEST\UK\London\UK--London--Filename2.pdf
C:\FILETEST\UK\Manchester\UK--Manchester--Filename3.pdf
C:\FILETEST\UK\Liverpool\UK--Liverpool--Filename4.pdf
C:\FILETEST\UK\Chester\UK--Chester--Filename5.pdf

I have had an attempt to re-using a script from another question in stackoverflow (Batch create folders based on part of file name and move files into that folder). I understand that it would not do exactly what I need, but cant seem to get any output.

@ECHO OFF
SETLOCAL
SET "sourcedir=c:\FILETEST"
PUSHD %sourcedir%
FOR /f "tokens=1*delims=--" %%a IN (
 'dir /b /a-d *.*.*'
 ) DO (  
 ECHO MD %%a
 ECHO MOVE "%%a.%%b" --\%%a\
)
POPD
GOTO :EOF

Apologies for any headaches caused, I am hoping this is a simple one to solve.

Thank you,

Panos

Panos
  • 47
  • 1
  • 7
  • 2
    In the script you are using, the answer says that you need to remove the two `ECHO` commands to activate it. Where it says `ECHO MD %%a` you would change it to just `MD %%a`. The `ECHO` part just means print to console. Removing it will execute the `MD` command instead of echoing it. You say you are not getting any output though, how are you running the script? Double clicking it? Or running it from the command prompt? You should run it from the command prompt otherwise the output will only show for a brief moment and auto close. – Matt Aug 18 '17 at 07:49
  • I think you need to alter the file mask as well: `dir /b /a-d *.*.*` try running that command in your directory and make sure it returns the list of files. Try hanging the mask to `*.*` which is just `filename.extension`. – Matt Aug 18 '17 at 08:02
  • Hi Matt, thank you for your quick reply! Magoo has helped solve the issue – Panos Aug 18 '17 at 08:26

1 Answers1

2
@ECHO OFF
SETLOCAL
SET "sourcedir=c:\FILETEST"
PUSHD %sourcedir%
FOR /f "tokens=1,2*delims=-" %%a IN (
 'dir /b /a-d *--*--*.*'
 ) DO if "%%c" neq "" (  
 ECHO MD "%%a"
 ECHO MD "%%a\%%b"
 ECHO MOVE "%%a--%%b--%%c" ".\%%a\%%b\"
)
POPD
GOTO :EOF

Read the directory list of files in the current directory, (/a-d = no directorynames) that match *--*--*. Tokenise so that %%a acquires the part before the first --sequence, %%b the second and %%c the remainder.

If %%c is not empty then make the directories ".\%%a" and ".\%%a\%%b" (quoted because any spaces in the name would otherwise be seen as "create two directories") then move the file, again quoted for the same reason.

Note that each character individually between delims= and the close-quote is a delimiter - a delimiter-string is not supported. Consequently, this code will pick up - as well as --- and any other sequence of - and try to process it. You could gate the create/move further by adding if exist "%%a--%%b--%%c" directly after the if "%%c" neq ""before the (.

The md will create a directory if the target name does not already exist, and produce an error-message if it already exists. To suppress the error message, append 2>nul to the md lines.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • Hi Magoo, thank you very much for your quick reply!! I I amended the delimiter on the 5th line to two hyphens and your script works very nicely indeed. – Panos Aug 18 '17 at 08:18
  • @Panos, please put the delimiter back how it was; Magoo was correct in just using one hyphen. – Compo Aug 18 '17 at 08:22
  • Just re-read your comment and just got the "-" and "--"... Causes some issues with files, but will have to figure a way around that if batch file cannot only look for "--". Again, thank you so much for your help!! – Panos Aug 18 '17 at 08:28
  • Hi Compo, yeap, just put it back the way it was, thank you – Panos Aug 18 '17 at 08:29