I wrote a batch with many help of this community (with a first question here If statement in batch and app not recognized anymore) but I have more and more project to add to this batch and it becomes unreadable.
What I have : a statistic tool that send me statistics Excel files into \10.0.0.1\MyFolder I need to copy these files into another network and put these files into subfolders regarding current date.
What I wrote (that is currently working) :
@echo off
setlocal enabledelayedexpansion
net use Z: \\10.0.0.1\MyFolder
set path=Z:\
set year=%date:~10,4%
set month=%date:~4,2%
set day=%date:~7,2%
echo %date%
for /f "delims=" %%a in ('dir *.xlsx /b /a-d "%path%" ') do (
set "name=%%~na"
if "!name!"=="!name:PROJECT1=!" (
if "!name!"=="!name:PROJECT2=!" (
if "!name!"=="!name:PROJECT3=!" (
if "!name!"=="!name:PROJECT4=!" (
if "!name!"=="!name:PROJECT5=!" (
set folder=Empty
) else (
set folder=Project5
datename=%year%%month%%day%
)
) else (
set folder=Project4
if "!name!"=="!name:jour=!" (
set datename=%year%%month%%day%
) else (
set datename=%year%%month%
)
)
) else (
set folder=Project3
if "!name!"=="!name:jour=!" (
set datename=%year%%month%%day%
) else (
set datename=%year%%month%
)
)
) else (
set folder=Project2
if "!name!"=="!name:jour=!" (
set datename=%year%%month%%day%
) else (
set datename=%year%%month%
)
)
) else (
set folder=Project1
set datename=%year%%month%%day%
)
echo !datename!
IF not exist "\\10.0.0.2\Stats\!folder!\%year%" ( mkdir "\\10.0.0.2\Stats\!folder!\%year%" )
IF not exist "\\10.0.0.2\Stats\!folder!\%year%\%month%" ( mkdir "\\10.0.0.2\Stats\!folder!\%year%\%month%" )
copy "%path%%%a" "\\10.0.0.2\Stats\!folder!\%year%\%month%\!datename!_!name!%%~xa"
)
%SystemRoot%\System32\net.exe use Z: /delete
First, I'm mapping a network drive. Then, into this folder I check each file that are named with PROJECT X and other data. If I found PROJECT X into name of the file, I create a var folder and datename to put files into correct folder. If statistics subfolder with Year or Month are not exist batch will create them. Finally I'm copying file into statistics' subfolders with overwrite if already exists. This is exactly what I need.
My question is : actually I have 5 projects, and in a few days I'll have about 10 or 15, so with this structure this program is not optimized. What can I modify to make it more readable and easier to execute for the server ?
EDIT:
I have the folder C:\Files\ that contains Excel files (one file for one statistic) and the name of each project is include into the name file (like abc_PROJECT1_def.xls
, abc_PROJECT2_def.xls
, etc.
I would like with my batch copy these files regarding its name and date. Example : abc_PROJECT1_def.xls
will be copied into C:\Statistics\PROJECT1\2016\05\
, abc_PROJECT2_def.xls
will be copied into C:\Statistics\PROJECT2\2016\05
, etc.
EDIT 2:
Here is my new code :
for %%a in (%mypath%*.xlsx) do (
for /f "tokens=2 delims=#" %%b in ("%%a") do (
echo ----%%b-----
ECHO mkdir "\10.0.0.2\Stats\%%b\%year%\%month%"
ECHO copy "%%a" "\10.0.0.2\Stats\%%b\%year%\%month%\%year%%month%%day%_%%~nxa"
)
)
It's seems working fine (thank you so much for your help), but I only have a last question : the final name of copied file depends of content of original file name. For example, if original file name contains word jour
, I need to rename it starts with %year%%month%
, but if not new file name should starts with %year%%month%%day%
, that's why I used datename
variable.
Is it correct to do something like :
for %%a in (%mypath%*.xlsx) do (
for /f "tokens=2 delims=#" %%b in ("%%a") do (
if "%%~nxa"=="%%~nxa:jour" (
set datename=%year%%month%%day%
) else (
set datename=%year%%month%
)
echo ----%%b-----
ECHO mkdir "\10.0.0.2\Stats\%%b\%year%\%month%"
ECHO copy "%%a" "\10.0.0.2\Stats\%%b\%year%\%month%\!datename!_%%~nxa"
)
)