emp1953 Posted September 19, 2008 Share Posted September 19, 2008 (edited) I have a batch file that creates directories, changes permissions and copies files onto a usb device.It works fine but as we all know, as soon as something like this is created, someone wants an enhancement.Several problems,I needed to pass in the thumb drive letter since each PC could assign a different drive letter to the device.I wanted to pass in a parameter but couldnt achieve that so I added a line in the batch script requesting the user to enter it from the keyboard.Then I needed to put the output showing what the script was doing into a log file so the user could view the successful completion of the script.I was able to do this too. but.....When I redirected the output to the log file, the script no longer prompted for the drive letter. Also the script progress no longer displays in the command window. I need it to display in the command window as well as redirect to a log file, and I need the prompt for the user to enter a drive letter to show up as before the redirect was implemented.Heres the scriptecho "automated method to move new files from view area to thumbdrive"set scriptpath=U:\NewApps\ConfigurationManagement\set viewpath=U:\NewApps\Quiz36\TCC\Code\set /p thumb_dr= What is the thumb drive letter ?echo "creating directory tree on thumbdrive"%thumb_dr%cd nearfieldmkdir srcmkdir fpgaIncludemkdir includemkdir mfaIncludemkdir msgIncludemkdir nsiIncludemkdir control_filesmkdir c_codeecho "copying cm scripts"cd ..\nearfieldcopy %scriptpath%Scripts\* .echo "copying src directory files"cd srccopy %viewpath%src\* .echo "copying fpgaInclude directory files"cd ../fpgaIncludecopy %viewpath%fpgaInclude\* .echo "copying include directory files"cd ../includecopy %viewpath%include\* .echo "copying mfaInclude directory files"cd ../mfaIncludecopy %viewpath%mfaInclude\* .echo "copying msgInclude directory files"cd ../msgIncludecopy %viewpath%msgInclude\* .echo "copying nsiInclude directory files"cd ../nsiIncludecopy %viewpath%nsiInclude\* .echo "copying control_files directory files"cd ../control_filescopy %viewpath%control_files\* .echo "copying c_code directory files"cd ../c_codecopy %viewpath%c_code\* .cd ../echo "DONE"Thanksemp1953 Edited September 22, 2008 by Yzöwl code tags added Link to comment Share on other sites More sharing options...
Yzöwl Posted September 20, 2008 Share Posted September 20, 2008 What you're looking for is to 'tee' the output. There are several methods and incarnations available for doing so, I'd suggest you start with tee taken from this package.You may also find some other interesting utilities there too, especially mkdir! Link to comment Share on other sites More sharing options...
strel Posted September 20, 2008 Share Posted September 20, 2008 (edited) Get drive with:set thumb_dr=%~d0And split output with:http://www.commandline.co.uk/mtee/ Edited September 21, 2008 by strel Link to comment Share on other sites More sharing options...
strel Posted September 20, 2008 Share Posted September 20, 2008 Here you can find Yzölw and others spreading wisdom:http://www.msfn.org/board/Help-me-to-redir...ors-t69151.html Link to comment Share on other sites More sharing options...
jaclaz Posted September 21, 2008 Share Posted September 21, 2008 Since there are a number of repetitive tasks, all of them "name" oriented, wouldn't something like this (just an example, NOT tested, DO NOT take it as "final" code ) be simpler/handier? set scriptpath=U:\NewApps\ConfigurationManagement\set viewpath=U:\NewApps\Quiz36\TCC\Code\set /p thumb_dr= What is the thumb drive letter ?if not exist %thumb_dr%:\nearfield\nul md %thumb_dr%:\nearfieldcd /d %thumb_dr%:\nearfieldset logfile=%thumb_dr%:\nearfield\log.txtecho .>%logfile%copy %scriptpath%Scripts\*& CALL :mytee "copying cm scripts"FOR %%A in (src fpgaInclude include mfaInclude msgInclude nsiInclude control_files c_code) DO (mkdir %%A& CALL :mytee "making %%A directory"copy %viewpath%%%A\* %thumb_dr%:\nearfield\%%A& CALL :mytee "copying %%A directory files")CALL :mytee "End of program"GOTO :EOF:myteeecho %~1echo .echo %~1>>%logfile%echo .>>%logfile%GOTO :EOFjaclaz Link to comment Share on other sites More sharing options...
Yzöwl Posted September 22, 2008 Share Posted September 22, 2008 The \nul is not required, nor recommended in an OS using cmd.exe.If Exist XXX is True as long as XXX exists (file or a directory); When the path is quoted, appending \nul always returns false.Question:Is there a reason why you're asking for user input for the thumbdrive drive letter? This is something which can be programmed into the routine. You may think it's easier to leave it as it is, but that's because you've got no error checking etc. built into the routine. By the time asked for the input, read it, checked it is a single character in the range D-Z then searched, if successful, it seems like you've made things more long winded.Additionally:You're doing far too much to-ing and fro-ing in that script. There's no real benefit in changing to a directory in order to copy to it, then changing to another in order to copy to it etc.Another thing is, you could use Xcopy, using that you could use the /I switch an thus negate creating a host of directories all with identical names to those your copying anyway. Link to comment Share on other sites More sharing options...
jaclaz Posted September 22, 2008 Share Posted September 22, 2008 The \nul is not required, nor recommended in an OS using cmd.exe.If Exist XXX is True as long as XXX exists (file or a directory); When the path is quoted, appending \nul always returns false.Yep, it was a DOS reminiscence when jolting down it. Though in this case the "spaces in the name" appear to be not a problem.OT, but not much, have you had any experience with the "nul.ext" ? http://www.msfn.org/board/Batch-Scripts-fo...ure-t98853.htmlhttp://www.msfn.org/board/batch-programming-t105969.htmlI mean, it seems like your "direct" approach is allright if you are sure that there is NO file with the same name as the searched folder, but would probably fail (later) if this rare case applies.The \nul approach does not work with spaces in names (read quoted path).What about the \nul.ext ?jaclaz Link to comment Share on other sites More sharing options...
Yzöwl Posted September 22, 2008 Share Posted September 22, 2008 Well as a rule, in the context your using it I'd simply use:If Not Exist %thumb_dr%:\nearfield\The trailing backslash hopefully being sufficient to differentiate! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now