Page 1 of 1

Add date to script?

Posted: 20 Nov 2014 06:00
by Daans180
Hello i have a move script but i like to add a date function so i can, lets see move all files older then >.....

this is what i have allready.

who can help me.
kind regards,


Code: Select all

echo off
SETLOCAL EnableDelayedExpansion
set movedFiles=0
for /R V:\Administratie\CS\Pakbon_Summaries\ %%G in (*) do (
    echo moving... "%%G"
    move /Y "%%G" V:\Administratie\CS\Pakbon_Summaries_per_maand\
    set /a movedFiles+="1"
        if !movedFiles! EQU 1000 GOTO endOfCopy rem if you moved 500 files
  )
  :endOfCopy
  echo Done, %movedFiles% files Where copied successfully
  pause
ENDLOCAL


MOD EDIT: PLEASE USE CODE TAGS

Re: Add date to script?

Posted: 20 Nov 2014 09:40
by foxidrive
This isn't fully tested: change the source and target folders to a test folder location, and test this with dummy files first.

The date to compare with is given as the first parameter to the batch file in YYYYMMDD format: such as MyBatch.bat 20130631

Code: Select all

@echo off
if "%~1"=="" (
   echo Moves filenames with modified date earlier than the date given as %%1 (in YYYYMMDD format^)
   echo/
   echo example: "%~f0" 20131229
   echo/
   pause
   goto :EOF
   )

:loop
set "file=%temp%\movefile-%random%.vbs"
if exist "%file%" goto :loop


:: change the source folder and filespec here

dir "V:\Administratie\CS\Pakbon_Summaries\*.*" /s /b /a-d >"%file%.list"

:: change the target folder here

set "target=V:\Administratie\CS\Pakbon_Summaries_per_maand\"

:: Create a list of files with modified dates in this format: YYYYMMDD "filename"

(
 echo. Const ForReading = 1, ForWriting = 2
 echo. infile = "%file%.list"
 echo. Set fso = CreateObject("Scripting.FileSystemObject"^)
 echo. Set f1 = fso.OpenTextFile(infile, ForReading^)
 echo. Do While not f1.AtEndOfStream
 echo. Set f = fso.GetFile(f1.readline^)
 echo. ' n = f.DateLastAccessed
 echo. ' nc = f.DateCreated
 echo. nm = f.DateLastModified
 echo. WScript.Echo CStr(Year(nm^)^)+Right(100+Month(nm^),2^)+Right(100+Day(nm^),2^)+" "+chr(34^)+f+chr(34^)
 echo. loop
 echo. f1.close
)>"%file%"
cscript //nologo "%file%" >"%file%.txt"

:: show the list of files in this format: modified-date "filename"
:: type "%file%.txt"&pause

set c=0
for /f "usebackq tokens=1,*" %%a in ("%file%.txt") do (
      if %%a LSS %~1 (
         echo/Creation date %%a - moving "%%~b"
         set /a c+=1
         move /Y "%%~b" "%target%" >nul
      )
   )
echo done - %c% files were moved.

del "%file%" "%file%.txt"  "%file%.list"
pause

Re: Add date to script?

Posted: 20 Nov 2014 09:50
by Squashman
Hi Foxidrive,
Seen you use this code twice in the last few days. Is this more flexible then just using FORFILES?

Re: Add date to script?

Posted: 20 Nov 2014 10:00
by Yury

Code: Select all

@echo off

set "dir=D:\Test"
set age=7

pushd "%dir%"
set movedFiles=0
for /f "tokens=*" %%G in ('
 robocopy . %random% /nc /ns /ndl /njh /njs /l /minage:%age%
') do (
 echo Moving:.. "%%G"
 move /y "%%G" "V:\Administratie\CS\Pakbon_Summaries_per_maand\"&& (
 set /a movedFiles+=1
 )
 for /f %%# in ('set /a movedFiles') do if %%# equ 1000 goto endOfCopy
 )
popd

:endOfCopy
echo.
echo Done: %movedFiles% files were moved successfully.
echo.
pause
exit /b 0

Re: Add date to script?

Posted: 25 Nov 2014 06:23
by Daans180
thanks all
this is working for me now
the last script worked fine

Thanks again
Daniel

Re: Add date to script?

Posted: 25 Nov 2014 06:43
by foxidrive
Squashman wrote:Hi Foxidrive,
Seen you use this code twice in the last few days. Is this more flexible then just using FORFILES?


The first case compared creation + modified dates which I think could be difficult for forfiles

I haven't tested it but using the VBS script could be quite a bit faster for a lot of files.

Re: Add date to script?

Posted: 26 Nov 2014 02:23
by Daans180
And a small next question,
What shoul you adjust in the script if you like to move file between a particular date:

let say files in a particular week or day,



@echo off

set "dir=V:\Administratie\CS\Pakbon_Summaries"
set age=299

pushd "%dir%"
set movedFiles=0
for /f "tokens=*" %%G in ('
robocopy . %random% /nc /ns /ndl /njh /njs /l /minage:%age%
') do (
echo Moving:.. "%%G"
move /y "%%G" "V:\Administratie\CS\Pakbon_Summaries_per_maand_2\"&& (
set /a movedFiles+=1
)
for /f %%# in ('set /a movedFiles') do if %%# equ 1000 goto endOfCopy
)
popd

:endOfCopy
echo.
echo Done: %movedFiles% files were moved successfully.
echo.
pause
exit /b 0

Re: Add date to script?

Posted: 26 Nov 2014 06:09
by Yury
Daans180 wrote:What shoul you adjust in the script if you like to move file between a particular date



Code: Select all

@echo off

set "from=D:\Test 1"
set "to=D:\Test 2"
set maxage=20141010
set minage=20141110

pushd "%from%"
set movedFiles=0
for /f "tokens=*" %%G in ('
 robocopy . %random% /nc /ns /ndl /njh /njs /l /minage:%minage% /maxage:%maxage%
') do (
 echo Moving:.. "%%G"
 move /y "%%G" "%to%\"&& (
 set /a movedFiles+=1
 )
 for /f %%# in ('set /a movedFiles') do if %%# equ 1000 goto endOfCopy
 )
popd

:endOfCopy
echo.
echo Done: %movedFiles% files were moved successfully.
echo.
pause
exit /b 0

Re: Add date to script?

Posted: 02 Dec 2014 02:10
by Daans180
Thanks for the reply this works for me,

Great forum for starters at least,

will be back.
Daniel 8)

Re: Add date to script?

Posted: 02 Dec 2014 19:35
by Squashman
Daans180 wrote:Thanks for the reply this works for me,

Great forum for starters at least,

will be back.
Daniel 8)

Hopefully to learn and contribute.