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" 
    ) 
)
Community
  • 1
  • 1
J. Grunder
  • 143
  • 2
  • 13

1 Answers1

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.

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • 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. – J. Grunder May 03 '16 at 12:27
  • If there are `.xlsx` files that don't belong to a project, you have an organizational problem, I think. Is that really an issue? – Stephan May 03 '16 at 12:39
  • 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. – J. Grunder May 03 '16 at 14:00
  • yes, that's what I understood. I used `%%~na` (which is the name of the file without the extension) instead of an additional variable `!folder!` – Stephan May 03 '16 at 14:31
  • Hmm ok, but I cant create a new folder for each day I have each report. That why in my code I test all file name to find the appropriate project to move the file into it's project folder. – J. Grunder May 03 '16 at 14:51
  • `mkdir` does not create a new folder with the same name, if it exists. Instead it gives an error message, which is redirected to nul (`2>nul`). So no need to check, if it exists - just try to create it and ignore the errormessage, if it did already exist. – Stephan May 03 '16 at 15:01
  • I'm sorry, I'm not sure I explain you correctly what I need. I've edited my post with more information, I hope it could help. – J. Grunder May 03 '16 at 15:41
  • 1
    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? – Stephan May 03 '16 at 15:46
  • I have control of naming strategy for each file, so yes I can put projectName between anything, underscore, minus, etc (no special char). But it could be abc_def_ghi_jkl_PROJECT1_mno_pqr.xls :) Yes we have a very complicated naming strategy – J. Grunder May 04 '16 at 08:11
  • for automatic processing it's essential, that there are clear rules (without exceptions) how to find the project name. For example always between the fourth and fifth underscore or always between another unique delimiter that surely doesn't occure elsewhere in the filenames (like - or #). – Stephan May 05 '16 at 16:20
  • Hi, I can put project name between "-" sign – J. Grunder May 10 '16 at 10:00
  • I choose # separator. I've updated my initial post with EDIT2, I have a question about variable I used to determine final file name. Can you help me on this topic ? :) – J. Grunder May 10 '16 at 12:31
  • 1
    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 `||`. – Stephan May 10 '16 at 15:43