0

I try to access some Excel data stored on a server and copy these files to another server ; the destination path should be different with the name of the file. Here is my code :

setlocal enabledelayedexpansion
net use Z: \\10.0.0.1\Statistiques
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"
    set folder=Empty
    if "!name!"=="!name:Client1=!" (
        set folder=Client1
    )
    if "!name!"=="!name:Client2=!" (
        set folder=Client2
    )
    copy "%path%%%a" "\\10.0.0.2\Documents\Statistiques\!folder!\%year%%month%%day%_!name!%%~xa"
)
net use Z: /delete

My issue is that the last 'net' is not recognized as an internal or external command, operable program or batch file.

On the first net use, the drive is correctly mount ; the files are correctly copied, but when I want remove the drive I have this error.

May be an error in my if () statement ?

J. Grunder
  • 143
  • 2
  • 13

2 Answers2

2

PATH is an environment variable defined by Windows containing the directories in which command processor should search for executables or scripts with a file extension listed in environment variable PATHEXT. Run in a new command prompt window set path or just set to get the predefined environment variables displayed with their current values.

Your batch code contains:

set path=Z:\

This overrides the value of PATH defined by Windows and therefore command processor can't find net.exe anymore as not being in current directory or in root directory of drive Z:.

Use a different name for the environment variable which gets Z:\ assigned as value.

Best would be additionally for both occurrences of net to use

%SystemRoot%\System32\net.exe

This would avoid the need to search for net.* by command processor in current directory and in all directories of PATH until a file is found with a file extension listed in PATHEXT.

Mofi
  • 46,139
  • 17
  • 80
  • 143
  • Yes I've made confusing with system var path ; I used the last proposition : `%SystemRoot%\System32\net.exe` instead of renaming vars. Thanks for your help. – J. Grunder Dec 29 '15 at 12:45
  • I suggest that you rename your var to something other than `path` rather than explicitly giving the full path to net.exe. That will make your code more readable and, more important, you won't get surprised again in the future, months after you've forgotten this problem, when you add another line to your script which depends on seeing a valid path. – David Goldfarb Dec 29 '15 at 13:12
1

The problem is your path statement (the third line of your script).

It looks like you misunderstand what that command does. "path" does not set your current directory. It sets the list of directories that will be searched for programs.

If you want to set your current directory to be z:, just say z:

Or, if I misunderstood, and you want to search for programs on Z: too, then say set path=z:;%path% which adds Z: to your path.

You told the computer to look only in Z: for programs. Therefore, it could not find net (or many other programs).

David Goldfarb
  • 1,796
  • 3
  • 18
  • 32
  • Please look on all occurrences of `%path%` in batch code provided in question and you will see that your answer is not right as J. Grunder just used `path` as simple variable to hold a single directory path. – Mofi Dec 29 '15 at 12:41
  • Thank you for your help David, I understand your solution but later I'm using this var to make a listing of files, so I don't want this check is done on my %System32%, it could take long time. I've made a confusing with environment var path. – J. Grunder Dec 29 '15 at 12:43
  • Right you are; I missed that J Grunder was using `path` as a simple variable. I recommend leaving path with its usual meaning, and using a different variable name for the local variable. This is cleaner and easier to maintain, rather than smashing path, and then having to explicitly list the directory for `net` (and any other programs this script calls when you update it in the future). – David Goldfarb Dec 29 '15 at 12:50