md and move Help experts

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

Re: md and move Help experts

#16 Post by aGerman » 09 May 2011 14:27

Well, if I asked you for real!!! examples I didn't ask out of couriosity. But latest you gave file names without the trailing _m. When I asked you for the output of echo %date% you forgot the seperator (BTW: I still do not know it). Last but not least if you asked for a code that moves the first 100 .tiff files together with the first 50 .xml files etc. it might work in most cases but actually it wasn't the real requirement.
You should keep in mind that I'm only as good as your specifications and your requirement.

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
  set "fileName=%%~nb"
  for /f "delims=" %%c in ('dir /a-d /b /on "%root%\images\!fileName:~,-1!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

#17 Post by renzlo » 09 May 2011 17:19

thanks aGerman and im sorry for not being specific, i will try the code soon. Thanks for all the help.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: md and move Help experts

#18 Post by dbenham » 10 May 2011 12:48

Hi aGerman

From an early post in this thread
aGerman wrote: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.

Below is an alternative :GetDate function that does not rely on calls to the registry. I adapted techniques from the DosTips :Unique function. Would it not work for the vast majority, regardless of country?

Code: Select all

:GetDate yOut mOut dOut
setlocal
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('"echo:|date"') do (
  for /f "tokens=1-3 delims=/.- " %%A in ("%date:* =%") do (
    set %%a=%%A
    set %%b=%%B
    set %%c=%%C
  )
)
set /a "yy=10000%yy% %%10000, mm=100%mm% %%100, dd=100%dd% %%100"
(endlocal
  set %~1=%yy%
  set %~2=%mm%
  set %~3=%dd%
)
exit /b

One thing puzzles me about the following:

Code: Select all

rem from my proposed :GetDate alternative (adapted from :Unique)
for /f "tokens=1-3 delims=/.- " %%A in ("%date:* =%") do (

rem from aGerman's code
set "xDate=%date%"
for /f "tokens=2" %%i in ("%xDate%") do set "xDate=%%i"

Specifically I don't understand the need for %date:* =% in "my" code, and for /f "tokens=2" in your code. Both sets of code are allowing for a leading token prior to the delimited numeric date string. On my Vista machine in the U.S. I have no such token. What is this mysterious token?

Thanks

Dave Benham

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

Re: md and move Help experts

#19 Post by aGerman » 10 May 2011 14:36

Hi Dave.

I knew your approach before and (with some minor changes) it would work for me as well. First let me show you my output of echo:|date

Code: Select all

C:\>echo:|date
Aktuelles Datum: 10.05.2011
Geben Sie das neue Datum ein: (TT-MM-JJ)

C:\>

Well, as you can see your solution isn't language independent, because of the following translation:
day = Tag
month = Monat
year = Jahr
So, it would be simple to replace TT by dd and JJ by yy, but this doesn't protect you from errors with other languages.
________________________________________________________________________________________________________

On an older machine I saw the following output of echo %date%:
dddd, yyyy-mm-dd
That means for some reason the weekday may be prepended to the date (I was not able to find out why). For that reason I trim the 1st token in case the output has two tokens.
BTW I like the manner you did it :)

Regards
aGerman

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

Re: md and move Help experts

#20 Post by renzlo » 10 May 2011 17:59

hi aGerman,thanks for everything, your script is working great. I wish i could be as good as you are.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: md and move Help experts

#21 Post by dbenham » 10 May 2011 21:33

Thanks aGerman for your clear explanations.

Armed with your information I think I have developed the beginnings of a true "universal" date parser that does not need access to the registry. I'll post it on a new thread.

Dave Benham

Post Reply