using wildcards to copy directories in .bat files - batch-file

I have created a .bat file where i input a job number. I want to copy the contents of one folder using the input value with wildcards to another with only the input value. It will not copy the contents of this folder using wildcards. Any suggestions?
sample:
Input = 19000
c:\test2\19000 project\test.doc
c:\test\19000
.bat file below
COLOR 0a
:USERID
CLS
ECHO Enter User Name:
ECHO > NUL
SET /p USERID=
IF %USERID% EQU c GOTO PASSWORD
IF %USERID% LSS c GOTO INVALID
IF %USERID% GTR c GOTO INVALID
:PASSWORD
CLS
ECHO Enter Password:
SET /p PASSWORD=
IF %PASSWORD% EQU c GOTO :job number
IF %PASSWORD% LSS c GOTO INVALID
IF %PASSWORD% GTR c GOTO INVALID
CLS
:INVALID
ECHO INAVLID LOGIN.....
PING -n 3 127.0.0.1 > NUL
ECHO Please try again...
PING -n 3 127.0.0.1 > NUL
GOTO USERID
:Job Number
Set input1=
set /p input1= Job Number:
cls
ECHO OFF
c:
cd\
xcopy "c:\test2\%input1% "*" " "c:\test\%input1%" /e
cd\
c:
cls
EOF
exit

This should xcopy any folder that matches the query with a wildcard after it, to the c:\test\ folder with the same folder structure.
#echo off
COLOR 0a
CLS
:USERID
SET /p "USERID=Enter User Name: "
IF "%USERID%" NEQ "c" GOTO INVALID
ECHO(
SET /p "PASSWORD=Enter Password: "
IF "%PASSWORD%" NEQ "c" GOTO INVALID
ECHO(
Set "input1="
set /p "input1=Job Number: "
for /d /r "c:\test2\" %%a in ("%input1%*") do (
xcopy "%%a\*.*" "c:\test\%%~nxa\" /s/h/e/k/f/c
)
cls
exit
goto :EOF
:INVALID
ECHO INVALID LOGIN.....
PING -n 3 127.0.0.1 > NUL
ECHO Please try again...
PING -n 3 127.0.0.1 > NUL
goto :userid

Related

CMD - Input from another cmd

I have one cmd where you do a choice (1 for login, 2 for register, ...) and then you enter the user, and password (set /p user="User: " , set /p password="Password: ") and what I want to do is enter the choice, username and password with another batch file that I make...
What I tried:
#echo off
title Wizzard-Cracker
color a
cd C:\Users\Wolfy\Documents\Wolfy\Subory .BAT\Wizzard
:A
start wizzard.cmd /K set choice=1 & set user=wolfy & set password=sombest
Login Batch:
#echo off
title Login and Register
color F
setlocal enableDelayedExpansion
:A
echo [Login and Register]
echo [1.][Login]
echo [2.][Register]
echo [3.][Exit]
set /p input="Choose: "
if "%input%"=="1" (
cls
goto login
)
if "%input%"=="2" (
cls
goto register
)
if "%input%"=="3" (
cls
exit
)
:login
echo [Login]
set /p user=User:
echo.
set /p password=Password:
echo.
if NOT EXIST data/%user%.userfile (
echo User %user% isn't registered.
ping localhost -n 2 >nul
cls
goto login
)
set /p correct=<data/%user%.userfile
set chars=0123456789abcdefghijklmnopqrstuvwxyz
for /L %%N in (10 1 36) do (
for /F %%C in ("!chars:~%%N,1!") do (
set "correct=!correct:-%%N=%%C!"
)
)
if "%password% "=="!correct!" (
echo Login successful.
ping localhost -n 2 >nul
cls
goto console
) else (
echo Incorrect password entered.
ping localhost -n 2 >nul
cls
goto login
)
:register
echo [Register]
set /p rUser=Register User:
echo.
set /p rPassword=Register Password:
echo.
if EXIST data/%rUser%.userfile (
echo User is already registered.
goto register.choice
)
set chars=0123456789abcdefghijklmnopqrstuvwxyz
for /L %%N in (10 1 36) do (
for /F %%C in ("!chars:~%%N,1!") do (
set "rPassword=!rPassword:%%C=-%%N!"
)
)
echo !rPassword! > data/%rUser%.userfile
echo Registration was successful!
ping localhost -n 2 >nul
cls
goto A
:register.choice
set /p choice="Go to Login [Y/N]: "
if %choice%==y (
cls
goto login
)
if %choice%==Y (
cls
goto login
)
if %choice%==n (
cls
goto register
)
if %choice%==N (
cls
goto register
)
How do I do that?
Here's a quick rewrite, there may be errors or things you would prefer not to change but it should show you a way of doing it.
#ECHO OFF
TITLE Login and Register
COLOR F
SETLOCAL ENABLEDELAYEDEXPANSION
IF "%~1" NEQ "1" (GOTO :A) ELSE (IF "%2" EQU "" (GOTO :A) ELSE (SET "user=%~2"
IF "%~3" EQU "" (SET password=)
GOTO :vlog))
:A
CLS
ECHO [Login and Register]
ECHO [1.][Login]
ECHO [2.][Register]
ECHO [3.][Exit]
CHOICE /C 123 /N
IF ERRORLEVEL 3 EXIT/B
IF ERRORLEVEL 2 GOTO :register
:login
CLS
ECHO [Login]
SET /P user=User:
IF NOT DEFINED user GOTO :login
:vlog
IF "%user:*=%" NEQ "%user%" GOTO :login
IF NOT EXIST data/%user%.userfile (
ECHO User %user% isn't registered.
TIMEOUT 2 >NUL
GOTO :login
)
:pass
SET /P correct=<data/%user%.userfile
ECHO.
IF NOT DEFINED password (
SET /P password=Password:
)
IF NOT DEFINED password GOTO :pass
SET chars=0123456789abcdefghijklmnopqrstuvwxyz
FOR /L %%N IN (10 1 36) DO (
FOR %%C IN (!chars:~%%N,1!) DO (
SET "correct=!correct:-%%N=%%C!"
)
)
IF "%password%"=="%correct%" (
ECHO.
ECHO Login successful.
TIMEOUT 2 >NUL
GOTO :console
)
ECHO Incorrect password entered.
TIMEOUT 2 >NUL
GOTO :pass
:register
CLS
ECHO [Register]
SET /P rUser=Register User:
IF NOT DEFINED rUser GOTO :register
IF "%rUser:*=%" NEQ "%rUser%" GOTO :register
IF EXIST data/%rUser%.userfile (
ECHO.
ECHO User is already registered.
GOTO :register.choice
)
:rpass
SET /P rPassword=Register Password:
IF NOT DEFINED rPassword GOTO :rpass
SET chars=0123456789abcdefghijklmnopqrstuvwxyz
FOR /L %%N IN (10 1 36) DO (
FOR %%C IN (!chars:~%%N,1!) DO (
SET "rPassword=!rPassword:%%C=-%%N=!"
)
)
>data/%rUser%.userfile ECHO %rPassword%
ECHO.
ECHO Registration was successful!
TIMEOUT 2 >NUL
GOTO :A
:register.choice
ECHO.
CHOICE /C YN /M "Go to Login"
IF ERRORLEVEL 2 GOTO :register
GOTO :login
:console
REM Section is Missing
You should be able to run it in one of these ways, depending upon your requirements:
"X:\SomePathTo\wizzard.cmd" 1 wolfy sombest
start "" "X:\SomePathTo\wizzard.cmd" 1 wolfy sombest
call "X:\SomePathTo\wizzard.cmd" 1 wolfy sombest
Of course if you cannot change the actual wizzard.cmd script then I'm afraid that you have no way of bypassing the prompts' which should, after all, only waste a couple of seconds of your precious time.
instead of set /p correct = <C:\file , get rid of the p and type set correct =<C:\file
Why would you use a different batch file? But however, if you want to parse data between to batch instances, make use of command line arguments. A simple google to that gives you a clear explanation. But be carefull. Batch files are not safe to use passwords. You have to learn programming for that. VB.net is an example language which is easy to learn

Class: ECHO is off from batbox

I wanted to make a little game using batbox. I wanted it to display which class you used but it didn't work. It showed "Class: ECHO is off." And I don't know what's wrong. Here's the code I used:
:charectarselect
cd gamedata
color 0f
cls
batbox /g 0 0
echo Select class:
echo.
echo %mage%
echo %worrior%
set /p classes=Class:
echo %classes%>pclass.gm
goto login
:login
color 0f
cd gamedata
set /p pclass=<pclass.gm
set /p user=<username.gm
set /p pass=<password.gm
if "%pclass%" == "1" echo Mage>pclass.gm
cls
title Login
echo ======================LOGIN======================
set /p login=Username :
if %login% == %user% goto password
goto login
:password
cls
echo ======================LOGIN======================
set /p passw=Password :
if %passw% == %pass% goto game
goto password
:game
cls
batbox /c 0xc9 /d "Health: %health%"
batbox /g 11 0 /c 0x9c /d "Strength: %strength%"
batbox /g 23 0 /c 0x9a /d "Class: %pclass%"
pause >nul
The problem is caused by you entering 1 for class. Specifically, it's caused by the line echo %classes%>pclass.gm
When you entered 1 in character selection, it expands that command to echo 1>pclass.gm. Unfortunately, 1> is used to redirect output from STDOUT to a file, and so batch is interpreting this as a command to send the output of echo to pclass.gm. By itself, echo displays whether echo is on or off.
You can get around this by adding parentheses to the echo line:
(echo %classes%)>pclass.gm

after compiling a batch file to exe it stops working

I'm writing a small folder locking software. Recently a masking function was integrated, so that the password input is masked. When in a bat file the program works perfectly, however after compiling the program stops working. The problem ,I think, lies in the "masking" code, as it starts an endless loop and masks every input, even the "Enter" stroke, thus preventing the program from further executing. I even tried iexpress, but it also gives an Error, namely:
Error creating process Command.com/c
C:\Users...\AppData\Local\Temp\IXP000.TMP\Folder~1.BAT
Can someone please double check my code and tell me what is wrong, as I am still learning and could not figure out how to fix it.
Thanks in advance.
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
mode con cols=80 lines=25
color 5F
title Folder Locker by KBKOZLEV
:SETPASS
set "tipp="
set "password="
if exist "password.txt" (
set /p password=<password.txt
attrib +h +s "password.txt"
)
if exist "tipp.txt" (
set /p tipp=<tipp.txt
attrib +h +s "tipp.txt"
)
:START
if exist "Locked" goto :OPEN
if exist "Unlocked" goto :LOCK
if not exist "Unlocked" goto :MDLOCKER
:LOCK
ren "Unlocked" "Locked"
attrib +h +s "Locked"
echo(
echo Folder locked.
CHOICE /C X /T 1 /D X > nul
goto :END
exit
:MDLOCKER
md "Unlocked"
echo>password.txt 1234
echo>tipp.txt 1234
attrib +h +s "password.txt"
attrib +h +s "tipp.txt"
cls
echo(
echo Private folder created successfully.
CHOICE /C X /T 1 /D X > nul
goto :END
:OPEN
color 2F
cls
echo ********************************************************************************
echo Folder Locker by KBKOZLEV v.01
echo.
echo ********************************************************************************
echo ---- Enter password to unlock folder, or enter "new" to set a new password. ----
echo --------------------------------------------------------------------------------
echo.
echo Password tipp: %tipp%
echo(
set "pass="
rem set /p "pass=Password: "
Set /P "=Password:" < Nul
Call :PasswordInput pass
if /i "%pass%"=="new" goto :NEWPASS
if "%pass%"=="%password%" (
attrib -h -s "Locked"
ren "Locked" "Unlocked"
echo(
echo Folder unlocked successfully.
goto :END
)
goto :FAIL
:FAIL
color 4F
cls
echo(
echo Invalid password, please try again.
CHOICE /C X /T 1 /D X > nul
cls
goto :OPEN
:NEWPASS
color 8F
cls
echo(
set "oldpass="
rem set /p "oldpass=Old password: "
Set /P "=Old Password:" < Nul
Call :PasswordInput oldpass
if not "%oldpass%"=="%password%" goto :FAIL
:ENTERNEW
color 8F
cls
echo(
set "newpass=""
rem set /p "newpass=New password: "
Set /P "=New Password:" < Nul
Call :PasswordInput newpass
set newpass=%newpass:"=%
if "%newpass%"=="" (
echo(
echo Invalid new password, please enter new password again.
CHOICE /C X /T 1 /D X > nul
goto :ENTERNEW
)
if exist "password.txt" attrib -h -s "password.txt"
echo>password.txt %newpass%
echo(
set "passtipp=""
set /p "passtipp=New tipp: "
set passtipp=%passtipp:"=%
if exist "tipp.txt" attrib -h -s "tipp.txt"
if not "%passtipp%"=="" (
echo>tipp.txt %passtipp%
) else (
del "tipp.txt"
)
goto :SETPASS
:END
color
EndLocal
Goto :Eof
:PasswordInput
::Author: Carlos Montiers Aguilera
::Last updated: 20150401. Created: 20150401.
::Set in variable Line a input password
::
::Update 20150503: http://stackoverflow.com/users/3439404/josefz?tab=profile
::Changes made in next lines:
:: SetLocal EnableDelayedExpansion
:: If !CHR!==!CR! Echo(&EndLocal&set "%1=%Line%"&Goto :Eof
::Usage:
:: Call :PasswordInput variableName
::where variableName is a name of output variable (by reference call)
::
SetLocal EnableDelayedExpansion
For /F skip^=1^ delims^=^ eol^= %%# in (
'"Echo(|Replace.exe "%~f0" . /U /W"') Do Set "CR=%%#"
For /F %%# In (
'"Prompt $H &For %%_ In (_) Do Rem"') Do Set "BS=%%#"
Set "Line="
:_PasswordInput_Kbd
Set "CHR=" & For /F skip^=1^ delims^=^ eol^= %%# in (
'Replace.exe "%~f0" . /U /W') Do Set "CHR=%%#"
If !CHR!==!CR! Echo(&EndLocal&set "%1=%Line%"&Goto :Eof
If !CHR!==!BS! (If Defined Line (Set /P "=!BS! !BS!" <Nul
Set "Line=!Line:~0,-1!"
)
) Else (Set /P "=*" <Nul
If !CHR!==! (Set "Line=!Line!^!"
) Else Set "Line=!Line!!CHR!"
)
Goto :_PasswordInput_Kbd
You are executing the 16 bit command.com (only in Win 32) not the 32 or 64 bit cmd.exe. It doesn't suport brackets, long filenames, or set /p.
It also doesn't support long file names.

instead of not printing anything batch file prints echo is off

I have a very little problem, which gets on my nerves. I wrote a program for locking folders and I gave it the option to change the password as well as the tipp, however, if no tipp is typed, I want it to just print nothing. Instead it says "echo is off"
I guess it's the syntax, but I do not know where. Can someone double check it for me. thank you!
#Echo Off
setlocal EnableDelayedExpansion
mode con cols=80 lines=25
set /a "tries=3"
color 5F
title Folder Locker by KBKOZLEV
:SETPASS
set "tipp="
set "password="
if exist "password.txt" (
set /p password=<password.txt
attrib +h +s "password.txt"
)
if exist "tipp.txt" (
set /p tipp=<tipp.txt
attrib +h +s "tipp.txt"
)
:START
if exist "Locked" goto :OPEN
if exist "Unlocked" goto :LOCK
if not exist "Unlocked" goto :MDLOCKER
:LOCK
ren "Unlocked" "Locked"
attrib +h +s "Locked"
echo.
echo Folder locked.
CHOICE /C X /T 1 /D X > nul
goto :END
exit
:MDLOCKER
md "Unlocked"
echo>password.txt 1234
echo>tipp.txt 1234
attrib +h +s "password.txt"
attrib +h +s "tipp.txt"
cls
echo.
echo Private folder created successfully.
CHOICE /C X /T 1 /D X > nul
goto :END
:OPEN
color 2F
cls
echo ********************************************************************************
echo Folder Locker by KBKOZLEV v.01
echo.
echo ********************************************************************************
echo ---- Enter password to unlock folder, or enter "new" to set a new password. ----
echo You have %tries% attempts left.
echo --------------------------------------------------------------------------------
echo.
echo Password tipp: %tipp%
echo.
set "pass="
Set /P "=Password:" < Nul
set "psCommand=powershell -Command "$pword = read-host -AsSecureString ; ^
$BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword); ^
[System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)""
for /f "usebackq delims=" %%p in (`%psCommand%`) do set pass=%%p
if /i "%pass%"=="new" goto :NEWPASS
if "%pass%"=="%password%" (
attrib -h -s "Locked"
ren "Locked" "Unlocked"
echo.
echo Folder unlocked successfully.
goto :END
)
set /a tries=%tries -1
if %tries%==0 (
goto :FAIL2
)
goto :FAIL
:FAIL
color 4F
cls
echo.
echo Invalid password, please try again.
CHOICE /C X /T 1 /D X > nul
cls
goto :OPEN
:FAIL2
color 4F
cls
echo.
echo Invalid password, program will now close.
CHOICE /C X /T 2 /D X > nul
cls
goto :END
:NEWPASS
color 8F
cls
echo.
set "oldpass="
Set /P "=Old Password:" < Nul
set "psCommand=powershell -Command "$pword = read-host -AsSecureString ; ^
$BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword); ^
[System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)""
for /f "usebackq delims=" %%p in (`%psCommand%`) do set oldpass=%%p
if not "%oldpass%"=="%password%" goto :FAIL
:ENTERNEW
color 8F
cls
echo.
set "newpass=""
Set /P "=New Password:" < Nul
set "psCommand=powershell -Command "$pword = read-host -AsSecureString ; ^
$BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword); ^
[System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)""
for /f "usebackq delims=" %%p in (`%psCommand%`) do set newpass=%%p
set newpass=%newpass:"=%
if "%newpass%"=="" (
echo.
echo Invalid new password, please enter new password again.
CHOICE /C X /T 1 /D X > nul
goto :ENTERNEW
)
if exist "password.txt" attrib -h -s "password.txt"
echo>password.txt %newpass%
echo.
set "passtipp=""
set /p "passtipp=New tipp:"
set passtipp=%passtipp:"=%
if exist "tipp.txt" attrib -h -s "tipp.txt"
if not "%passtipp%"=="" (
echo>tipp.txt %passtipp%
) else (
del "tipp.txt"
)
goto :SETPASS
:END
color
EndLocal
exit
The only approach:
if "%passtipp%"=="" (echo(>tipp.txt) else (echo>tipp.txt %passtipp%)
Explanation: see all my comments to other answers (for the present), please...
To suppress the "ECHO is off" text when echoing a blank line, change echo to echo. with a trailing dot. Your script already does this for blank lines to the console. The same thing works for redirecting to files.
For example, this line will put "ECHO is off" into tipp.txt if the %passtip% variable is empty:
echo>tipp.txt %passtipp%
This line will cause tipp.txt to be empty:
>tipp.txt echo.%passtipp%
It's usually safe to append all instances of echo with trailing punctuation. The form echo. can be problem if there is a file named "echo" in the path; In that case, a safer alternative is echo(.
The root cause of your issue is that your syntax for redirecting data to a file is wrong. When echo is run by itself, it outputs whether echo is ON or OFF. So when you have lines like echo>password.txt 1234, you are saying "Take the output of echo and redirect it to password.txt 1234".
What you should be doing is echo 1234>password.txt, which will redirect the string 1234 to password.txt.
And then do the same thing for tipp.txt.
Also, I feel the need to point out that the word you're looking for is "tip," not "tipp."

How to do while loop in windows batch

I wrote a windows batch script to check and move files to another directory based on the list I put in a notepad file named list.txt. But I have no idea that how to set the while-loop. Only to jump out of the subroute when the condition fulfill.
In C Programming, we could write like this by setting a while-loop direcly. But seems in windows batch is quite different.
All I want is like this:
Directory A:
1. A.txt
2. B.txt
3. list.txt (By line sequential with filename want to move)
4. process.bat
Directory B:
None of files (Then move a file by order of line set in list.txt) OR
A.txt (If already existed a text file in directory, process.bat will pause before A.txt disappear)
Process.bat
#echo off
:readline
for /f "tokens=*" %%a in (list.txt) do call :processline %%a
goto :eof
:processline
if exist D:\DirectoryA\*.txt (
echo %time% >> D:\AutoLog\Log.txt
echo Previous job did not finished yet. >> D:\AutoLog\Log.txt
Timeout /t 30
echo.
)
set name=%*
if exist %name%.txt (
echo %time% >> D:\AutoLog\Log.txt
echo File found and processing %name%.txt now... >> D:\AutoLog\Log.txt
copy "%~dp0\%name%.txt" "D:\DirectoryB"
echo Transfer %name%.txt completed!! >> D:\AutoLog\Log.txt
echo. >> D:\AutoLog\Log.txt
Timeout /t 790
echo.
echo ==============================================================
)
:eof
Please guide me to finish the script by using a while-loop method. Thanks
I change some script sequence and it works now.
#echo off
:readline
for /f "tokens=*" %%a in (list.txt) do call :processline %%a
goto :eof
:processline
set name=%*
if exist C:\Test2\*.txt (
echo %date% %time% >> C:\Test2\Log.txt
echo Previous job did not finished yet. >> C:\Test2\Log.txt
Timeout /t 5
echo.
echo. >> C:\Test2\Log.txt
goto :processline
)
if exist %name%.txt (
echo %date% %time% >> C:\Test2\Log.txt
echo File found and processing %name%.txt now... >> C:\Test2\Log.txt
copy "%~dp0\%name%.txt" "C:\Test2"
echo Transfer %name%.txt completed!! >> C:\Test2\Log.txt
echo. >> C:\Test2\Log.txt
Timeout /t 10
echo.
echo ==============================================================
)
:eof
This will copy as well as count the number of lines from your text file..
# echo off
:TextPath
cls
set /p Input=#1 Enter the full path of the text file :
set /p Source=#2 Enter the full path of Source :
set /p Target=#3 Enter the full path of Destination :
:choice
set /P c=Ready to Continue[Y/N]?
if /I "%c%" EQU "Y" goto :Yes
if /I "%c%" EQU "N" goto :No
goto :choice
:Yes_Local
for /f "delims=" %%i in (%Input%) do echo f| xcopy /f /y "%Source%\%%i" "%Target%\%%i"
for /f %%C in ('Find /V /C "" ^< %Input%') do set Count=%%C
msg * No of Lines executed= %Count%
exit
:No
cls
color e
echo Redirecting to Main....
PING 127.0.0.1 -n 2 >NUL
cls
echo Please wait
PING 127.0.0.1 -n 4 >NUL
goto TextPath

Resources