0

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" ) ) 
1
  • 2
    This question suits better on CodeReview. Commented May 3, 2016 at 11:42

1 Answer 1

2

Why writing the same code for every customer again and again? Make it generic:

@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 %%a in (%mypath%*.xls) do ( for /f "tokens=2 delims=-" %%b in ("%%a") do ( echo ----%%b----- set datename=%year%%month% echo "%%~nxa"|find /i "jour" >nul && set datename=!datename!%day% ECHO mkdir "\\10.0.0.2\Stats\%%b%\year%\%month%" ECHO copy "%%a" "\\10.0.0.2\Stats\%%b\%year%\%month%\!datename!_%%~nxa" ) ) 

should do the same.

It shows you the mkdir and copy commands on the screen instead of executing them. It the output satisfies you, remove the two ECHO's.

Note: %PATH% is a systemvariable. Windows uses it to find it's own programs. Don't change it, if you don't know exactly, what you are doing.

Sign up to request clarification or add additional context in comments.

13 Comments

Thank you for your response. I've changed set path with mypath like you. But have a look on variable folder that is used to create subfolder into Stats folder. That's why I test for each project if file name contains project name.
If there are .xlsx files that don't belong to a project, you have an organizational problem, I think. Is that really an issue?
I send all statistics files (for all project) from the statistics server to 10.0.0.1 and then use the batch to dispatch each file into each subfolder of each project into 10.0.0.2 depending of the name of the Excel file.
ah - the important part is, that the projectname is just a part of the filename surrounded by other text. In this case, my approach doesn't work. Is it always preceeded with a text and an underscore and always followed by an underscore and some text?
substring substitution doesn't work with for variables. I hope I understand you correct: "if there is "jour" in the filename, then add the day". See my edit. If it's just the other way round, replace && with ||.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.