Windows scripting

Hi,
I have the following folder hierarchy:
Foldersource
           FolderX
                  File1
                  File2
          FolderY
                  File1
                  File2
I like to write a script that checks the file1 modified date and then based on that create a folder with that date as a name and place the entire hierarchy under foldersource under the target directory which is the archivefolder.

Thanks in advance
shmzAsked:
Who is Participating?
 
NVITCommented:
Here's a version that creates FolderTarget folder names based on the oldest file in each subfolder of Foldersource, i.e. FolderX, FolderY, etc. e.g. FolderTarget\FolderX\01_01_2015_03_46_PM

- Change srcdir and arcdir to your needs
- This version just echos the robocopy command for visual purposes. To run robocopy for real, remove the ECHO prefix

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

set SrcDirRoot=C:\Local\test\Foldersource
set ArcDir=C:\Local\test\FolderTarget

for /d %%A in (%SrcDirRoot%\*) do (
  REM Based on oldest file in SrcDirFull
  set SrcDirFull=%%A
  set SrcDir=%%~nA
  pushd "!SrcDirFull!"
  REM 01_01_2015_03_46_PM
  for /f %%X in ('dir /b /a-d /o-d') do (
    set fd=%%~tX
    REM 01/01/2015 03:46 PM
    for /f "tokens=1-6 delims=/: " %%a in ("!fd!") do (set /a MM=%%a& set DD=%%b& set YYYY=%%c& set Hr=%%d& set Min=%%e& set AMPM=%%f)
    if !MM! lss 10 set MM=0!MM!
    set fd=!YYYY!_!MM!_!DD!_!Hr!_!Min!_!AMPM!
  )
  ECHO robocopy /E /DCOPY:T "!SrcDirFull!" %ArcDir%\!SrcDir!\!fd!\
  popd
)

Open in new window

0
 
NVITCommented:
> ...checks the file1 modified date and then based on that create a folder with that date as a name
You have File1 under both FolderX and FolderY. Which File1 to use?

Here's a solution based on FolderX\File1:
- Change srcdir and arcdir to your needs

SETLOCAL ENABLEDELAYEDEXPANSION

set srcdir=C:\Local\test\Foldersource
set arcdir=C:\Local\test\FolderTarget

for %%A in (%srcdir%\FolderX\File1.txt) do (
  set fd=%%~tA
  set fd=!fd: =_!
  set fd=!fd:/=_!
  set fd=!fd::=_!
  robocopy /E "%srcdir%" %arcdir%\!fd!\
)

Open in new window

0
 
Bill PrewCommented:
So, do all the files under the foldersource have the same date?  If not, then it would be important what order the folders and files were "looked for", should that be by name, or something else?

Or, are you looking for one specific file in a defined folder, as NewVillageIT assumed in their approach?

Do you want to MOVE the files, or COPY them?

~bp
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
shmzAuthor Commented:
Hi,
Both folders are uploaded at the same time. My intention is to get only one file from one of the folders, check if the modified date is today's date and copy everything.
I guess if I move the folders then I do to have  
To worry about checking the date but at this stage I only want to copy.
Newbillage: I tried your code but nothing happens. The cmd shows the time is extracted correctly and is in the archive path but no directory created, nor filed copied.
0
 
shmzAuthor Commented:
Other thing I intend to do in archive folder is to see this hierarchy:

Archive
      2015_04_17_06_AM
            FolderX
                File......
             FolderY
                 File.
0
 
shmzAuthor Commented:
NewvillageIT

It woks!
Thank you so much. I will come back to ask some questions later. Thank you :)
0
 
NVITCommented:
> The cmd shows the time is extracted correctly and is in the archive path but no directory created, nor filed copied.

I think it's working. As I mentioned, you need to remove the ECHO fronting ROBOCOPY
0
 
shmzAuthor Commented:
NewvillageIT:
The script only creates folder with files modified date, it does not check if it match with today's date and then archive them.

It should not archive previous days.
0
 
NVITCommented:
> ...write a script that checks the file1 modified date and then based on that create a folder with that date as a name
That was your original request. I see you have revised that in a later post.

> ... My intention is to get only one file from one of the folders, check if the modified date is today's date and copy everything.
Are there multiple files in that folder? If so, as long as one file matches today's date, then archive all?

Also, does the file have to be in FolderX? Could it be in FolderY?
0
 
shmzAuthor Commented:
Sorry I should have clarified that.
There are multiple files in both folders. Another script load these files into these folders every morning.  All files in both folders have same time so checking
One date is sufficient for all.
Sometimes there is no file in these fders and hence nothing will be archived.
0
 
shmzAuthor Commented:
Would you also let me know what would be the 'move' command be if I exchange it with Dcopy? For future?
0
 
NVITCommented:
Question:
In this example, I believe you want to copy FolderX and FolderY but not FolderZ. Is that true?
Foldersource
  FolderX
    TodayFile1
    TodayFile2
  FolderY
    TodayFile1
    TodayFile2
  FolderZ
    YesterdayFile1
    YesterdayFile2

Open in new window


...to the archive like:
Archive
  2015_04_17_08_06_AM
    FolderX
      TodayFile1
      TodayFile2
    FolderY
      TodayFile1
      TodayFile2

Open in new window

0
 
shmzAuthor Commented:
FolderX and FolderY are always in sourceFolder.(their modified dates are old)
There is no other folder.
The content of folders being updated on daily basis. Files being overwritten. Same number of files.
I.e. If I don't archive files today, files will be overwritten with new data tomorrow, and I'll lose today's data.
However, when I copy them to archive I will copy them with their respective folderX and folderY.

Let's make it simple and forget about date comparison. Let's 'move' files to archive (as per my explanation in this comment) and then delete ( or "move" includes delete as well?) content of FolderX and folderY under sourceFolder.
And if no file uploaded in a given day then content of folders are empty and no archiving done.
I think the script is currently doing this and we need to use move instead of copy and check if files don't exist then don't archive. Just considering
In sourceFolder, folderX and FolderY stay but
In archive folder we have respective folders under the date folder.
0
 
shmzAuthor Commented:
Source folder
   FolderX.            23/05/2014
       File1.             17/04/2015
       File2.              17/04/2015
  FolderY.              23/05/2014
       FileA.              17/04/2015
 
After running the script, here is the expected result  
Archive
      2015_04_17_07_00_AM
             FolderX.     23/05/2014
                File1.       17/04/2015
                File2.        17/04/2015
             FolderY.       23/05/2014
                FileA.        17/04/2015

SourceFolder
    FolderX.      23/05/2014
    FolderY.       23/05/2014
0
 
Bill PrewCommented:
Give this a try.  It will copy only the files that are from today, and place them in the proper subfolders.  I used a date/time stamp in the format of YYYYMMDD_hhmmss format.  I would recommend that since it will allow easier sorting in chronological order.  Adjust the from and to folder names, and give it a test and let me know how it goes.

@echo off
setlocal EnableDelayedExpansion

REM Get current date and time stamp (YYYYMMDD_hhmmss)
for /f "tokens=1 delims=. skip=1" %%A in ('wmic os get LocalDateTime') do (
  if not defined LocalDateTime (
    set LocalDateTime=%%A
    set CurrentDateTime=!LocalDateTime:~0,8!_!LocalDateTime:~8,6!
  )
)

REM Define source and archive folder locations
set BaseDir=B:\EE\EE28657543\SourceFolder
set DestDir=B:\EE\EE28657543\ArchiveFolder\%CurrentDateTime%

REM define robocopy options
set Options=/MOV /S /R:1 /W:1 /NP /MAXAGE:1

REM Do the backups
robocopy "%BaseDir%" "%DestDir%" %Options%

Open in new window

~bp
0
 
shmzAuthor Commented:
Thanks everyone for the input.
I appreciate it.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.