Solved

Script to copy files with variables

Posted on 2013-06-01
1
426 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

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…
This collection of functions covers all the normal rounding methods of just about any numeric value.
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
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…

751 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