Complex script. Can't find error.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
assisttool
Posts: 6
Joined: 20 Dec 2015 11:25

Complex script. Can't find error.

#1 Post by assisttool » 20 Dec 2015 11:36

I have this script in a file that is supposed to help a beginner run commands in CMD.

Code: Select all

:Direct
            SET /p direct= "Copy and paste file path here. "
               cd "%direct%"
               Call :Assist

But, when I run the command that I have set to call this it prints "Copy and paste file here. " But, after I enter the path and press enter the script runs this bit of code instead.

Code: Select all

:userInfo
            SET /p userInfo= "What user do you want info on? "
               net user %userInfo%
                  Call :Assist

I don't understand what is going on. I have tried moving the first script and I have tried running the script on another computer. If you want to look at the whole program you can get it here.
https://github.com/AssistTool/The-Assist-Tool.git

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Complex script. Can't find error.

#2 Post by foxidrive » 20 Dec 2015 17:55

This line is unreliable as a user can have their downloads pointing to a different location. It can also be a path that requires double quotes.

Code: Select all

xcopy C:\Users\%user%\Downloads\The-Assist-Tool-master\The-Assist-Tool-master C:\Users\%user%\Desktop\AssistTool 


On merely viewing the code it seems unlikely that what you say above will occur.
You're saying that the lower code will run when "Call :Assist" is executed - right?

Can you try it again in an empty directory with a new copy of the file to see if the results change?

assisttool
Posts: 6
Joined: 20 Dec 2015 11:25

Re: Complex script. Can't find error.

#3 Post by assisttool » 21 Dec 2015 17:48

I turned echo on so I could see what the code was doing and instead of

Code: Select all

Call :Assist


it went to

Code: Select all

:userInfo
            SET /p userInfo= "What user do you want info on? "
               net user %userInfo%
                  Call :Assist


So I changed some things around and I got this result.

Code: Select all

         :BugFix
            Call :Assist
         :userInfo
            SET /p userInfo= "What user do you want info on? "
               net user %userInfo%
                  Call :Assist


For whatever reason the script was calling on line 105 of my code. By putting :BugFix in there it does what I want it to. :?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Complex script. Can't find error.

#4 Post by foxidrive » 22 Dec 2015 00:07

Put a pause command before and after that line. See if it is executed as you expect it to be.
Add one to other places so you can isolate where the issue is.

You may find it's not the place that it is branching from or to.

assisttool
Posts: 6
Joined: 20 Dec 2015 11:25

Re: Complex script. Can't find error.

#5 Post by assisttool » 22 Dec 2015 18:32

So I started by adding pauses in the code like you said foxidrive.

Code: Select all

   :Direct
         Echo on
         pause
            SET /p direct= "Copy and paste file path here. "
            pause
               cd "%direct%"
               pause
               GOTO :Assist
               pause
      


and

Code: Select all

   :BugFix
      pause
      GOTO :Assist
      pause


It worked just as I thought it would so I started to isolate the problem by removing :BugFix and it still worked. Then I started removing pauses from :Direct and I can remove all of them except the pause after GOTO :Assist.

Code: Select all

         :Direct
         Echo on
            SET /p direct= "Copy and paste file path here. "
               cd "%direct%"
               GOTO :Assist
               pause


But, when I take that pause out it stops working right. Should I just leave it in there?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Complex script. Can't find error.

#6 Post by foxidrive » 23 Dec 2015 12:35

I've made a lot of changes below, but haven't tested it.

1) using goto instead of call in many places
2) changed the top section
3) added /i in the if compares, to make the compare case-insensitive.
4) simplified some logic by using NOT

Code: Select all

@echo off
color f
title Assist Tool

rem The xcopy line below has the wrong source file by the look of it

      IF not exist "%userprofile%\Desktop\AssistTool\" (
         xcopy "%userprofile%\Downloads\The-Assist-Tool-master\The-Assist-Tool-master" "%userprofile%\Desktop\AssistTool\" >nul
      )

:Assist
   SET /p task= "Please enter a command... (For Help type HELP) "
      IF /i "%task%"== "HELP" goto :CMDS
      IF /i "%task%"== "ping" goto :Ping
      IF /i "%task%"== "+ user" goto :AddUser
      IF /i "%task%"== "- user" goto :DeleteUser
      IF /i "%task%"== "users" goto :NetUser
      IF /i "%task%"== "password change" goto :password
      IF /i "%task%"== "user active" goto :UserActive
      IF /i "%task%"== "user info" goto :userInfo
      IF /i "%task%"== "localgroups" goto :SetLocalGroup
      IF /i "%task%"== "localgroup info" goto :localgroupInfo
      IF /i "%task%"== "launch program" goto :LaunchProgram
      IF /i "%task%"== "remote" goto :Remote
      IF /i "%task%"== "Directory" goto :Direct
      IF /i "%task%"== "external command" goto :external
      IF /i "%task%"== "End" goto :End
      goto :assist

         :external
            SET /p ext= "Please enter terminal command... "
               echo %ext%
               echo Continue?
               pause
                  %ext%
                  goto :Assist

         :Direct
            SET /p direct= "Copy and paste file path here. "
               cd "%direct%"
               goto :Assist

         :Remote
            shutdown /i
               GOTO :Assist

         :AddUser
            SET /p userName= "What do you want the user name to be? "
               IF /i "%userName%"== "quit" GOTO :End
               net user %userName% /add
                  GOTO :SetLocalGroup

         :password
            SET /p Yass= "What user would you like to change the password of? "
            SET /p password= "Are you sure that you would like to change this password? (yes or no) "
               IF /i not "%password%"== "yes" goto :Assist
                     net user %Yass%
                     goto :Assist

         :UserActive
            Set /p userName= "Which user? "
            Set /p active= "Active (yes or no)? "
               IF /i "%active%"== "yes" GOTO :UserYesActive
               IF /i "%active%"== "no" GOTO :UserInactive
                  :UserYesActive
                     net user %userName% /active:yes
                        goto :Assist
                  :UserInactive
                     net user %userName% /active:no
                        goto :Assist

         :SetLocalGroup
            SET /p localGroup= "Would you like to add/remove to/from a localgroup? "
               IF /i not "%localGroup%"== "yes" goto :Assist
                     SET /p which="Add or remove? "
                        IF /i "%which%"== "add" GOTO :YesYes
                        IF /i "%which%"== "remove" GOTO :NoNo
                           :YesYes
                              SET /p userYes= "Name the user please "
                              SET /p yes= "Which localgroup do you want it in? "
                                 net localgroup %yes% %userYes% /add
                                    goto :Assist
                           :NoNo
                              SET /p userNo= "Name the user please "
                              SET /p no= "Which localgroup do you want it removed from? "
                                 net localgroup %no% %userNo% /del
                                 goto :Assist

         :LaunchProgram
            set /p userFileName= "Enter File Name "
            set /p userSure= "Are You Sure? (yes or no) "
               IF /i not "%userSure%"=="yes" goto :Assist
                  start %userFileName%
                  goto :Assist

         :DeleteUser
            Set /p whichOne= "Which user? "
               net user %whichOne% /del
                  goto :Assist

         :localgroupInfo
            SET /p Localgroup= "Which localgroup do you need information from? "
               net localgroup %Localgroup%
                  goto :Assist

         :userInfo
            SET /p userInfo= "What user do you want info on? "
               net user %userInfo%
                  goto :Assist

         :Ping
            SET /p ping= "What is the URL of the site you would like to ping?(I need the full url) "
               ping %ping%
                  goto :Assist

         :CMDS
             cd "C:\Users\%user%\Desktop\AssistTool\"
            start "" README!.txt
            goto :Assist

         :NetUser
            net users
            SET /p isThat= "Is that all? (yes or no) "
               IF /i not "%isThat%"== "yes" goto :Assist

         :End
            SET /p quitYes= "Are you sure you want to quit? (yes or no) "
               IF /i not "%quitYes%"== "yes" goto :Assist
               EXIT

assisttool
Posts: 6
Joined: 20 Dec 2015 11:25

Re: Complex script. Can't find error.

#7 Post by assisttool » 23 Dec 2015 18:15

Thanks foxidrive it appears to work better now.

For the xcopy at the beginning.

Code: Select all

   echo on
      cd C:\Users\%user%
         DIR AssistTool.bat /s > var1.txt
            find "C:\users" var1.txt> var2.txt
            
      xcopy "::directory variable" "C:\Users\%user%\Desktop\AssistTool"
      goto :Assist


Is there a way to get just the directory from the DIR command? Right now it prints


---------- VAR1.TXT
Directory of C:\Users\USERNAME\Desktop

but, i want it to get the directory and use it as the value of a variable. I can't get it to just isolate the directory itself it always has the "Directory of" in there. Is there a way to do that or not?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Complex script. Can't find error.

#8 Post by foxidrive » 24 Dec 2015 02:41

The brief switch helps you there - help dir will give you good information.

assisttool
Posts: 6
Joined: 20 Dec 2015 11:25

Re: Complex script. Can't find error.

#9 Post by assisttool » 24 Dec 2015 09:55

So, this is what I have come up with so far.

Code: Select all

    IF not exist %userprofile%\Desktop\AssistTool\ (
REM Creates a folder for the files.
      md %userprofile%\Desktop\AssistTool\
         Echo Created Succesfully
REM Finds the downloaded files and copies them to the folder.
   echo on
      cd %userprofile%
         DIR /B/S AssistTool.bat > var1.txt
REM
REM I need a function that calls the first line of var1.txt.
REM "%texte%" is filler          
REM         
      xcopy "%texte%" "%userprofile%\Desktop\AssistTool"
      pause
      )


All I need now is to get a function that calls the first line of var1.txt and uses the variable %texte% to copy the files.

I have tried

Code: Select all

set /p texte=< "var1.txt"  


and

Code: Select all

for /f "delims=" %%a in ('var1.txt') do (
echo %%a
exit /b
)


but, that doesn't work it prints "invalid drive specification" and doesn't copy the files.


Thanks for the help foxidrive.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Complex script. Can't find error.

#10 Post by foxidrive » 24 Dec 2015 10:28

This is untested:

Code: Select all

    IF not exist "%userprofile%\Desktop\AssistTool\" (
REM Creates a folder for the files.
      md "%userprofile%\Desktop\AssistTool\"
         Echo Created Succesfully
REM Finds the downloaded files and copies them to the folder.
     for /f "delims=" %%a in (' dir /s /b /a-d "%userprofile%\AssistTool.bat" ')  do (
        xcopy "%%~dpa\*.*" "%userprofile%\Desktop\AssistTool\" /s/h/e/k/f/c >nul
          )
       )

assisttool
Posts: 6
Joined: 20 Dec 2015 11:25

Re: Complex script. Can't find error.

#11 Post by assisttool » 25 Dec 2015 23:12

The code sort of works. Let me explain. The script creates the folder and, finds and copies the files perfectly. Then it echos "Cannot perform and cyclic copy" and then just sits like it's doing something. I tried just leaving it alone for awhile but, after 3 hours it stays the same.

Could it be that it is freaking out because I have more than one copy of the files? Or is it something else?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Complex script. Can't find error.

#12 Post by foxidrive » 26 Dec 2015 06:55

assisttool wrote:The code sort of works. Let me explain. The script creates the folder and, finds and copies the files perfectly. Then it echos "Cannot perform and cyclic copy"

A cyclic copy is where it is trying to copy a set of files from a folder, to another place within the same folder.

Change this line to the ones below it and run the code.
That's a debugging step that will show what commands would run - and it will allow us to see where the fault is.

Code: Select all

 xcopy "%%~dpa\*.*" "%userprofile%\Desktop\AssistTool\" /s/h/e/k/f/c >nul

Code: Select all

echo  xcopy "%%~dpa\*.*" "%userprofile%\Desktop\AssistTool\" /s/h/e/k/f/c 
pause

There has to be an AssistTool.bat on the desktop to cause this error.

Are there other support files for AssistTool.bat in the same folder, or other subdirectories involved with AssistTool.bat ?
Or is AssistTool.bat the entire program?

Post Reply