Solved

Script to copy files with variables

Posted on 2013-06-01
1
419 Views
Last Modified: 2013-06-01
I need a script I can schedule that will move files out of folder A and move them to sub-folders of folder B.  If there is not yet a folder in folder B named with the MAC address the script would need to create one.

The first 12 characters in the file name are the individual MAC address of the hardware device uploading the file, following the underscore is the date the file was uploaded.



Folder A = C:\FTP SERVER
Folder B = C:\Machine DATA
Subfolders of Folder B = C:\Machine DATA\0027AF110121   One folder for each mac address


0027AF110121_20130501.zip
0027AF110121_20130501.zip
0027AF110124_20130501.zip
0027AF110124_20130501.zip
0027AF110124_20130501.zip
0027AF110121_20130501.zip
0027AF110118_20130501.zip
0027AF110116_20130501.zip
0027AF110118_20130501.zip
0027AF110118_20130501.zip
0
Comment
Question by:NEXTEKHD
1 Comment
 
LVL 31

Accepted Solution

by:
Frosty555 earned 250 total points
Comment Utility
In batch, this is pretty easy because the beginning of each filename is the same length. You can get the substring of the first 12 characters of the file, and use that to create the directory and move the file into it.

@echo off

setlocal enableDelayedExpansion

set foldera=C:\temp\mac
set folderb=C:\temp\mac\folders

pushd %foldera%
for %%F in (*.zip) do (
   set file=%%F
   set dir=%folderb%\!file:~0,12!   

   echo Moving !file!  into  !dir!

   if not exist !dir! mkdir !dir!
   move !file! !dir!
)
popd

Open in new window


Some notes:

1) Delayed expansion is very important, the environment variables that are references with "!" instead of "%" characters (e.g. !file!) are environment variables that are using delayed expansion.

2) The expression "!file:~0,12!"  means "substring of the first 12 characters of !file!"
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
The canonical version of this article is on my web site here: http://iconoun.com/articles/collisions/ A companion presentation is available here: http://iconoun.com/articles/collisions/Unicode_Presentation.pdf
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now