Solved

Script to copy files with variables

Posted on 2013-06-01
1
423 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
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 Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Numerous files can not be opened anymore 17 68
Windows Batch File - Count Down 4 88
how to export this list 4 61
WMIC Command LIne 5 45
We were having a lot of "Heartbeat Alerts" in our SCOM environment, now "Heartbeat" in a SCOM environment for those of you who might not be familiar with SCOM is a packet of data sent from the agent to the management server on a regular basis, basic…
Article by: Leon
Software Metering within our group of companies has always been an afterthought until auditing of software and licensing became a pain point. Orchestrator and SCCM metering gave us the answer and it was an exciting process.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
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…

830 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