How to delete a directory using cmd - authentication

i'm new at cmd and this is the first code i've ever written using the program. There are parts of this code i haven't written myself.
My problem is that i'm trying to delete a folder using CMD but i can't get it to work.
I'm using this command:
RM /S /Q "directory"
In this code
cls
#ECHO OFF
color 02
title ija4779fc
if EXIST "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}" goto UNLOCK
if NOT EXIST MyFolder(2) goto MDMyFolder(2)
:CONFIRM
echo Are you sure to lock this folder? (Y/N)
set/p "cho=>"
if %cho%==Y goto LOCK
if %cho%==y goto LOCK
if %cho%==n goto END
if %cho%==N goto END
echo THIS IS NOT AN OPTION.
goto CONFIRM
:LOCK
ren MyFolder(2) "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
attrib +h +s "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
echo Folder locked
goto UNLOCK
:UNLOCK
echo ENTER PASSWORD:
set/p "pass=>"
if NOT %pass%== ija4779fc goto FAIL
attrib -h -s "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
ren "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}" MyFolder
echo UNLOCKED
goto PAUSE
:FAIL
Color 04
echo INCORRECT PASSWORD
DEL /S /Q "MyFolder(2)"
echo S
echo FILES DELETED
echo SYSTEM SHUTDOWN
goto end
:CORRECT
echo
:PAUSE
PAUSE
:MDMYFOLDER(2)
:END
end
MD Myfolder(2)
Thnx for the help.

The usual way to delete a subdirectory it to use RMDIR (or RD short short). The system documentation for RD is as follows (generated using RD /?)
Removes (deletes) a directory.
RMDIR [/S] [/Q] [drive:]path
RD [/S] [/Q] [drive:]path
/S Removes all directories and files in the specified directory
in addition to the directory itself. Used to remove a directory
tree.
/Q Quiet mode, do not ask if ok to remove a directory tree with /S

Related

Merging txt files with batch

#echo off
title Merge Text Files
color 9f
echo Welcome to the program!
set /p loc=Please enter the location of folder where all txt files are located:
cd %loc%
set /p name=Type the name of new merged file (without adding ".txt"):
echo Press enter button to continue!
pause>nul
copy /b *.txt %name%.txt
echo Done!
pause>nul
echo Press any key to exit!
pause>nul
exit
I'm getting an error incorrect syntax
Can you tell me where my mistake is

"Echo is OFF" text always inserted into batch file when SET variable is written to a batch file IF the file is made by another batch program

I am trying to create a batch file using another batch program using:
#echo {code here}>>batch-program.bat, but whenever I try to write code to write the contents of a SET variable to a text file, the batch program does not write the code into the other batch file, but instead writes "Echo is OFF."
Code is here:
#echo off
#echo #echo off>>apt.bat
#echo color 2A>>apt.bat
#echo echo example-batch>>apt.bat
#echo cd C:/Users/Default/apt/assets>>apt.bat
#echo mkdir cmdInput>>apt.bat
#echo cd C:/Users/Default/apt/assets/cmdInput>>apt.bat
#echo set /p cmdInput= cmd->>apt.bat
#echo %cmdInput%>>used-cmdInput.txt>>apt.bat
#echo pause>>apt.bat
This should have created a batch file named apt.bat, and written into the batch file:
#echo off
echo color 2A
echo example-batch
cd C:/Users/Default/apt/assets
mkdir cmdInput
cd C:/Users/Default/apt/assets/cmdInput
set /p cmdInput= cmd-
%cmdInput%>>used-cmdInput.txt
pause
but the 9th line (%cmdInput%>>used-cmdInput.txt)
is instead converted into the text line "Echo is OFF".
Have I done anything wrong, or is it just a really weird bug?
EDIT: I found another problem in the program, that because mkdir cmdInput is always run when apt.bat is run, so it displays a error message because of apt.bat trying to create the directory cmdInput though it already exists. apt/assets. So I have changed the code a bit, so that the directory cmdInput is created in the first "creation" batch file (the program that was used to create apt.bat). mkdir cmdInput has been removed from apt.bat.
You need to escape > with ^ but you need to escape % with %
#echo %%cmdInput%%^>^>used-cmdInput.txt>>apt.bat
unless you want to output the contents of cmdinput where you need
#echo %cmdInput%^>^>used-cmdInput.txt>>apt.bat
You can add 2>nul to the end of a md command to suppress the error message generated if the directory already exists.
You should use backslashes \ in directorynames, not forward slashes /. In winbat, the forward slash is often used for command switches. Sometimes forward slashes will work for directorynames, but backslashes always work.
You just have to "escape" the percent-signs with another % and other special chars like > with a caret) to prevent evaluation of your variable %cmdInput% (which probably is empty - therefore the Echo is off).
Also a single #echo off is sufficient. No need to add a # to every line.
#echo off
echo #echo off>>apt.bat
echo color 2A>>apt.bat
echo echo example-batch>>apt.bat
echo cd C:/Users/Default/apt/assets>>apt.bat
echo mkdir cmdInput>>apt.bat
echo cd C:/Users/Default/apt/assets/cmdInput>>apt.bat
echo set /p cmdInput= cmd->>apt.bat
echo %%cmdInput%%^>^>used-cmdInput.txt>>apt.bat
echo pause>>apt.bat
A more elegant way is to use only a single redirection (cmdhas to open the file for writing just once):
#echo off
(
echo #echo off
echo color 2A
echo echo example-batch
echo cd C:/Users/Default/apt/assets
echo mkdir cmdInput
echo cd C:/Users/Default/apt/assets/cmdInput
echo set /p cmdInput= cmd-
echo %%cmdInput%%^>^>used-cmdInput.txt
echo pause
)>apt.bat

Populate a batch from a file

I have been trying to work this 1 out it's a fairly simple batch but I'm pretty much lost, the searches got my close but none seemed to work so I'm hoping somebody can help with this.
I have a batch file, it is far from optimized but basically what I am trying to do now is use the ping I have running in the batch which is written to check.csv should then populate the individual IP's at the beginning (hope that makes sense).
in a nut shell
The variables at the beginning should be populated from check.csv
Here is the code
'#echo off
rem set the individual IP addresses
Rem these are manually set at present, I want these set automatically from check.csv which is created at the end of this file at present but will be moved infront..
set sarah=10.1.14.106
set richard=10.1.15.135
set kathh=10.1.12.79
edited out a number of these to reduce the code on screen for you, but they are all the same format, name & IP
rem set the individual profile name
set n1=sharman
set n2=rburrell
removed more of the same (just duplicates the above code for a different user)
rem set the computer name
set sarahPC=PB7B237
set richardPC=PB1VAL9
removed more of the same
REM set the main install paths
set M3="C:\Users\dclare\Documents\VW Compiler\Installers\M3.exe"
set H3="C:\Users\dclare\Documents\VW Compiler\Installers\H3.exe"
set MooresInst="C:\Users\dclare\Documents\VW Compiler\Catalogues\PD PS\Catalogue"
Rem menu
#echo off
cls
:start
echo.
echo 1 Ping
echo 2 List
echo 3 DBase Installers
echo 4 EXE Installers
echo 5 End
echo.
#CHOICE /C:12345
if errorlevel 5 goto End
if errorlevel 4 goto EXEInstallers
if errorlevel 3 goto DBase
if errorlevel 2 goto List
if errorlevel 1 goto Ping
goto End
:End
Pause
Exit
:EXEInstallers
echo EXE Installers
echo.
echo Upload folder
Xcopy %M3% "S:\# All Public\Information Technology\CAD\VWorlds" /s /y /q
Xcopy %H3% "S:\# All Public\Information Technology\CAD\VWorlds" /s /y /q
echo.
echo %sarah%
Xcopy %M3% "\\%sarah%\c$\Users\%n1%\Desktop\" /s /y /q
Xcopy %H3% "\\%sarah%\c$\Users\%n1%\Desktop\" /s /y /q
echo.
echo %richard%
Xcopy %M3% "\\%richard%\c$\Users\%n2%\Desktop\" /s /y /q
Xcopy %H3% "\\%richard%\c$\Users\%n2%\Desktop\" /s /y /q
echo.
removed more of the same (just duplicates the above code for a different user)
goto Start
:DBase
echo Database Installers
echo.
echo %sarah%
Xcopy %MooresInst% "\\%sarah%\c$\Virtual Worlds\Catalogue" /s /y /q
echo.
echo %richard%
Xcopy %MooresInst% "\\%richard%\c$\Virtual Worlds\Catalogue" /s /y /q
echo.
removed more of the same (just duplicates the above code for a different user)
echo
Goto Start
:List
echo.
echo This is a list of the IP's used currently, check against any that fail.
echo.
echo Name Puter IP
echo sarah %sarahPC%%sarah%
echo richard %richardPC% %richard%
echo kathh %kathhPC% %kathh%
echo amarie %amariePC% %amarie%
removed more of the same (just duplicates the above code for a different user)
echo.
Pause
Goto Start
:Ping
#echo off
if exist "C:\Users\dclare\Documents\VW Compiler\Copy to Desktop\Results.csv" Del /s /q "C:\Users\dclare\Documents\VW Compiler\Copy to Desktop\Results.csv"
Echo Pinging list...
set ComputerList=list.txt
pause
Echo Computername,IP Address>Final.csv
setlocal enabledelayedexpansion
for /f "usebackq tokens=*" %%A in ("%ComputerList%") do (
for /f "tokens=3" %%B in ('ping -n 1 -l 1 %%A ^|findstr Reply ^|^| echo Not found Failed:') do (
set IPadd=%%B
echo %%A,!IPadd:~0, -1!>>Check.csv
))
pause
Goto Start'
here is the check.csv file contents
PB1VAL9 10.1.15.135
PB7B218 Failed
PB1VAL8 10.1.15.111
PB7B210 10.1.5.253
removed more of the same (just duplicates the above code for a different user)
Try this routine out to set your variables to ip addresses:
echo off
for /f "usebackq tokens=1,2" %%a in ("check.csv") (
echo setting %%a=%%b
set "%%a=%%b"
)
With this ping routine it shows if it is online or offline. I had to guess what you were trying to do as we don't have your files to test it.
Give us a sample of Check.csv if you want to populate a set of variables with the IP addresses inside it.
:Ping
#echo off
Del /s /q "C:\Users\dclare\Documents\VW Compiler\Copy to Desktop\Results.csv" 2>nul
Echo Pinging list...
set "ComputerList=list.txt"
pause
Echo Computername,IP Address>Final.csv
for /f "usebackq delims=" %%A in ("%ComputerList%") do (
ping -n 1 -l 1 %%A >nul
if not errorlevel 1 (
>>Check.csv echo %%A,online
) else (
>>Check.csv echo %%A,offline
)
)
pause
Goto Start

Batch file which saves user input path with spaces to a text file. (win 7)

First of all i am a noob in programming.
I am trying to make a batch file which takes an installed directory of a program as user input when run for the first time (means it should not ask for the directory the second time it is run). By searching for various scripts, i reached till here,
#echo off
Echo =============================================
echo Directory
Echo =============================================
setlocal enableextensions enabledelayedexpansion
set /p mypath=Please specify install directory;
Echo %mypath% ----was what you typed
pause
echo start>temp.txt
echo %mypath%>>temp.txt
echo \programfolder\program.exe>>temp.txt
echo -argument -argument>>temp.txt
setlocal enabledelayedexpansion
set FINAL=
for /f %%a in (temp.txt) do (
set FINAL=!FINAL!%%a
)
echo %FINAL%>input.txt
del /q temp.txt
Pause
start "<input.txt"
This saves the input path in the "input.txt" text file, and runs the program the next time it is launched.
I want the text file to have the saved path as "start driveletter:\foldername\foldername with spaces\programfolder\program.exe" -arguments
However the "start", "program folder", "program.exe" and "-arguments" are fixed.
The user input path should get saved in- %mypath%.
The does what you asked, I think:
#echo off
if exist "go.bat" go.bat
set /p "mypath=Please specify install directory; "
Echo "%mypath%" ----was what you typed
pause
>"go.bat" echo #start "" "%mypath%\programfolder\program.exe" -argument -argument

Windows Batch: Automatically upload local CSV file list via FTP

I built a script that reads a CSV file for a list of files and then uploads them to an FTP server. The CSV structure is like this:
local_file,remote_file
The script creates a text file with all the necessary FTP commands and then runs the FTP command. Everything works, except that the for loop executes code that is outside of it's command, i.e., everything below echo put "%1" "%2" >> %Commands% is also executed on every for loop, and instead of getting a nicely formatted file with all the put commands I get this (from the commands file output):
open servername
username
password
binary
put "local_path_to\first_file_on_the_list.php" "remote_path_to\first_file_on_the_list.php"
close
bye
put "-d" "-i"
close
bye
put "-d" "-i"
close
bye
put "-d" "-i"
close
...
Here is the script code:
#echo off
setlocal EnableExtensions
rem Connection credentials
set Server=servername
set UserName=username
set Password=password
set Commands="commands.txt"
echo open %Server% > %Commands%
echo %UserName% >> %Commands%
echo %Password% >> %Commands%
echo binary >> %Commands%
rem Read the CSV file line by line
for /f %%a in (matches3.csv) do call :parse %%a
rem Transform CSV line into FTP put commmand
:parse
echo put "%1" "%2" >> %Commands%
:end
rem Add commands to close ftp conn
echo close >> %Commands%
echo bye >> %Commands%
rem Perform the FTP upload
echo loggin in to ftp...
FTP -d -i -s:%Commands% %Server%
echo finished.
pause
rem Clean up.
if exist %Commands% del %Commands%
endlocal
exit
I don't understand why everything below :end is getting executed!
Thank you very much in advance
Why shouldn't the code below :end execute? You never put anything in the script to end the :parse routine.
The first ewall answer should almost work, except you need a GOTO :END after the FOR statement. I don't see how a proper implementation of his suggestion can lead to an endless loop.
Another option is to simply move your subroutine after your EXIT statement.
You have other hidden problems. File paths/names can contain spaces, so the default FOR delimiter of space,tab will not preserve the entire line if there are spaces. The default EOL will also cause any line that begins with ; to be ignored. That is a potential (but unlikely) problem, because a valid filename can begin with ;.
The solution is to set EOL to a character that cannot begin a valid file spec, and set DELIMS to nothing: "EOL=: DELIMS="
It is much more efficient to enclose all your file writing lines within parentheses and redirect the output just once. It's also easier to write and looks better.
Edit - The original script was attempting to connect to the server in both the ftp script and the ftp command line. One or the other had to be removed. I removed it from the ftp script.
#echo off
setlocal EnableExtensions
rem Connection credentials
set Server=servername
set UserName=username
set Password=password
set Commands="commands.txt"
(
echo %UserName%
echo %Password%
echo binary
rem Read the CSV file line by line
for /f "eol=: delims=" %%a in (matches3.csv) do call :parse %%a
rem Add commands to close ftp conn
echo close
echo bye
)>%Commands%
rem Perform the FTP upload
echo logging in to ftp...
FTP -d -i -s:%Commands% %Server%
echo finished.
pause
rem Clean up.
if exist %Commands% del %Commands%
endlocal
exit
rem Transform CSV line into FTP put commmand
:parse
echo put "%1" "%2"
The :end doesn't stop the CALL function from running, it's just a label. Instead, I suspect you meant to use GOTO :eof, which would return to the for-loop:
...
rem Read the CSV file line by line
for /f %%a in (matches3.csv) do call :parse %%a
rem Transform CSV line into FTP put commmand
:parse
echo put "%1" "%2" >> %Commands%
goto :eof
rem Add commands to close ftp conn
...
Or, rather than using a call, you may find it a lot simpler to use parenthesis, like this:
...
rem Read the CSV file line by line
for /f %%a in (matches3.csv) do (
rem Transform CSV line into FTP put command
echo put "%%a" "%%b" >> %Commands%
)
rem Add commands to close ftp conn
...
maybe this works
#echo off
setlocal EnableExtensions
rem Connection credentials
set Server=servername
set UserName=username
set Password=password
set Commands="commands.txt"
echo open %Server% > %Commands%
echo %UserName% >> %Commands%
echo %Password% >> %Commands%
echo binary >> %Commands%
rem Read the CSV file line by line
for /f %%a in (matches3.csv) do call :parse %%a
rem Transform CSV line into FTP put commmand
:parse
echo put "%1" "%2" >> %Commands%
:end
rem Add commands to close ftp conn
echo close >> %Commands%
echo bye >> %Commands%
rem Perform the FTP upload
echo loggin in to ftp...
FTP -d -i -s:%Commands% %Server%
echo finished.
pause
rem Clean up.
if exist %Commands% del %Commands%
endlocal
exit /b
rem Transform CSV line into FTP put commmand
:parse
echo put "%1" "%2" >> %Commands%
exit /b
:end
rem Add commands to close ftp conn
echo close >> %Commands%
echo bye >> %Commands%
exit /b