Jump to content

batch file help - prompt user to name a dir


graysky

Recommended Posts

Goal of batch file: 1) make a dir the users specifies and 2) copy files to that newly created dir.

Problem is what I came up with doesn't work if the user specifies a name with spaces (non DOS 6.2 rules).

cls
@echo off
echo
set /p target=Please enter the directory name:
mkdir %target%

If the user enters a single word, it works just fine. If the user enters several words separated by a space, it doesn't work.

Can someone help? The final line of the batch file will be something like:

move f:\*.* %target% .

Link to comment
Share on other sites


Sure does, thanks man!

edit - now is there a way I can start the windows xp copy or move command rather than using the DOS command? (I.e. launch whatever exe windows uses when a user does a drag-and-drop that will invoke the standard "Copying ..." dialogbox?

Edited by graysky
Link to comment
Share on other sites

I don't think you can from dos you can from a vbscript though.

Const moveOrRenameTrue = 8
Const yesToAll = 16
Const displayProgress = 256
Const noConfirmDirCreate = 512

Set objShellApp = CreateObject("Shell.Application")
set toFolder=objShellApp.NameSpace(Destination)
toFolder.CopyHere Source, moveOrRenameTrue + yesToAll + displayProgress + noConfirmDirCreate

Link to comment
Share on other sites

As a side note (complaint) to your original 'batch code', you really need to use some sort of error trapping.

For example

  1. your user uses a blank name
  2. your user makes a mistake
  3. your user inputs illegal characters
  4. your user types a name of a folder which already exists

The 'batch language' is not the best choice to cover these sorts of things, however an attempt should be made to do so.

<Edit>

Here is an easy read example which covers the above scenarios reasonably well.

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS

SET BN="%TEMP%\_BADNAME.DAT"
SET MN="%TEMP%\_MYNAME.DAT"
>%BN% (
ECHO.\
ECHO./
ECHO.*
ECHO.^|
ECHO.^<
ECHO.^>
ECHO.:
ECHO.?
ECHO.,
ECHO.^")

:FOLDNAME
FOR %%? IN (NAME Z ANSR) DO (SET %%?=)
CLS
(SET /P NAME= PLEASE ENTER YOUR CHOSEN DIRECTORY NAME: )
<NUL (SET /P Z=%NAME%)>%MN%
IF DEFINED NAME (GOTO CHKIT)
CLS
ECHO/ BLANK NAMES ARE NOT ACCEPTED!
ECHO/
SET /P "ANSR= DO YOU WISH TO CHOOSE A DIRECTORY NAME (Y/N)? "
IF /I '%ANSR:~0,1% NEQ 'Y (GOTO EOF)
GOTO FOLDNAME

:CHKIT
ECHO/%NAME%|FINDSTR/G:"%BN%" %MN%||GOTO RSURE
CLS
ECHO/ YOUR NAME CONTAINS ILLEGAL CHARACTERS!
ECHO/
ECHO/ PLEASE CHOOSE ANOTHER DIRECTORY NAME.
PING -n 4 LOCALHOST>NUL
GOTO FOLDNAME

:RSURE
CLS
IF DEFINED ANSR (SET ANSR=)
ECHO/
ECHO/ THE DIRECTORY NAME YOU HAVE CHOSEN IS
ECHO/
ECHO/ %NAME%
ECHO/
SET /P "ANSR= WOULD YOU LIKE TO CHANGE IT (Y/N)? "
IF /I '%ANSR:~0,1% EQU 'Y (GOTO FOLDNAME)

:UNUSED
IF NOT EXIST %NAME% GOTO DONE
CLS
ECHO/ THE NAME YOU HAVE CHOSEN ALREADY EXISTS!
ECHO/ PLEASE CHOOSE ANOTHER DIRECTORY NAME.
PING -n 4 LOCALHOST>NUL
GOTO FOLDNAME
CLS

:DONE
ECHO/
ECHO/ YOU'VE CHOSEN A NEW VALID DIRECTORY NAMED
ECHO/ %NAME%
PAUSE

:EOF
DEL %BN% %MN%

You would replace or add to the data beneath the DONE label

</Edit>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...