Batch file to move and reorganize files and folders.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Kona
Posts: 5
Joined: 16 Jun 2014 22:18

Batch file to move and reorganize files and folders.

#1 Post by Kona » 16 Jun 2014 22:38

Hello,

I have a program at work that exports reports (PDF) every night for archival purposes, but the file and folder names are not user friendly.
Every night it creates a folder that has yesterday's date as name in this format ddmmyy (yesterday's folder is 160614).
What I would like is to have a batch file that would run with a scheduled task every morning and find yesterday's folder, copy its content, paste them to a network share and rename the files.
I would like the destination to have this kind of structure: Containing_folder/2014/June/16/"files"

The files will be accessible on \\servername\d$\audit and should be sent to \\servername\d$\Public
One of the file names is GLTB.PDF

Your help would be greatly appreciated, and please let me know if i can give you any extra info.

Thanks,
Kona

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch file to move and reorganize files and folders.

#2 Post by foxidrive » 16 Jun 2014 23:02

Kona wrote:I would like the destination to have this kind of structure: Containing_folder/2014/June/16/"files"


Using month names will stop the folders sorting correctly.

How do you want the files renamed? You didn't say.

Kona
Posts: 5
Joined: 16 Jun 2014 22:18

Re: Batch file to move and reorganize files and folders.

#3 Post by Kona » 17 Jun 2014 02:23

Ok, didn't think about that.
Then "01 January" would be fine.
For the example I gave you the filename should be "Guest Ledger Trial Balance"

Thanks

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch file to move and reorganize files and folders.

#4 Post by foxidrive » 17 Jun 2014 02:45

This will sort properly - containing folder\2014\06\16\files

Do you only have one file in your folders?

Kona
Posts: 5
Joined: 16 Jun 2014 22:18

Re: Batch file to move and reorganize files and folders.

#5 Post by Kona » 17 Jun 2014 03:12

Ok that's fine.
No I have 16, but I'm in the process of changing them so I'll change them myself in the file.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch file to move and reorganize files and folders.

#6 Post by foxidrive » 17 Jun 2014 04:11

Test this to see if it copies the folder.

Code: Select all

@echo off
set day=-1
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%day%,now) : d=weekday(s)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^& right(100+month(s),2)^& right(100+day(s),2)
for /f %%a in ('cscript /nologo "%temp%\%~n0.vbs"') do set "result=%%a"
del "%temp%\%~n0.vbs"
set "YY=%result:~2,2%"
set "YYYY=%result:~0,4%"
set "MM=%result:~4,2%"
set "DD=%result:~6,2%"
set "data=%yyyy%\%mm%\%dd%"
set "folder=%dd%%mm%%yy%

for /d /r "\\servername\d$\audit" %%a in (%folder%*) do robocopy "%%a" "\\servername\d$\Public\%data%" /mir
pause

Kona
Posts: 5
Joined: 16 Jun 2014 22:18

Re: Batch file to move and reorganize files and folders.

#7 Post by Kona » 17 Jun 2014 04:42

Works great, makes the directories exactly as expected.
If it could also only copy and rename the files we specify (there is one file in there i don't need) it would be awesome.

Thanks!

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Batch file to move and reorganize files and folders.

#8 Post by Squashman » 17 Jun 2014 06:22

Kona wrote:If it could also only copy and rename the files we specify (there is one file in there i don't need) it would be awesome.

Thanks!

The batch file is using Robocopy to copy the files. I bet if you read the help file for that command you would see this.

Code: Select all

/XF file [file]... :: eXclude Files matching given names/paths/wildcards.

Kona
Posts: 5
Joined: 16 Jun 2014 22:18

Re: Batch file to move and reorganize files and folders.

#9 Post by Kona » 18 Jun 2014 04:14

ok, i will take it from here then.
Thanks a lot !

Post Reply