Solved

Script to copy files with variables

Posted on 2013-06-01
1
433 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 31

Accepted Solution

by:
Frosty555 earned 250 total points
ID: 39213498
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This collection of functions covers all the normal rounding methods of just about any numeric value.
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Viewers will learn how to maximize accessibility options in an Excel workbook for users with accessibility issues.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Suggested Courses

622 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