Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
Dabless
- Posts: 6
- Joined: 21 Jan 2013 13:17
#1
Post
by Dabless » 21 Jan 2013 13:23
Hi ! Sorry for my bad english, but I am a french canadian, This is why there is some part of my code in french!
I am trying to make a script for the school, Its in windows server 2008. Everything works fine alone. I mean that my command choice works, if I don't put it in the if, and my if work, if I try with an "echo Enter choice command here"
Here the code :
Code: Select all
@echo off
set corbeille=c:\corbeille
if "%1"=="" ( goto:eof )
cd %corbeille% || del %corbeille% >nul REM YOU CANNOT TEST A FOLDER WITH \nul IN WIN SERVER 2008 THIS COMMAND IS UGLY BUT THIS IS WHAT I FOUND
if NOT EXIST %corbeille% ( mkdir %corbeille% )
for %%A in (%*) do if EXIST %%A (
if EXIST %corbeille%\%%~nxA (
echo choice command here
) ELSE (
move %%A %corbeille% > nul
Shift )
) ELSE (
ECHO le fichier %%A n'existe pas, donc il n'a pas ete deplace
)
Here the choice !
Code: Select all
@echo off
choice /c ON /t 10 /d N /m "oui ou non"
IF ERRORLEVEL 2 GOTO non
IF ERRORLEVEL 1 GOTO oui
:non
echo il a dit non
GOTO end
:oui
echo IL A DIT OUI
:end
-
abc0502
- Posts: 1007
- Joined: 26 Oct 2011 22:38
- Location: Egypt
#2
Post
by abc0502 » 21 Jan 2013 14:27
can you explain more, i don't really get what you need. try to explain what your code is supposed to do and what errors do you get.
-
Dabless
- Posts: 6
- Joined: 21 Jan 2013 13:17
#3
Post
by Dabless » 21 Jan 2013 15:11
The script is supose to be a "Secure delete" "Corbeille" is the delete dir. I got the message "La syntaxe de la commande est incorect" if i translate it my self its some thing like "the syntax command is incorect". The choice here is to ask if he want to "over write" if the same file allready exist in the "corbeille". After 10 sec, the default aweser is no
-
DigitalSnow
- Posts: 20
- Joined: 21 Dec 2012 13:36
- Location: United States
#4
Post
by DigitalSnow » 21 Jan 2013 20:47
If I understand what you are wanting to do correctly. This should work.
1. Loop through each command line parameter.
2. If it exists in the folder, prompt the user for a choice
3. Else move the file to the folder.
Code: Select all
@echo off
set corbeille=c:\corbeille
cd %corbeille% || del %corbeille% >nul REM YOU CANNOT TEST A FOLDER WITH \nul IN WIN SERVER 2008 THIS COMMAND IS UGLY BUT THIS IS WHAT I FOUND
if NOT EXIST %corbeille% ( mkdir %corbeille% )
:Next
if "%~1"=="" ( goto :eof )
if EXIST "%~1" (
if EXIST "%corbeille%\%~nx1" (
choice /c ON /t 10 /d N /m "oui ou non"
IF ERRORLEVEL 2 GOTO non
IF ERRORLEVEL 1 GOTO oui
:non
echo il a dit non
GOTO end
:oui
echo IL A DIT OUI
:end
) ELSE (
move "%~f1" "%corbeille%" > nul
)
) ELSE (
ECHO le fichier %~1 n'existe pas, donc il n'a pas ete deplace
)
Shift
goto Next
-
Dabless
- Posts: 6
- Joined: 21 Jan 2013 13:17
#5
Post
by Dabless » 23 Jan 2013 13:38
The problem is still there. I don't know what to do.
If I put echo on, the error comes at the loop for and crash there.
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#6
Post
by foxidrive » 23 Jan 2013 21:49
change this
echo IL A DIT OUI
:end
) ELSE (
to this, and try it.
echo IL A DIT OUI
:end
REM
) ELSE (
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#7
Post
by foxidrive » 23 Jan 2013 21:53
Dabless wrote:
cd %corbeille% || del %corbeille% >nul REM YOU CANNOT TEST A FOLDER WITH \nul IN WIN SERVER 2008 THIS COMMAND IS UGLY BUT THIS IS WHAT I FOUND
The way to test for the existence of a folder in the NT series is this:
if exist "c:\folder\" (echo folder exists) else (echo folder doesn't exist)
The trailing backslash is the key to testing a folder.
-
Dabless
- Posts: 6
- Joined: 21 Jan 2013 13:17
#8
Post
by Dabless » 24 Jan 2013 10:28
foxidrive wrote:change this
echo IL A DIT OUI
:end
) ELSE (
to this, and try it.
echo IL A DIT OUI
:end
REM
) ELSE (
This works. But it does not pass every command, I mean the choice command will only show once, even if it needed twice
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#9
Post
by foxidrive » 24 Jan 2013 11:21
The choice routine only runs when these two things are true:
if EXIST "%~1" (
if EXIST "%corbeille%\%~nx1" (
-
Dabless
- Posts: 6
- Joined: 21 Jan 2013 13:17
#10
Post
by Dabless » 24 Jan 2013 13:21
foxidrive wrote:The choice routine only runs when these two things are true:
if EXIST "%~1" (
if EXIST "%corbeille%\%~nx1" (
Yes thats the point, but when I lauch the script, at the "For" line there is a message error "Syntax command is incorect (My traduction is probablly not exact)"
-
Dabless
- Posts: 6
- Joined: 21 Jan 2013 13:17
#11
Post
by Dabless » 24 Jan 2013 14:29
GOOD NEWS ! IT WORKED !:D
I mixed all your tips together and the result is HORRIBLE, but it works :
Code: Select all
@echo off
rem Script nommer ''Spagatti'' fais par charles goudrea
rem but : supression sécuritaire
if exist c:\corbeille.dir del c:\corbeille.dir
if NOT EXIST c:\corbeille\ mkdir c:\corbeille REM Ceci est la vrais facon de tester si un dossier exist en windows NT
:next
rem La boucle for ne marchais pas, j'ai utiliser un goto
if "%1"=="" goto:eof rem j'ai été obliger de mettre le vérification là D:
if EXIST %1 (
if EXIST c:\corbeille\%~nx1 (
choice /c ON /t 10 /d N /m "voulez vous ecraser le fichier %1"
IF ERRORLEVEL 2 GOTO non
IF ERRORLEVEL 1 GOTO oui
:non
echo Le fichier n'as pas ete ecraser 2> nul
GOTO end
:oui
echo > nul rem sans ces echo, sa ne marche pas, j'ai découvert sa en essie erreur
move /Y %1 c:\corbeille > nul
:end
REM
rem Sans ce REM cela ne marche pas, je ne sais pas pour quoi j'ai eu ce conseille sur un forum
) else ( move %1 c:\corbeille>nul )
) else ( echo le fichier %1 n esxite pas)
echo > nul rem sans ces echo, sa ne marche pas, j'ai découvert sa en essie erreur
shift rem utile sans boucle for
goto next
THANKS GUYS !
And YES the REM after the :end need to be there
And YES the 2 echo>nul NEED to be there
I don't know why, but it works.
hahaha
Thanks