md and move Help experts

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
renzlo
Posts: 116
Joined: 03 May 2011 19:06

md and move Help experts

#1 Post by renzlo » 03 May 2011 20:55

I am a newbie, i have tried coding this in batch but i ended up coding it forever and it created a very huge batch file which I know some experts can put it in a simple code, I am now asking for your help guys.

Here's the situation:
I have two folders, located in C:\Mine\, Folder1 named images and Folder2 named mdata. images folder has thousands of tiff/tif images there and mdata folder has xml files there, the amount of xml is based on the amount of tiff files divided by two. So, for example I have 1000 tiff files the xml files would be 500.

Now, I want to make a directory on the C:\Mine\ and the name should be EN(current date:MMDD)001 for example EN0504001, move the first 100 tiff files (should be sorted by name) on the created directory and move first 50 xml files (should be sorted by name) on the created directory too and after that it will make a directory again EN(current date:MMDD)002 (sequenced directory) and will do the same as the previous commands, and will continue making directory in sequence and move the tiff and xml files until the images folder and mdata folder went empty.

I know this is possible. Any help will be much appreciated.

Thank you for your time.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: md and move Help experts

#2 Post by aGerman » 04 May 2011 16:34

Try it this way:

Code: Select all

@echo off &setlocal enabledelayedexpansion

set "root=c:\mine"
set /a numTiffs=100
set /a numXmls=numTiffs / 2

call :GetDate y m d
set /a m=10%m%, d=10%d%
set "basename=EN%m:~-2%%d:~-2%"

set i=0
for /f "tokens=1* delims=:" %%a in ('dir /a-d /b /on "%root%\images\*.tif?"^|findstr /n .') do (
  set /a n=%%a %% numTiffs - 1
  if !n!==0 (
    set /a i+=1
    set "j=00!i!"
    set "name=%root%\%basename%!j:~-3!"
    md "!name!" 2>nul
  )
  move "%root%\images\%%~b" "!name!\"
)

set i=0
for /f "tokens=1* delims=:" %%a in ('dir /a-d /b /on "%root%\mdata\*.xml"^|findstr /n .') do (
  set /a n=%%a %% numXmls - 1
  if !n!==0 (
    set /a i+=1
    set "j=00!i!"
    set "name=%root%\%basename%!j:~-3!"
    md "!name!" 2>nul
  )
  move "%root%\mdata\%%~b" "!name!\"
)
goto :eof


:GetDate yOut mOut dOut
setlocal
for /f "tokens=3" %%a in ('reg query "HKCU\Control Panel\International" /v iDate') do set "iDate=%%a"
for /f "tokens=3" %%a in ('reg query "HKCU\Control Panel\International" /v sDate') do set "sDate=%%a"
for /f "tokens=2" %%i in ("%date%") do set "date=%%i"
for /f "tokens=1-3 delims=%sDate%" %%a in ("%date%") do (
  if %iDate%==0 set /a mm=100%%a%%100,dd=100%%b%%100,yy=10000%%c%%10000
  if %iDate%==1 set /a dd=100%%a%%100,mm=100%%b%%100,yy=10000%%c%%10000
  if %iDate%==2 set /a yy=10000%%a%%10000,mm=100%%b%%100,dd=100%%c%%100
)
endlocal &set "%~1=%yy%" &set "%~2=%mm%" &set "%~3=%dd%"
goto :eof

Regards
aGerman

renzlo
Posts: 116
Joined: 03 May 2011 19:06

Re: md and move Help experts

#3 Post by renzlo » 04 May 2011 18:49

hey aGerman thanks for the script,its working fine and great. By the way, i would like to ask the following:

1. What if i dont have access to registry?
2. What would be the additional script if i want to create a folder base on user input? For example, i need to start the folder filename to 3,meaning EN0505003 will be created and files will be move there and will do the sequence starting from 3.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: md and move Help experts

#4 Post by aGerman » 05 May 2011 10:44

renzlo wrote:1. What if i dont have access to registry?

As I explained so many times DateTime (type of data) values are unknown for a batch code. What you can find in variable %date% is only a string of characters. It depends on your registry settings how it looks like (the order of year, month and day as well as the used seperator). For my german settings that means DD.MM.YYYY, but for other settings it could be something like MM/DD/YYYYY or YYYY-MM-DD or something similar. Because I have no idea what
echo %date%
would display in your case I had no chance but getting the order and the separator from your registry. Tell me how it looks like for you and I will adapt the code without reading registry values.

renzlo wrote:2. What would be the additional script if i want to create a folder base on user input? For example, i need to start the folder filename to 3,meaning EN0505003 will be created and files will be move there and will do the sequence starting from 3.

It's the value of %i% that you have to customize. If you answered the first question I could insert this feature as well.

Regards
aGerman

renzlo
Posts: 116
Joined: 03 May 2011 19:06

Re: md and move Help experts

#5 Post by renzlo » 05 May 2011 19:55

thanks for the reply aGerman,

1. Its yyyymmdd.
2. Thanks.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: md and move Help experts

#6 Post by aGerman » 06 May 2011 12:06

renzlo wrote:1. Its yyyymmdd.

Sorry but I can't believe it. The order of data might be correct, but I miss the separator (slash, dash or something like that).
You should double check the output of
echo %date%
and change the value in line
set "dateSeparator=..."
in the code below!

Code: Select all

@echo off &setlocal enabledelayedexpansion

:: SETTINGS
set "root=d:\mine"      &REM root folder
set /a numTiffs=100     &REM number of .tiff files placed in a single sub folder
set "dateOrder=y m d"   &REM order of year, month and day in %date% (space separated)
set "dateSeparator=-"   &REM separator of year, month and day in %date%

:: CALCULATE PREDEFINED VARIABLES
set /a numXmls=numTiffs / 2
set "xDate=%date%"
for /f "tokens=2" %%i in ("%xDate%") do set "xDate=%%i"
for /f "tokens=1-3" %%a in ("%dateOrder%") do (
  for /f "tokens=1-3 delims=%dateSeparator%" %%d in ("%xDate%") do (
    set /a %%a=10000%%d%%10000,%%b=10000%%e%%10000,%%c=10000%%f%%10000
  )
)
set /a m=10%m%, d=10%d%
set "basename=EN%m:~-2%%d:~-2%"

:: WAIT FOR USER INPUT
:loop
set /p "inp=Sequence starting from number: "
echo("%inp%"|findstr /vrxc:"\"[0-9][0-9]*\"" >nul &&goto :loop
set /a inp-=1

:: MOVE .TIFF FILES
set /a i=inp
for /f "tokens=1* delims=:" %%a in ('dir /a-d /b /on "%root%\images\*.tif?"^|findstr /n .') do (
  set /a n=%%a %% numTiffs - 1
  if !n!==0 (
    set /a i+=1
    set "j=00!i!"
    set "name=%root%\%basename%!j:~-3!"
    md "!name!" 2>nul
  )
  move "%root%\images\%%~b" "!name!\"
)

:: MOVE .XML FILES
set /a i=inp
for /f "tokens=1* delims=:" %%a in ('dir /a-d /b /on "%root%\mdata\*.xml"^|findstr /n .') do (
  set /a n=%%a %% numXmls - 1
  if !n!==0 (
    set /a i+=1
    set "j=00!i!"
    set "name=%root%\%basename%!j:~-3!"
    md "!name!" 2>nul
  )
  move "%root%\mdata\%%~b" "!name!\"
)


Regards
aGerman

renzlo
Posts: 116
Joined: 03 May 2011 19:06

Re: md and move Help experts

#7 Post by renzlo » 06 May 2011 22:42

Hi again aGerman, thanks for everything,it is working fine now, everytime you share your script with me I learn. Thanks for not getting tired helping newbies like me. By the way, I put your name on credits part of my batch file.

I would like to ask for the last time.

1. I need to create a summary report of that created directory with the tiff files and xml file. For example, EN0507001 is created, now inside that folder EN0507001.txt will be created with this contents:

=======================================================
Count----------------------------XML Files---------------Image Count
1 ------------------------------random_001.xml-------------- 2
2 ------------------------------random_002.xml-------------- 2
.
.
.
.
50-----------------------------random_002.xml-------------- 2
=======================================================

Total IMG Files: 100
Total XML Files: 50

End of Report

2. To explain the above summary report, the contents of my images file is named like this, "randomtext0001_C1.tiff", "randomtext0001_C2.tiff", "randomtext0002_C1.tiff", "randomtext0002_C2.tiff","randomtext0003_C1.tiff", "randomtext0003_C2.tiff" ....... "randomtext0050_C1.tiff", "randomtext0050_C2.tiff". The xml folder contents are the following: "randomtext0001.xml","randomtext0002.xml","randomtext0003.xml" ............ "randomtext0050.xml", that's why it has 2 image counts per xml.

Thanks for your time again.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: md and move Help experts

#8 Post by aGerman » 07 May 2011 15:20

It's doable, but when I read your last post I noticed that I should write a new code for you. The batch file should move the xml files first, then search the associated tiff files and move them. Otherwise your "summary report" would not make any sense.
Before I start writing the code, please give some real examples of xml file names and associated tiff file names.

Regards
aGerman

renzlo
Posts: 116
Joined: 03 May 2011 19:06

Re: md and move Help experts

#9 Post by renzlo » 07 May 2011 21:39

thanks aGerman, here are the sample real filename.

xml: 0001067426_001_Amsterdam01_1124533.xml
associated tiffs:0001067426_001_Amsterdam01_1124533_c1.tiff and 0001067426_001_Amsterdam01_1124533_c2.tiff

by the way, "Amsterdam01_1124533" is changing in xml and tiff files. 0001067426_001 and _c1 and _c2 is not changing.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: md and move Help experts

#10 Post by aGerman » 08 May 2011 05:56

I see. This should work:

Code: Select all

@echo off &setlocal enabledelayedexpansion

:: SETTINGS
set "root=d:\mine"      &REM root folder
set /a numXmls=50       &REM number of .xml files placed in a single sub folder
set "dateOrder=y m d"   &REM order of year, month and day in %date% (space separated)
set "dateSeparator=-"   &REM separator of year, month and day in %date%

:: CALCULATE PREDEFINED VARIABLES
set "xDate=%date%"
for /f "tokens=2" %%i in ("%xDate%") do set "xDate=%%i"
for /f "tokens=1-3" %%a in ("%dateOrder%") do (
  for /f "tokens=1-3 delims=%dateSeparator%" %%d in ("%xDate%") do (
    set /a %%a=10000%%d%%10000,%%b=10000%%e%%10000,%%c=10000%%f%%10000
  )
)
set /a m=10%m%, d=10%d%
set "basename=EN%m:~-2%%d:~-2%"

:: WAIT FOR USER INPUT
:loop
set /p "i=Sequence starting from number: "
echo("%i%"|findstr /vrxc:"\"[0-9][0-9]*\"" >nul &&goto :loop
set /a i-=1

:: MOVE .XML AND .TIFF FILES, WRITE SUMMARY REPORT
for /f "tokens=1* delims=:" %%a in ('dir /a-d /b /on "%root%\mdata\*.xml"^|findstr /n .') do (
  set /a n=%%a %% numXmls - 1
  if !n!==0 (
    if defined name (
      >>"!txtName!" echo ==============================================================================
      >>"!txtName!" echo(
      >>"!txtName!" echo Total XML Files: !totalXML!
      >>"!txtName!" echo Total IMG Files: !totalIMG!
    )
    set /a i+=1, totalXML=0, totalIMG=0, xmlCount=0
    set "j=00!i!"
    set "name=%root%\%basename%!j:~-3!"
    set "txtName=!name!\%basename%!j:~-3!.txt"
    md "!name!" 2>nul
    >"!txtName!" echo ==============================================================================
    >>"!txtName!" echo Count  XML Files                                                   Image Count
  )
  move "%root%\mdata\%%~b" "!name!\"
  set /a imgCount=0, totalXML+=1, xmlCount+=1
  for /f "delims=" %%c in ('dir /a-d /b /on "%root%\images\%%~nb_c*.tif?"') do (
    move "%root%\images\%%~c" "!name!\"
    set /a imgCount+=1, totalIMG+=1
  )
  call :add_spaces "!xmlCount!" 7 strNum
  call :add_spaces "%%~b" 60 strName
  >>"!txtName!" echo !strNum!!strName!!imgCount!
)
if defined name (
  >>"%txtName%" echo ==============================================================================
  >>"%txtName%" echo(
  >>"%txtName%" echo Total XML Files: %totalXML%
  >>"%txtName%" echo Total IMG Files: %totalIMG%
)
goto :eof


::SUB ROUTINE FOR CREATING A FIXED STRING LENGTH
:add_spaces stringIn lengthIn newStringOut
setlocal
set "spaces=                                                            "
set "out=%~1%spaces%"
set "out=!out:~,%~2!"
endlocal &set "%~3=%out%"
goto :eof

Regards
aGerman

renzlo
Posts: 116
Joined: 03 May 2011 19:06

Re: md and move Help experts

#11 Post by renzlo » 08 May 2011 21:07

hi aGerman thanks. Thanks for the time, I really appreciate it. By the way, when I execute the batch with your code, the error "Divide by zero error" appears.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: md and move Help experts

#12 Post by aGerman » 09 May 2011 00:32

Worked in my tests. Please double check the settings of dateOrder and dateSeparator.

Regards
aGerman

renzlo
Posts: 116
Joined: 03 May 2011 19:06

Re: md and move Help experts

#13 Post by renzlo » 09 May 2011 03:25

hi aGerman,the batch moved the xml files and create a report but when moving tiff files it keeps on saying file not found.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: md and move Help experts

#14 Post by aGerman » 09 May 2011 04:37

Hmm, if the batch file moved
0001067426_001_Amsterdam01_1124533.xml
it is looking for
0001067426_001_Amsterdam01_1124533_c*.tiff
where the asterisk is for any character.

What could be wrong?

Regards
aGerman

renzlo
Posts: 116
Joined: 03 May 2011 19:06

Re: md and move Help experts

#15 Post by renzlo » 09 May 2011 08:26

i forgot to mention that the xml files ends in *_M.xml. Is that the problem?

Post Reply