Moving files to folders based on file name

January 20, 2010 at 02:59:23
Specs: Windows XP
Hi, I looked at the example of an almost perfect
version of what I needed here on this site
http://bit.ly/7Aslit But for some reason it doesn't quite
do what I was after due to the file names I am dealing
with.

Basically what I need to do is take a file name like
these

-83G)@+AWS#_oprlogprio7_90311358.zip
!04)JZH;WS#_oprlogprio7_90309569.zip
'56M5}2BWS#_oprlog7_90309567.zip

then take the number for example 90311358 out of
that file name and make a folder named 90311358 and
put that file inside that folder and then repeat for all
the files with the same number. All the files have "_"
in their name just before the number starts and it
should always be 8 digits and always ends with ".zip"
and also has "WS#_" after a random string of
characters (uniqueness required due to number of
files)

The problem with the other example was that I had to
use the option to MOVE the file rather than COPY and
when I changed that example from "for %%I in (*.dat)
do" to "for %%I in (*.zip) do" it would not work as
DOS could not find these file names even when I
manually used DIR filename but it had no problem
finding the file in the folder using the search option in
windows.

Is there another DOS script that will do this task for
me?

Thanks Heaps.

Keith.


See More: Moving files to folders based on file name

Report •


#1
January 20, 2010 at 06:12:21
The following is NT batch script working under Windows 2000 and higher.
After saving with the name you prefer type e.g.

mycopy Your_Folder_name

The batch is not tested, so report if anything goes wrong.

@echo off & setlocal EnableDelayedExpansion
echo.
pushd %*
for %%j in (*.zip) do (
  set file=%%~nj
  set file=!file:~-8!
  if not exist C:\!file! md C:\!file!
  copy "%%~j" "C:\!file!"
  echo "%%~j" -^> "C:\!file!"
)
popd
echo.& echo.DONE
:: End_Of_Batch


Report •

#2
January 21, 2010 at 03:44:20
Hi IVO

Not quite working? I'm not sure what was going wrong at the time so don't know what to ask or say as I got forced to go back to the first one.

I went back to the original one and made a change to a few
bits as I have now found out that the files need to have
.zip.mime at the end of them due to the software that
imports it expects this. *sighs* At least I have jogged some
very old memories and made some new ones about DOS.

Here is the code I'm working with.

@echo off
:Make sure all files are named .zip.mime
ren *.zip *.zip.mime
setlocal EnableDelayedExpansion
:For every file in current dir,
for %%I in (*.mime) do (
  call :check_ %%~nI
:Remove "echo" from the below line to have the file copied to 
the destination directory.
md %2\!Dir2Move!\
echo move %1\%%I %2\!Dir2Move!\
move %1\"%%I" %2\!Dir2Move!\
)
goto :EOF
:check_
: Find the length of the filename by redirecting it to a temp 
file and getting filesize.
set tmpFile=%temp%\##.tmp
set fname=%*
echo !fname! > !tmpFile!
for %%F in (!tmpFile!) do set flnmLen=%%~zF
del !tmpFile! >NUL
set /a flnmLen=!flnmLen!-3
:echo fname=**!fname!** Len=!flnmLen!
:Extract last J characters and see whether the first char is _
:If yes, that is what we want.
for /l %%J in (1,1,!flnmLen!) do (
  set stripped=!fname:~-%%J!
  if not [!stripped!]==[] (
    set lastChar=!stripped:~0,1!
    if [!lastChar!]==[_] (
      :remove _ from the beggining of string and the last 4 
digits off the end
      set Dir2Move2=!stripped:~1!
	  set Dir2Move=!Dir2Move2:~0,-4!
      exit /b
    )
  )
)
exit /b
:EOF

OK

Now this script works ALMOST perfectly but I am having
problems with some specific file names. As I mentioned the
file names are like this (now with .mime)

!04)JZH;WS#_oprlogprio7_90309569.zip.mime

and out of 20,000 files I will have a thousand or so that
cannot be found by DOS without the use of "quotes".

I've tried all sorts of things to get around this and have even
tried using the freeware version of XXCOPY and then DEL in the loop which does find a few more of them but still it doesn't find 100% of them.

-=-=-=-

OK long story short. In the command
move %1\"%%I" %2\!Dir2Move!\
is there some way I can make it move the filenames that
can only be found when using "quotes" around them? As you can see I've already tried the "quotes" around the %%I with no luck.

If not is there a different script I can run that will change the
name from say

-83G)@+AWS#_oprlogprio7_90311358.zip.mime
!04)JZH;WS#_oprlogprio7_90309569.zip.mime
'56M5}2BWS#_oprlog7_90309567.zip.mime

to

00000001WS#_oprlogprio7_90311358.zip.mime
00000002WS#_oprlogprio7_90309569.zip.mime
00000003WS#_oprlog7_90309567.zip.mime

Either way is fine I'm just kinda lost at the moment. :)

Thanks heaps


Report •

#3
January 21, 2010 at 13:41:23
I'm guessing that the exclamations are making the most fuss, but there might well be other char.s causing havoc as well. I would rename any files that are kicking up, so you or some other poor programmer down the road won't have to deal with garbage like @!>$$&!<+= as filenames. What you need to do is find out if any other char.s other than exclams are involved. this test script will display all those files that have exclams in them, and provisionally rename them. It is
a combination of testing and repairing, so it's kind of garbley and LOTS of pauses and echos, so feel free to work with it to your best advantage:
@echo off & setlocal enabledelayedexpansion
set ws=WS#_
for %%a in (*) do (
set /a xx+=1
set test1=%%a
call :renam !test1!
setlocal disabledelayedexpansion
echo.disabled:[%%a]
set test2=%%a
call :uu
endlocal enabledelayedexpansion
)
goto :eof

:uu
echo %xx%
echo.test1:[%test1%] test2:[%test2%]
if "%test1%" neq "%test2%" (
echo.test1:[%test1%] test2:[%test2%] bad filename
pause
)
:: renaming is contingent upon WS#_ in filename
if "%ff%" neq "" (
::ren %test2% %ff%
echo ren %test2% %ff%
pause
)

goto :eof

:renam
:: this just generates the new name, doesn't do the rename
set ff=
for /L %%z in (0 1 99) do (
set test=!test1:~%%z,4!
echo.##[!test!]
if "!test!" equ "" goto :zzz
if "!test!" equ "!ws!" (
echo.!test!
set xy=00000000!xx!
set xy=!xy:~-8!
set ff=!xy!!test1:~%%z!
echo.FF:!ff!
pause
goto :zzz
)
)

:zzz


Report •

Related Solutions

#4
January 21, 2010 at 15:39:25
All I can say is WOW...

You are correct these files have horrible things in the first 8 characters. The programmer who made this was working with a combination of Java on the server which sends data to an Oracle server from devices running Windows CE. I am starting to question the programmers sanity and possibly species. :)

My Files

I've put two files in that shared mediafire folder (link above).
Output.txt is what came out when I ran the script against the files in the folder (a list of which is in FileList.txt)

I removed the pause commands from your script and ran it against the large set of file as it seemed to work on a small test.

The test did prove quite well that characters in the file name are causing the issue.

Is there a script I can run that will just rename the first 8 characters of the filename to 8 letters or numbers (doesn't matter if they are random) without caring what characters it is trying to change?

I will only be running this script on the files that did not work and then running the original script again.

Again thanks this work is brilliant.


Report •

#5
January 21, 2010 at 21:14:56
i could not get files from the mediafire - might be a browser problem as i am running ie5 (i think) (too lazy so far to go to firefox but i'm increasingly motivated) when i hit the download button it just took me to some dumb "upgrad to bulk download" type screen. I tried lots of ways, if you can enlighten me how to get the files i would be interested to see what transpired on your end and filenames you're dealing with.
The thing I wrote was supposed to do exactly what you just said: rename file regardless of everything up to WS#_ and preserving everything from that point on. (subrtn :renam generates the new name, rename performed up in :uu)
it's just a hack, so i left the actual rename disabled with ::,
and i can't (so far) see outputs so i don't know what resulted.
It sounds like you're on the right track and i would DEFINITELY rename those dingleberry filenames. (it were me, i would rename all the files so you have consistent format, but that's strictly a personal preference and prob'ly not necess.)

Report •

#6
January 21, 2010 at 21:42:30
D:\Blah>e.bat
##[=GCR]
##[GCR,]
##[CR,@]
##[R,@W]
##[,@WS]
##[@WS#]
##[WS#_]
WS#_
FF:00000006WS#_oprlogprio7_90310225.zip.mime
disabled:[!!=GCR,@WS#_oprlogprio7_90310225.zip.mime]
6
test1:[=GCR,@WS#_oprlogprio7_90310225.zip.mime] test2:[!!=GCR,@WS#_oprlogprio7_9
0310225.zip.mime]
test1:[=GCR,@WS#_oprlogprio7_90310225.zip.mime] test2:[!!=GCR,@WS#_oprlogprio7_9
0310225.zip.mime] bad filename
ren !!=GCR,@WS#_oprlogprio7_90310225.zip.mime 00000006WS#_oprlogprio7_90310225.z
ip.mime
##[UWQF]
##[WQF8]
##[QF8;]
##[F8;W]
##[8;WS]
##[;WS#]
##[WS#_]
WS#_
FF:00000007WS#_oprlogprio7_90309569.zip.mime
disabled:[!!UWQF8;WS#_oprlogprio7_90309569.zip.mime]
7
test1:[UWQF8;WS#_oprlogprio7_90309569.zip.mime] test2:[!!UWQF8;WS#_oprlogprio7_9
0309569.zip.mime]
test1:[UWQF8;WS#_oprlogprio7_90309569.zip.mime] test2:[!!UWQF8;WS#_oprlogprio7_9
0309569.zip.mime] bad filename
ren !!UWQF8;WS#_oprlogprio7_90309569.zip.mime 00000007WS#_oprlogprio7_90309569.z
ip.mime
##[#~UA]
##[~UAY]
##[UAY6]
##[AY6B]
##[Y6BW]
##[6BWS]
##[BWS#]
##[WS#_]
WS#_
FF:00000008WS#_oprlog7_90309563.zip.mime
disabled:[!#~UAY6BWS#_oprlog7_90309563.zip.mime]
8
test1:[#~UAY6BWS#_oprlog7_90309563.zip.mime] test2:[!#~UAY6BWS#_oprlog7_90309563
.zip.mime]
test1:[#~UAY6BWS#_oprlog7_90309563.zip.mime] test2:[!#~UAY6BWS#_oprlog7_90309563
.zip.mime] bad filename
ren !#~UAY6BWS#_oprlog7_90309563.zip.mime 00000008WS#_oprlog7_90309563.zip.mime
disabled:[!$R$T&]AWS#_oprlog7_90310173.zip.mime]
9
test1:[$R$T
']AWS#_oprlog7_90310173.zip.mime]' is not recognized as an internal or external
command,
operable program or batch file.
']AWS#_oprlog7_90310173.zip.mime]' is not recognized as an internal or external
command,
operable program or batch file.
test1:[$R$T
']AWS#_oprlog7_90310173.zip.mime]' is not recognized as an internal or external
command,
operable program or batch file.
']AWS#_oprlog7_90310173.zip.mime]' is not recognized as an internal or external
command,
operable program or batch file.
ren !$R$T
']AWS#_oprlog7_90310173.zip.mime' is not recognized as an internal or external c
ommand,
operable program or batch file.
##[%+X3]
##[+X3J]
##[X3J)]
##[3J)@]
##[J)@W]
##[)@WS]
##[@WS#]
##[WS#_]
WS#_
FF:00000010WS#_oprlogprio7_90309587.zip.mime
disabled:[!%+X3J)@WS#_oprlogprio7_90309587.zip.mime]
10
test1:[%+X3J)@WS#_oprlogprio7_90309587.zip.mime] test2:[!%+X3J)@WS#_oprlogprio7_
90309587.zip.mime]
@WS#_oprlogprio7_90309587.zip.mime] was unexpected at this time.

D:\Blah>


Report •

#7
January 21, 2010 at 21:49:08
That was the results... Also that upgrade to pro window you just click on the cancel button... :)

The file list is available if you need it... I can pop it onto a site if you have a preference?


Report •

#8
January 21, 2010 at 22:36:01
probably pipe ( | ) generating the other errors...
check back tmrw.

Report •

#9
January 22, 2010 at 16:05:50
i finally got ahold of your filelist (the "download" button was not appearing, but somehow, by accident, it did... once.
i never was able to get the small file but didn't need it anyway.

well, i was wrong about the pipe (he, your programmer) didn't use them (suprisingly enough), it was the ampersand. putting quotes around the filename fixed that. the filelist.txt was very helpful, as i could work with your filenames directly. i made the list into files in a directory and used that to test things, and the following script worked. (it moves the files rather than renaming them due to infinite recursion, but other option is to read the filenames from filelist.txt and process it that way using rename.) my program moves them to a parallel directory called "g" (..\g)

@echo off>flist & setlocal enabledelayedexpansion
set ws=WS#_
set /a xx=0
for %%a in (*.mime) do (
set /a xx+=1
set test1="%%a"
call :renam
setlocal disabledelayedexpansion
set test2="%%a"
:echo A:%%a
call :uu
endlocal enableextensions enabledelayedexpansion
)
goto :eof

:uu
:echo.filename: %test2%
:echo.newname: %ff%
if "%ff%" neq "" (
echo on
move %test2% ..\g\%ff%
echo off
)
goto :eof

:renam
set ff=
for /L %%z in (0 1 99) do (
set test=!test1:~%%z,4!
if "!test!" equ "" goto :zzz
if "!test!" equ "!ws!" (
set xy=00000000!xx!
set xy=!xy:~-8!
set ff="!xy!!test1:~%%z!
echo.FF:!ff!
goto :eof
)
)

i hope it works on your end of town...


Report •

#10
January 23, 2010 at 03:41:36
The system cannot find the path specified.
FF:"00001760WS#_oprlogprio7_90310048.zip.mime"
The system cannot find the path specified.
FF:"00001761WS#_oprlogprio7_90311358.zip.mime"
The system cannot find the path specified.
FF:"00001762WS#_oprlog7_90310061.zip.mime"
The system cannot find the path specified.
FF:"00001763WS#_oprlogprio7_90309593.zip.mime"
The system cannot find the path specified.
FF:"00001764WS#_oprlogprio7_90302349.zip.mime"
The system cannot find the path specified.
FF:"00001765WS#_oprlog7_90309672.zip.mime"
The system cannot find the path specified.
FF:"00001766WS#_oprlogprio7_90309651.zip.mime"
The system cannot find the path specified.
FF:"00001767WS#_oprlogprio7_90309592.zip.mime"
The system cannot find the path specified.
FF:"00001768WS#_oprlogprio7_90310050.zip.mime"
The system cannot find the path specified.
FF:"00001769WS#_oprlogprio7_90309593.zip.mime"
The system cannot find the path specified.
FF:"00001770WS#_oprlogprio7_90309703.zip.mime"
The system cannot find the path specified.
FF:"00001771WS#_oprlogprio7_90310226.zip.mime"
The system cannot find the path specified.
FF:"00001772WS#_oprlog7_90309651.zip.mime"
The system cannot find the path specified.
FF:"00001773WS#_oprlogprio7_90309569.zip.mime"
The system cannot find the path specified.
FF:"00001774WS#_oprlog7_90310227.zip.mime"
The system cannot find the path specified.
FF:"00001775WS#_oprlogprio7_90309588.zip.mime"
The system cannot find the path specified.
FF:"00001776WS#_oprlogprio7_90309569.zip.mime"
The system cannot find the path specified.
FF:"00001777WS#_oprlogprio7_90311360.zip.mime"
The system cannot find the path specified.
FF:"00001778WS#_oprlogprio7_90309569.zip.mime"
The system cannot find the path specified.
FF:"00001779WS#_oprlogprio7_90310448.zip.mime"
The system cannot find the path specified.
FF:"00001780WS#_oprlogprio7_90310457.zip.mime"
The system cannot find the path specified.
FF:"00001781WS#_oprlogprio7_90310151.zip.mime"
The system cannot find the path specified.

D:\Backup>


Report •

#11
January 23, 2010 at 03:46:14
Fantastic code... Blown away...

I have to sit down and look at this a lot harder to work out what it is doing exactly. No changes have been made to the files after running the script. I'm not sure why? It seems that it should but the script then complains that it can't find any of the files and that doesn't make sense?


Report •

#12
January 23, 2010 at 05:33:48
Try ECHO ON instead of ECHO OFF

Report •

#13
January 23, 2010 at 12:38:20
are you running it from the same directory the target files are in? that was how i had it set up here. also, it moves the files to another directory (..\g). try and verify if they are showing up somewhere else on your disk. and as tvc said, try it with the echo on.
You can set it up to run from a different directory, but why bother...

Report •

#14
January 24, 2010 at 04:15:33
Yeah it was in the same folder D:\Backup> and it was named
Z.bat and no file was moved and no folder was created named
"g" and no random files either anywhere on the laptop. Very
strange cause the script appears to create file list but does not
change the file. After running it on 1 file with echo on I got an
output file that was very large so captured the beginning and
end of it. I'll give it another go with a few other files to see what
results I'll get. Will post anything interesting.

Report •

#15
January 24, 2010 at 12:35:58
uuh, i just reviewed what i did and noticed i did not put:
md ..\g
into my code anywhere! I had already manually created that dir. so my code ran ok, but when i went back and ran it again, (without directory g), it gave the same results you got.
try creating the directory, or else add it into the code (pre-loop, it only needs done once).
on my system:
c:\work\temp\f
contains the initial filenames, created out of your filelist.txt
it also contains the batchscript.
c:\work\temp\g
starts out empty, and end up filled with 00000001WS#_...
00000002WS#_... 00001782WS#_...

sorry about this oversight, my fault (assuming that this is in deed the source of the errors).


Report •

#16
January 24, 2010 at 18:43:16
AWESOME... Yeah didn't think of that... FANTASTIC... Run that script then the other one and I have 100% coverage of file.

Absolutely perfect you totally ROCK... :)

Thanks heaps...


Report •


Ask Question