Jump to content

Batch File Coding Help


MXs_369
 Share

Recommended Posts

REM Made by Niklas Jumlin. http://blog.jumlin.com

@echo off

cls

setLocal EnableDelayedExpansion

setLocal EnableExtensions

REM for safety precautions, nothing will actually run or be modified if you run this script.

REM to actually make us of this script you should edit the safety variable below to "no"

set safety=yes

if /i "%safety%"=="no" set run=ECHO

if /i "%safety%"=="no" set run=@

set yourOldFolder=C:\Cadwell\Sierra Wave\Data.Cadwell

set yourNewFolder=\\nsm001\AppData\Backup\CADWELL\EMG Mesa

set logfile=%AppData%\filemove_log.txt

set TmpFile=%AppData%\variables.txt

set debug=no

REM possible formats are "mm dd yyyy", "dd mm yyyy" and "yyyy mm dd"

set DateFormat=mm dd yyyy

REM possible separators are - (dash), / (slash) or . (dot)

set DateSeparator=-

REM script

dir /s /b /a:-D %yourOldFolder% > %TmpFile%

for /f "Tokens=* Delims= " %%a In (%AppData%\variables.txt) Do (

REM from now on, to use variables set within this FOR loop, an expression sign (!) is needed at start and end of the variable. e.g !var! instead of %var%

REM this also requires: setLocal EnableDelayedExpansion

REM set a variable to modified date of the current file

call set md=%%~ta

REM split up dd/mm/yyyy hh:mm to yyyy, mm and dd

if "%DateFormat%"=="dd mm yyyy" (

for /f "Tokens=1,2,3 Delims=%DateSeparator% " %%i In ('echo !md!') do set md=%%i& set mm=%%j& set my=%%k

)

REM split up yyyy-mm-dd hh:mm to yyyy, mm and dd

if "%DateFormat%"=="yyyy mm dd" (

for /f "Tokens=1,2,3 Delims=%DateSeparator% " %%i In ('echo !md!') do set my=%%i& set mm=%%j& set md=%%k

)

REM split up mm/dd/yyyy hh:mm to yyyy, mm and dd

if "%DateFormat%"=="mm dd yyyy" (

for /f "Tokens=1,2,3 Delims=%DateSeparator% " %%i In ('echo !md!') do set mm=%%i& set md=%%j& set my=%%k

)

REM merge vars into one var

call set mdate=!my! !mm! !md!

REM remove spaces in mdate

call set mdate=!mdate: =!

REM debug1

if /i "%debug%"=="yes" (

echo CASE:

echo File: %%~na

echo Path: %%~pa

echo Filetype: %%~xa

echo Filesize: %%~za byte

echo Full filepath: %%a

echo Full path: %%~da%%~pa

echo Modified date: !mdate!

)

REM end of debug1

REM get current date, split up dd/mm/yyyy hh:mm to yyyy, mm and dd

if "%DateFormat%"=="dd mm yyyy" (

for /f "Tokens=1-4 Delims=%DateSeparator%" %%i In ('date /t') Do set cd=%%i& set cm=%%j& set cy=%%k

)

REM get current date, split up yyyy-mm-dd hh:mm to yyyy, mm and dd

if "%DateFormat%"=="yyyy mm dd" (

for /f "Tokens=1-4 Delims=%DateSeparator%" %%i In ('date /t') Do set cy=%%i& set cm=%%j& set cd=%%k

)

REM get current date, split up mm/dd/yyyy hh:mm to yyyy, mm and dd

if "%DateFormat%"=="mm dd yyyy" (

for /f "Tokens=1-4 Delims=%DateSeparator%" %%i In ('date /t') Do set cm=%%i& set cd=%%j& set cy=%%k

)

REM set YYYY - (MINUS) 1 year

call set /a cy=!cy!-1

REM merge vars into one var

call set cdate=!cy! !cm! !cd!

REM remove spaces in cdate

call set cdate=!cdate: =!

REM debug2

if /i "%debug%"=="yes" (

echo.

echo ACTION:

echo "%%~nxa" is modified "!mdate!"

echo If it would have been modified "!cdate!" it would be moved.

echo.

echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

echo.

echo See how the action would look like?

REM confirm

set /p confirm=y/n?:

if /i "!confirm!"=="y" (

cls

echo move %%a %yourNewFolder%\olderThanOneYear%%~pa%%~nxa

echo.

pause

cls

)

REM end of confirm

)

REM end of debug2

REM actual job

if !mdate! LEQ !cdate! (

REM write to logfile

echo. >> %logfile%

echo File: %%a >> %logfile%

echo Modified: !mdate! - above file is more than one year old, will be moved >> %logfile%

echo New path: %yourNewFolder%\olderThanOneYear%%~pa%%~nxa >> %logfile%

echo Will delete, if empty: %%~da%%~pa >> %logfile%

REM create directory structure of parent directories in %yourNewFolder% path. Requires: setLocal EnableExtensions

%run% mkdir %yourNewFolder%\olderThanOneYear\%%~pa

REM move file to new destination

%run% move %%a %yourNewFolder%\olderThanOneYear%%~pa%%~nxa

REM delete parent directory if empty

%run% rmdir %%~da%%~pa

)

REM end of actual job

)

REM end of script

:exit

echo.

echo Logfile: %logfile%

echo.

EndLocal

pause

the original script (which is not mine just to note) is by one year

REM set YYYY - (MINUS) 1 year

call set /a cy=!cy!-1

REM merge vars into one var

call set cdate=!cy! !cm! !cd!

REM remove spaces in cdate

call set cdate=!cdate: =!

what do i need to change this 2 month time frame

thanks,

Link to comment
Share on other sites

I think you need to change this part of code, to months instead of years.

REM set YYYY - (MINUS) 1 year
call set /a cy=!cy!-1	
REM merge vars into one var
call set cdate=!cy! !cm! !cd!
REM remove spaces in cdate
call set cdate=!cdate: =!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...