I have been having problems with making a batch script that can write into a old text file. When i try to run it with the old text file it says that "access is denied" (so when i try to put any text into the existing file)
Anyways here is the code:
#echo off
title file editor/creator
set lineNR=0
:start
set /p ANS= Do you want to access a 1:"old file" or 2"create a new" [1/2]
if %ANS% EQU 1 ( goto old
) ELSE if %ANS% EQU 2 ( goto new
) ElSE ( echo invalid input & goto start)
:old
set /p name = what is the name of the file
set /p ending = what type of file is it
goto loop
:new
set /p name= what do you want the name of the file to be
set /p ending= what type of file do you want the file to be
echo %name%.%ending%
:Q1
set /p echo_off= do you want echo to be off? [y/n]
if %echo_off% EQU y (
echo #echo off >%name%.%ending%
goto loop
) ELSE IF %echo_off% EQU n (
goto loop
) ELSE (
goto Q1
)
:loop
echo press CTRL+LSHIF+C to end loop
goto loop1
:loop1
set /a lineNR=%lineNR% + 1
set /p line= %lineNR%:
echo %line% >>%name%.%ending%
// this is where it says that access is denied
goto loop1
It is just a simple, but a common issue.
set a = b
Creates a variable named a (with the space) and a value of b(with the space).
Remove the spaces and it will work.
Related
I was creating something with command line and came across this error - 'x was unexpected at this time', I tried switching the variable to x, %%x and %x% but none of them worked. Here are the code in the part:
echo Please enter the memory value [Recommended: 512M Xms 1024M Xmx]
set /p rams=Xms[MB]=
set /p ramx=Xmx[MB]=
echo %rams%|findstr /R "^[1-9][0-9]*$">nul
if NOT %errorlevel% EQU 0 (
echo Error: Please enter a valid number [Xms].
)
echo %ramx%|findstr /R "^[1-9][0-9]*$">nul
if NOT %errorlevel% EQU 0 (
echo Error: Please enter a valid number [Xmx].
)
if NOT %errorlevel% EQU 0 (
echo Execution failure: Invalid args
pause
goto start
)
set count=0
for %x in (*.jar) do set /a count+=1
if %count% == 1 (
java -Xms%rams%M -Xmx%ramx%M -jar BCU.jar
echo BCU.jar was executed.
pause
goto start
)
if %count% GEQ 1 (
echo [= = = = Multiple file found = = = =]
dir *.jar /B
echo [= = = = = = = = = = = = = = = = = =]
echo Please input file by the whole name with extension.
set /p file=Select File:
if exist %file% (
java -Xms%rams%M -Xmx%ramx%M -jar %file%
echo %file% was executed.
pause
) else (
echo File was not found.
pause
)
goto start
)
For some reason, the for command works normally when I execute it from the command prompt, but not when it was ran.
I ran the program as administrator
Only the for part is faulty, anything above it works normal
It is expected to have the count value defined and able to be used for the IF statement below
It is known that the java line works just fine without problem
For the full file it is located at: https://drive.google.com/file/d/1SgdoyCL5qaZGIWGJQDUKEJyEyUI2Y2u8/view?usp=sharing
Your immediate error is that you need to use %% within batch files, a single % works only on the command line. In other words, it should be:
for %%x in (*.jar) do set /a count+=1
(I'm not convinced you did try %%x as you state, since that actually works, but it may be that you just formatted it incorrectly).
But you'll strike another problem (at least) with using things like this (pared back and formatted for explanation):
if %count% GEQ 1 (
set /p file=Select File:
if exist %file% (
java -Xms%rams%M -Xmx%ramx%M -jar %file%
)
)
The entire block above is evaluated before it executes, meaning that %file% will be turned into whatever it was before the block started, despite the fact you're setting it within the block.
What you need to do is setlocal enabledelayedexpansion at the top of your file, and use the !file! delayed-expansion variant, something like:
setlocal enableextensions enabledelayedexpansion
:
if %count% GEQ 1 (
set /p file=Select File:
if exist !file%! (
java -Xms%rams%M -Xmx%ramx%M -jar !file!
)
)
:
endlocal
It's only needed for variables that can change within the loop, so not for the %rams% and %ramx% in this particular case - they're set before the if block starts.
Below is the Report.csv file, where I need to update the Status column based on the condition, I file in Lockbox exist. I have written the code for if file exist or not, but don't know how to update the Status column.
Lockbox Location Status Reason(N)
3080 Minot Y
1780 Minot N Not Scheduled
2280 Minot Y
3015 Windsor Y
2215 Windsor Y
1515 Windsor Y
29011 Windsor Y
Below is the code I have written. Please help..
#echo off
setlocal
set count=0
echo %time%
echo %date%
set y=%Date:~10,5%
set m=%Date:~4,2%
set d=%Date:~7,2%
if "%time:~0,1%"==" " (set tym=0%time:~1,1%) ELSE set tym=%time:~0,2%
set tm=%tym%%time:~2,0%
echo %tm%
pause
set pattern1=INGFINS.IMAGE.image1.29011.%y%%m%%d%%tm%
set pattern2=INGFINS.IMAGE.image2.2215.%y%%m%%d%%tm%
set pattern3=INGFINS.IMAGE.image3.1515.%y%%m%%d%%tm%
set pattern4=INGFINS.IMAGE.image4.3015.%y%%m%%d%%tm%
set pattern5=INGFINS.IMAGE.image5.1780.%y%%m%%d%%tm%
set pattern6=INGFINS.IMAGE.image6.2280.%y%%m%%d%%tm%
set pattern7=INGFINS.IMAGE.image7.3080.%y%%m%%d%%tm%
IF EXIST "C:\Users\Desktop\zipLocation\*%pattern1%*.<" (
ECHO "%pattern1% exist"
) ELSE (
ECHO "%pattern1% not exist"
)
IF EXIST "C:\Users\Desktop\zipLocation\*%pattern2%*.<" (
ECHO "%pattern2% exist"
) ELSE (
ECHO "%pattern2% not exist"
)
IF EXIST "C:\Users\Desktop\zipLocation\*%pattern3%*.<" (
ECHO "%pattern3% exist"
) ELSE (
ECHO "%pattern3% not exist"
)
IF EXIST "C:\Users\Desktop\zipLocation\*%pattern4%*.<" (
ECHO "%pattern4% exist"
) ELSE (
ECHO "%pattern4% not exist"
)
IF EXIST "C:\Users\Desktop\zipLocation\*%pattern5%*.<" (
ECHO "%pattern5% exist"
) ELSE (
ECHO "%pattern5% not exist"
)
IF EXIST "C:\Users\Desktop\zipLocation\*%pattern6%*.<" (
ECHO "%pattern6% exist"
) ELSE (
ECHO "%pattern6% not exist"
)
IF EXIST "C:\Users\Desktop\zipLocation\*%pattern7%*.<" (
ECHO "%pattern7% exist") ELSE (
ECHO "%pattern7% not exist"
)
rem call "statusReport.vbs"
endlocal
pause
Although some other users tend to close your question, I give next solution hoping that it could help. Commented .bat script:
#ECHO OFF >NUL
SETLOCAL EnableExtensions DisableDelayedExpansion
rem set input and output files: change to meet your circumstances
set "_fileIn=d:\bat\so\files\39587196_Report.TSV" tab-separated values
set "_fileOut=d:\bat\so\files\39587196_ReportOut.csv" comma-separated values
rem empty output file
>"%_fileOut%" type NUL
rem show input file
type "%_fileIn%"
echo(
rem obtain %_datetime% variable in locale independent yyyymmddHHMMSS format
for /f "tokens=2 delims==" %%G in (
'wmic OS get localdatetime /value') do set "_datetime=%%G"
set "_datetime=%_datetime:~0,14%"
rem reduce %_datetime% variable to yyyymmddHH format as the OQ does
set "_datetime=%_datetime:~0,10%"
set count=0
echo %date% %time%
echo %_datetime%
set "_pattern1=INGFINS.IMAGE.image1.29011.%_datetime%"
set "_pattern2=INGFINS.IMAGE.image2.2215.%_datetime%"
set "_pattern3=INGFINS.IMAGE.image3.1515.%_datetime%"
set "_pattern4=INGFINS.IMAGE.image4.3015.%_datetime%"
set "_pattern5=INGFINS.IMAGE.image5.1780.%_datetime%"
set "_pattern6=INGFINS.IMAGE.image6.2280.%_datetime%"
set "_pattern7=INGFINS.IMAGE.image7.3080.%_datetime%"
set "_pattern8=INGFINS.IMAGE.image7.8888.%_datetime%" for testing purposes
set "_ForGDelims= delims=," CSV input: delimiter is Comma (0x2C Comma)
set "_ForGDelims=" TSV input: delimiter is Tab (0x09 Character Tabulation)
if /I "%_fileIn:~-4%"==".tsv" set "_ForGDelims=" soft-code instead of above line
set "_csvHeader=" CSV header not processed yet
rem iterate input file, line by line, tokenized
for /F "usebackq tokens=1,2,3,*%_ForGDelims%" %%G in ("%_fileIn%") do (
set "_Lockbox=%%~G"
set "_Location=%%~H"
set "_Status=%%~I"
set "_Reason=%%~J"
if defined _csvHeader (
rem show checked Lockbox value (no NewLine)
<NUL set /P "=checking %%~G"
set "_LockboxFoundInVariable="
rem iterate all _pattern* variables
for /F "tokens=1,2 delims==" %%g in ('set _pattern') do (
rem take 4th token in %_pattern*% value
for /F "tokens=4 delims=." %%# in ("%%~h") do (
if "%%#"=="%%~G" (
echo - found %%G in %%g:%%h
set "_LockboxFoundInVariable=%%g"
rem ??? I don't comprehend what this ↓ LessThan should mean ???
IF EXIST "C:\Users\Desktop\zipLocation\*%%h*.<" (
rem ??? I don't comprehend what this ↑ LessThan should mean ???
set "_Status=y" pattern exists
) ELSE (
set "_Status=n" pattern does not exist
)
)
)
)
rem echo NewLine if necessary i.e. if checked Lockbox value not in any _pattern*
if not defined _LockboxFoundInVariable (echo()
) else (
set "_csvHeader=anything" CSV header processed already
)
call :csvLineOut
)
rem show output file
echo(
type "%_fileOut%"
ENDLOCAL
goto :eof
:csvLineOut
rem you could use <TAB>s ↓ ↓ ↓ instead of commas
>>"%_fileOut%" echo %_Lockbox%,%_Location%,%_Status%,"%_Reason%"
goto :eof
Please read Delimiter-separated values about .csv vs. .tsv difference. Note that I don't keep using quotation marks to surround every field strictly…
Output:
==> D:\bat\SO\39587196.bat
Lockbox Located Status Reason(N)
3080 Minot Y
1111 Test Y
1780 Minot N Not Scheduled
2280 Minot Y
2215 Windsor Y
29011 Windsor Y
9999 Test Y
20.09.2016 17:41:53,87
2016092017
checking 3080 - found 3080 in _pattern7:INGFINS.IMAGE.image7.3080.2016092017
checking 1111
checking 1780 - found 1780 in _pattern5:INGFINS.IMAGE.image5.1780.2016092017
checking 2280 - found 2280 in _pattern6:INGFINS.IMAGE.image6.2280.2016092017
checking 2215 - found 2215 in _pattern2:INGFINS.IMAGE.image2.2215.2016092017
checking 29011 - found 29011 in _pattern1:INGFINS.IMAGE.image1.29011.2016092017
checking 9999
Lockbox,Located,Status,"Reason(N)"
3080,Minot,n,""
1111,Test,Y,""
1780,Minot,n,"Not Scheduled"
2280,Minot,n,""
2215,Windsor,n,""
29011,Windsor,n,""
9999,Test,Y,""
==>
Further resources (required reading, incomplete):
(command reference) An A-Z Index of the Windows CMD command line
(helpful particularities) Windows CMD Shell Command Line Syntax
(%~G, %~H etc. special page) Command Line arguments (Parameters)
(special page) EnableDelayedExpansion
(>, >> etc. special page) Redirection
(%_fileIn:~-4% etc.) Extract part of a variable (substring)
(%% doubled percent sign) Escape Characters, Delimiters and Quotes
I have been having troubles with batch-codes that I would expect to work, but don't...
Below is what I have written...
#echo off
cls
:loop
set /p "input=Input a number: "
set /a "number=%input%" 2>nul
REM check if input valid
if "%input%" NEQ "%number%" (
cls
Echo Please Enter a valid number! &Echo.&Echo.
goto :loop
)
Set /a Even=number%%2
if %Even% EQU 0 (
Echo Substituting Even Number in: x / 2
Echo set /p"=(%number%) / 2 = "
set /a answer=number/2
) Else (
Echo Substituting Odd Number in: 3x - 1
<nul set /p"=3(%number%)-1 = "
set /a answer=number*3
set /a answer=answer-1
)
Echo %answer%
Echo.
Echo.
goto :loop
Echo Unexpected Error . . .
pause
Exit
Whenever I input a number into the console, it does the math, like I want it to, but prints the number -1, and every time i input another number, the number goes to -2, -3, -4, so on.
Put a setlocal enableextensions at the beginning after the #echo off, e.g.
#echo off
setlocal enableextensions
cls
Also, I think you would also need to use delayed variable expansion (usually denoted by !var!), which would change your script above to something like this:
#echo off
setlocal enableextensions enabledelayedexpansion
cls
:loop
set /p "input=Input a number: "
set /a number=!input! 2>nul
REM check if input valid
if "!input!" NEQ "!number!" (
cls
Echo Please Enter a valid number!
Echo.
Echo.
goto :loop
)
REM Make sure that it is an integer put in (just in case)
set /a int=!number! %% 1
if "!input!" NEQ "!int!" (
cls
Echo Please Enter a valid number!
Echo.
Echo.
goto :loop
)
Set /a Even=!number! %% 2
if !Even! EQU 0 (
Echo Substituting Even Number in: x / 2
set /a answer=!number! / 2
) Else (
Echo Substituting Odd Number in: 3x - 1
set /a answer=!number! * 3 - 1
)
Echo !answer!
Echo.
Echo.
goto :loop
I also would like to point out that I also fixed a few other bugs (set /p isn't of any use in this script at all, especially in where it is used, and also you need the modulus to find even/odd).
I have a specific folder (mak) that has sub folders (1,2,3,4,5). I have a batch find and replace script that I want to run only on sub folders 1,2 and 3. How will this be possible ?
You could set up a varribel system were you can tell the computer that these are the files i want serached then
throught that you can us cd C:...your main file location\%your varible1% and then have it loop so it will add each varible to another then set it up to create temp files so i knows which on has been made, look somthing like this
#echo off
Echo Max Numbers: 5
:setn
set /p n = 1
:start
set /p V%n% = Enter File Path:
:answer
set /p done = Is that all? (Y/N)
echo temp file > %done%.txt
if exist Y.txt goto File
if exist N.txt goto Varible%n%
goto answer
:varible1
echo %n%>%n%.txt
if %n% = 1 set /p n = 2
goto start
:Varible2
echo %n%>%n%.txt
if %n% = 2 set /p n = 3
goto start
:Varible3
echo %n%>%n%.txt
if %n% = 3 set /p n = 4
goto start
:Varible4
echo %n%>%n%.txt
if %n% = 4 set /p n = 5
goto start
:varible5
goto File
:file
"Your file code here"
this code may not work it depends on the type of windows you have.
I am doing user input and checking to see if they typed n or y... and it's not working because it says both either way.
Here's what I have:
#echo off
set /P theuserinput="Type your name: "
echo So your name is: %theuserinput%?
set /P isit="Y/N: "
echo You typed: %isit%
if (%isit% == "y") goto :saidyes
if (%isit% == "n") goto :saidno
:saidyes
echo Hooray!
:saidno
echo Aww
PAUSE
First you can add a default goto after the two if's.
Then, in both tests you have to add the quotes around %isit% and to remove the parenthesis. You may also add the /I flag to do an insensitive string comparison.
Finally, add goto after each echo to jump over the next one.
#echo off
set /P theuserinput="Type your name: "
echo So your name is: %theuserinput%?
set /P isit="Y/N: "
echo You typed: %isit%
if /I "%isit%" == "Y" goto :saidyes
if /I "%isit%" == "N" goto :saidno
goto :error
:saidyes
echo Hooray!
goto :end
:saidno
echo Aww
goto :end
:error
echo ERROR
:end
PAUSE
Need change in syntax
Here is the modified code
#echo off
set /P theuserinput="Type your name: "
echo So your name is: %theuserinput%?
set /P isit="Y/N: "
echo You typed: %isit%
if "%isit%" == "Y" GOTO saidyes
if "%isit%" == "N" GOTO saidno
:saidyes
echo Hooray!
GOTO paused
:saidno
echo Aww
:paused
PAUSE
....
In above example The Y/N is supposed to be capital letter only.
There are a few things I would do differently to this to ensure that it works properly...
First off the corrected code is the following, but at the bottom is my suggested code:
#echo off
setlocal
set theuserinput=
set /P theuserinput="Type your name: "
echo So your name is: %theuserinput%?
set /P isit="Y/N: "
echo You typed: %isit%
if '%isit%'== 'Y' goto saidyes
if '%isit%'=='N' goto saidno
:saidyes
echo Hooray!
goto end
:saidno
echo Aww
goto end
:end
pause
endlocal
exit
However, to make sure that you only get Y or N there are a few things I would put in...
First I would make sure that you only grab the first letter by adding:
IF NOT '%isit%'=='' set isit=%isit:~0,1%
Next I would make sure that you only have Capital letters, so you make the swap if they are lowercase, because this will actually not work in some cases if the CaSe isn't correct:
if '%isit%'== 'y' set isit=Y
if '%isit%'== 'Y' goto :saidyes
if '%isit%'=='n' set isit=N
if '%isit%'=='N' goto :saidno
The final edit for this file could look like the following:
#echo off
:top
set theuserinput=
set /P theuserinput="Type your name: "
echo So your name is: %theuserinput%?
set /P isit="Y/N: "
IF NOT '%isit%'=='' set isit=%isit:~0,1%
IF '%isit%'=='y' set isit=Y
IF '%isit%'=='Y' goto saidyes
IF '%isit%'=='n' set isit=N
IF '%isit%'=='N' goto saidno
goto top
:saidyes
echo You typed: %isit%
echo Hooray!
goto end
:saidno
echo You typed: %isit%
echo Aww
goto end
:end
pause
exit
Quotes are commonly misused in DOS batch files. Unlike UNIX shell scripts, the DOS batch language is not thought-out. For example, when the isit variable contains quotes
set isit="Y"
then the above if-statements expand to
IF ""Y"" == "Y" GOTO saidyes
IF ""Y"" == "N" GOTO saidno
because cmd.exe does not remove the quotes in "%isit%"=="Y" before evaluating the expression.
Quotes in isit shall not occur with the original batch file posted here. But often you process path names in batch files, and Windows passes long file names in quotes to hide blanks. For example, as in "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC". To work around this it is common practice to write if-statements so:
IF ["%isit%"] == ["Y"] GOTO saidyes
IF [%isit%] == ["Y"] GOTO saidyes
IF [%isit%] == [Y] GOTO saidyes
For set isit="Y" this becomes:
IF [""Y""] == ["Y"] GOTO saidyes
IF ["Y"] == ["Y"] GOTO saidyes
IF ["Y"] == [Y] GOTO saidyes
and for set isit=Y:
IF ["Y"] == ["Y"] GOTO saidyes
IF [Y] == ["Y"] GOTO saidyes
IF [Y] == [Y] GOTO saidyes
and for set isit=:
IF [""] == ["Y"] GOTO saidyes
IF [] == ["Y"] GOTO saidyes
IF [] == [Y] GOTO saidyes
which is far from being elegant, but at least works now for quoted and unquoted variables. It also works when isit is empty. Oftenly batch files stop because of empty variables:
set x=
REM ...
IF %x% == x GOTO x
because now the if-statement expands to:
IF == x GOTO x
and cmd.exe fails. These errors are usually hard to debug.
The trick is very old; I remember using it already in MSDOS. Note that the square brackets are not evaluated by cmd.exe; they're just some rarely used characters. But it's just a trick. Any advanced batch script needs a dequoting-function:
#echo off
setlocal EnableExtensions
setlocal EnableDelayedExpansion
REM ...
set /P isit="Y/N: "
call :dequote isit
REM ...
IF "!isit!" == "Y" GOTO saidyes
IF "!isit!" == "N" GOTO saidno
REM ...
goto done
:dequote
for /f "delims=" %%A in ('echo %%%1%%') do set %1=%%~A
goto :eof
:done
The deqote-subroutine removes any double quotes around the variable name passed to the function.
After dequoting the []-trick is not required anymore. Note also the use of ! instead of %. The exclamation marks force cmd.exe to reexpand isit.
I changed the code somewhat to help fix your problem. Instead of goto :(function) you just say goto (function)
#echo off
set /P theuserinput=Type your name:
echo So your name is: %theuserinput%?
set /p isit=Y/N:
echo You typed: %isit%
if %isit% == "y" goto saidyes
if %isit% == "n" goto saidno
:saidyes
echo Hooray!
:saidno
echo Aww
pause