Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Script to copy files with variables

Posted on 2013-06-01
1
Medium Priority
?
435 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 1000 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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

User Beware!  This is a rather permanent solution to removing your email from an exchange server.  The only way to truly go back is to have your exchange administrator restore your mailbox from backups.  This is usually the option of last resort.  A…
Ever notice how you can't use a new drive in Windows without having Windows assigning a Disk Signature?  Ever have a signature collision problem (especially with Virtual Machines?)  This article is intended to help you understand what's going on and…
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
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…

661 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