Jump to content

Batch file functionality


Recommended Posts

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 script

echo "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 nearfield

mkdir src
mkdir fpgaInclude
mkdir include
mkdir mfaInclude
mkdir msgInclude
mkdir nsiInclude
mkdir control_files
mkdir c_code

echo "copying cm scripts"
cd ..\nearfield
copy %scriptpath%Scripts\* .

echo "copying src directory files"
cd src
copy %viewpath%src\* .

echo "copying fpgaInclude directory files"
cd ../fpgaInclude
copy %viewpath%fpgaInclude\* .

echo "copying include directory files"
cd ../include
copy %viewpath%include\* .

echo "copying mfaInclude directory files"
cd ../mfaInclude
copy %viewpath%mfaInclude\* .

echo "copying msgInclude directory files"
cd ../msgInclude
copy %viewpath%msgInclude\* .

echo "copying nsiInclude directory files"
cd ../nsiInclude
copy %viewpath%nsiInclude\* .

echo "copying control_files directory files"
cd ../control_files
copy %viewpath%control_files\* .

echo "copying c_code directory files"
cd ../c_code
copy %viewpath%c_code\* .

cd ../

echo "DONE"

Thanks

emp1953

Edited by Yzöwl
code tags added
Link to comment
Share on other sites


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? :unsure:

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%:\nearfield
cd /d %thumb_dr%:\nearfield

set logfile=%thumb_dr%:\nearfield\log.txt

echo .>%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

:mytee
echo %~1
echo .
echo %~1>>%logfile%
echo .>>%logfile%
GOTO :EOF

jaclaz

Link to comment
Share on other sites

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

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. :blushing:

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.html

http://www.msfn.org/board/batch-programming-t105969.html

I mean, it seems like your "direct" approach is allright :thumbup 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

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...