问题
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 ?
回答1:
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.
回答2:
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).
来源:https://stackoverflow.com/questions/34510520/if-statement-in-batch-and-app-not-recognized-anymore